C# 加载页面时发生XamlParseException

C# 加载页面时发生XamlParseException,c#,windows-phone-8,windows-phone,xamlparseexception,C#,Windows Phone 8,Windows Phone,Xamlparseexception,我在我的应用程序中发现了一个奇怪的问题。当我打开其中一个页面时,应用程序的InitializeComponent()方法会抛出一个XamlParseException。我能做些什么来解决这个问题?我在xaml代码中也没有发现任何明显的错误。在这里您可以看到页面的my.cs和.xaml文件: using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Window

我在我的应用程序中发现了一个奇怪的问题。当我打开其中一个页面时,应用程序的InitializeComponent()方法会抛出一个XamlParseException。我能做些什么来解决这个问题?我在xaml代码中也没有发现任何明显的错误。在这里您可以看到页面的my.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;
            }
        }
    }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Navigation;
使用Microsoft.Phone.Controls;
使用Microsoft.Phone.Shell;
使用System.IO.IsolatedStorage;
名称空间应用程序名称
{
公共部分类NewsSummaryPage:PhoneApplicationPage
{
列表保存列表;
项目清单;
列表主题列表;
隔离存储设置;
布尔值amISelectingProjects=true;
布尔值firstSelection=true;
字符串练习摘要文本;
公共新闻摘要页()
{
初始化组件();
//初始化设置
设置=隔离存储设置。应用程序设置;
}
private void nextButton_Click(对象发送方,路由目标)
{
//将现有列表加载到保存列表中(用于临时存储)
savingList=null;
savingList=readSetting(“practiceList”)!=null?(列表)readSetting(“practiceList”):新建列表();
//从隔离存储中删除现有的practiceList
设置。删除(“实践者”);
//将新的实践总结添加到保存列表中
savingList.Add(practiceTextBox.Text.ToString());
//将保存列表保存到IsolatedStorage中的practiceList中
设置。添加(“实践者”,保存列表);
//保存摘要文本本身。将“-PracticesSummary”添加到名称末尾,以便能够添加它而不会与项目列表及其引用冲突
添加(practiceTextBox.Text.ToString()+“-practicesummary”,practicesummaryText);
//保存设置
设置。保存();
MessageBox.Show(“下一步”);
}
已加载private void newsummary页面(对象发送方,路由目标)
{
//初始化列表
projectList=新列表();
尝试
{
//加载现有列表
选择Projects和SubjectsListBox.Items.Clear();
projectList.Clear();
MessageBox.Show(“已加载”);
projectList=readSetting(“projectList”)!=null?(List)readSetting(“projectList”):new List();
选择Projects和SubjectsListBox.ItemsSource=projectList;
}
捕获(例外)
{
//运行方法CurrentProjectNotEmpty();
}
}
私有静态对象读取设置(字符串键)
{
返回IsolatedStorageSettings.ApplicationSettings.Contains(键)?IsolatedStorageSettings.ApplicationSettings[键]:null;
}
private void SelectProjects和SubjectsList Box_Selection已更改(对象发送者,System.Windows.Controls.SelectionChangedEventArgs e)
{
}
private void SelectProjects和Subjects列表框(对象发送者,System.Windows.SizeChangedEventArgs e)
{
//根据amISelectingProjects变量选择项目/主题
if(amISelectingProjects.Equals(true))
{
//用户正在选择一个项目。从项目中获取名称并使用它获取主题
var selectedItem=selectprojects和subjectsListBox.selectedItem作为字符串;
String item=selectedItem;
MessageBox.Show(“选择”);
subjectsList=readSetting(项目)!=null?(列表)readSetting(项目):新建列表();
选择Projects和SubjectsListBox=null;
选择项目和主题列表框。项目资源=主题列表;
//将amISelectingProjects设置为false,以便用户下次可以选择主题
amISelectingProjects=false;
//将列表框设置为多选模式以选择多个主题
SelectProjects和SubjectsListBox.SelectionMode=SelectionMode.Multiple;
//告诉用户下一步应该做什么
MessageBox.Show(“与所选项目对应的主题现在将显示在列表中。点击所有要创建摘要的项目,然后在完成后按create。”);
}
else if(amISelectingProjects.Equals(false))
{
//用户正在选择主题。请选择多个主题
if(firstSelection.Equals(true))
{
PracticesSummaryText=选择项目和主题列表框。选择编辑项作为字符串;
firstSelection=false;
}
else if(firstSelection.Equals(false))
{
//这不是用户选择的第一个主题,因此在两者之间添加一些字符
PracticesSummaryText=PracticesSummaryText+”。新建主题=“+选择项目和主题列表框。选择编辑项作为字符串;
}
}
}
}
}
我的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()上 在Knowledge_Organizer.newsummaryPage..ctor()}System.Exception{System.Windows.Markup.XamlParseException}

查看输出窗口,它通常包含有关异常内容的有用信息。

或者当出现异常窗口时,按break并转到visual studio调试菜单,选择windows->locals。异常应出现在“局部视图”中

或者尝试调试并捕获异常,然后查看其属性和内部消息


检查内部异常是否存在实际异常