加载页面时出现XamlParseException

时间:2014-01-05 13:48:53

标签: c# windows-phone-8 windows-phone xamlparseexception

我在我的应用中发现了一个奇怪的问题。当我打开其中一个页面时,应用程序的InitializeComponent()方法抛出一个XamlParseException。我该怎么做才能摆脱这个问题?我的xaml代码中也没有发现任何明显的错误。在这里,您可以看到我的页面的.cs和.xaml文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage;

namespace app_name
{
public partial class newsummaryPage : PhoneApplicationPage
{
    List<String> savingList;
    List<String> projectList;
    List<String> subjectsList;

    IsolatedStorageSettings settings;
    Boolean amISelectingProjects = true;
    Boolean firstSelection = true;

    String practicesummaryText;
    public newsummaryPage()
    {
        InitializeComponent();

        //Initialize settings
        settings = IsolatedStorageSettings.ApplicationSettings;
    }

    private void nextButton_Click(object sender, RoutedEventArgs e)
    {
        //Load existing list into the savingList (For temporary storage)
        savingList = null;
        savingList = readSetting("practiceList") != null ? (List<String>)readSetting("practiceList") : new List<String>();

        //Remove existing practiceList from IsolatedStorage
        settings.Remove("practiceList");

        //Add the new practice summary to the savingList
        savingList.Add(practiceTextBox.Text.ToString());

        //Save the savingList into practiceList in IsolatedStorage
        settings.Add("practiceList", savingList);

        //Save the summary text itself. Add "-practicesummary" to the end of the name to be able to add it without conflicts with the projectList and it's references
        settings.Add(practiceTextBox.Text.ToString() + "-practicesummary", practicesummaryText);

        //Save the settings
        settings.Save();

        MessageBox.Show("next");
    }

    private void newsummaryPage_Loaded(object sender, RoutedEventArgs e)
    {
        //Initialize the list
        projectList = new List<String>();
        try
        {
            //Load existing list
            selectprojectsandsubjectsListBox.Items.Clear();
            projectList.Clear();
            MessageBox.Show("loaded");
            projectList = readSetting("projectList") != null ? (List<String>)readSetting("projectList") : new List<String>();

            selectprojectsandsubjectsListBox.ItemsSource = projectList;
        }
        catch (Exception)
        {
            //run method CurrentProjectNotEmpty();
        }
    }

    private static object readSetting(string key)
    {
        return IsolatedStorageSettings.ApplicationSettings.Contains(key) ? IsolatedStorageSettings.ApplicationSettings[key] : null;
    }

    private void selectprojectsandsubjectsListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
    }

    private void selectprojectsandsubjectsListBox_Tap(object sender, System.Windows.SizeChangedEventArgs e)
    {
        //Select a project/subject depending on the amISelectingProjects variable
        if (amISelectingProjects.Equals(true))
        {
            //The user is selecting a project. Get the name from the project and use it to get the subjects
            var selectedItem = selectprojectsandsubjectsListBox.SelectedItem as String;
            String item = selectedItem;

            MessageBox.Show("select");
            subjectsList = readSetting(item) != null ? (List<String>)readSetting(item) : new List<String>();
            selectprojectsandsubjectsListBox = null;
            selectprojectsandsubjectsListBox.ItemsSource = subjectsList;

            //Set amISelectingProjects to false so that the user can select subjects next time
            amISelectingProjects = false;

            //Set the ListBox to multiple selection mode for selecting multiple subjects
            selectprojectsandsubjectsListBox.SelectionMode = SelectionMode.Multiple;

            //Tell the user what he should do next
            MessageBox.Show("The subjects corresponding the the project you selected will now appear in the list. Tap on all the items you want to create a summary of and then press create when you're finished.");
        }
        else if (amISelectingProjects.Equals(false))
        {
            //The user is selecting a subject. Select multipe
            if (firstSelection.Equals(true))
            {
                practicesummaryText = selectprojectsandsubjectsListBox.SelectedItem as String;
                firstSelection = false;
            }
            else if (firstSelection.Equals(false))
            {
                //This is not the first subject that the user selects, therefore add some characters in between
                practicesummaryText = practicesummaryText + ". New subject= " + selectprojectsandsubjectsListBox.SelectedItem as String;
            }
        }
    }
}
}

我的xaml文件:

<phone:PhoneApplicationPage
x:Class="app_name.newsummaryPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="#FF03171B" Loaded="newsummaryPage_Loaded">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" MinHeight="1111"/>
        <RowDefinition Height="0*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel Grid.Row="0" Margin="12,0,0,0" Height="116" VerticalAlignment="Top">
        <TextBlock Text="app name" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock Text="New summary" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>
    <Grid x:Name="newsummaryGrid" Margin="0,116,12,370">
        <TextBox x:Name="practiceTextBox" HorizontalAlignment="Left" TextWrapping="Wrap" Width="458" Margin="0,27,0,0" Height="72" VerticalAlignment="Top"/>
        <TextBlock HorizontalAlignment="Left" Margin="10,0,0,0" TextWrapping="Wrap" Text="Enter a new name for your summary:" VerticalAlignment="Top"/>
        <Button x:Name="nextButton" Content="Create the summary" HorizontalAlignment="Left" Margin="0,553,0,0" VerticalAlignment="Top" Width="456" Click="nextButton_Click"/>
        <ListBox x:Name="selectprojectsandsubjectsListBox" Margin="10,99,12,80" SelectionChanged="selectprojectsandsubjectsListBox_SelectionChanged" FontSize="36" Tap="selectprojectsandsubjectsListBox_Tap"/>
    </Grid>

    <!--ContentPanel - place additional content here-->
</Grid>

</phone:PhoneApplicationPage>

内部异常:

  • $ exception {System.Windows.Markup.XamlParseException:无法分配给属性'System.Windows.UIElement.Tap'。 [线:32位置:172] 在System.Windows.Application.LoadComponent(对象组件,Uri resourceLocator) 在Knowledge_Organizer.newsummaryPage.InitializeComponent() at Knowledge_Organizer.newsummaryPage..ctor()} System.Exception {System.Windows.Markup.XamlParseException}

1 个答案:

答案 0 :(得分:1)

请参阅输出窗口,它通常包含有关异常内容的有用信息。 enter image description here

或出现异常窗口时,按中断并转到visual studio DEBUG菜单,选择windows-&gt; locals。该异常应出现在locals视图中。

或尝试调试并捕获异常,而不是查看它的属性和内部消息。

enter image description here enter image description here