Xaml 无法创建“X”的实例
我正在尝试在 Windows Phone 7 中为我的应用程序创建一个“设置”页面。我创建了 AppSettings 类并从我的 MainPage.xaml 引用它。这是我的代码:
<phone:PhoneApplicationPage
x:Class="Shapes4Kids.MainPage"
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"
xmlns:local="clr-namespace:ShapesSettings;assembly=Shapes4Kids"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="696"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<phone:PhoneApplicationPage.Resources>
<local:AppSettings x:Key="appSettings"></local:AppSettings>
</phone:PhoneApplicationPage.Resources>
但是在我引用 AppSettings 的行(本地:AppSettings 行)上,我收到一条错误消息,指出“无法创建 AppSettings 的实例”。
I am trying to create a Settings page for my app in Windows Phone 7. I created the AppSettings class and is referring it from my MainPage.xaml. This is my code:
<phone:PhoneApplicationPage
x:Class="Shapes4Kids.MainPage"
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"
xmlns:local="clr-namespace:ShapesSettings;assembly=Shapes4Kids"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="696"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<phone:PhoneApplicationPage.Resources>
<local:AppSettings x:Key="appSettings"></local:AppSettings>
</phone:PhoneApplicationPage.Resources>
But on the line where I refer the AppSettings (local:AppSettings line) , I get an error message stating that " cannot create an instance of AppSettings".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为实例化ApplicationsSettings 会引发异常。如果将以下内容添加到构造函数中,应该没问题;
This is because instantiating ApplicationsSettings throws an exception. If you add the following to your constructor, you should be fine;
对于像这样在 xaml 中引用的对象,它们需要有一个默认的无参数构造函数。我会仔细检查一下情况是否如此。
其他潜在问题可能是构造函数中引发的异常。
For objects to be reference in xaml like this they need to have a default parameterless constructor. I'd double check this is the case.
Other potential issues could be an exception thrown in the constructor.
一种可能的原因也可能是依赖属性初始化失败。
我在尝试在 XAML 中实例化的类中有以下代码:
...其中此依赖项属性用于保存对 ListView 的引用。但是 VS 默认的“propdp”代码片段生成了这个“new UIPropertyMetadata(0)”,这在引用变量的情况下有点错误。它应该是“new UIPropertyMetadata(null)”。
改变这个解决了我的问题。由于某种原因,我不想在运行时收到任何可见的异常。
One possible reason could also be failing dependency property initialization.
I had a following code in the class that I was trying to instantiate in XAML:
...where this dependency property was intended for holding a reference to ListView. But VS default "propdp" code snippet generated this "new UIPropertyMetadata(0)" which is a bit wrong in case of reference variable. It should be "new UIPropertyMetadata(null)".
Changing this fixed the issue for me. For some reason I wans't receiving any visible exception from this at runtime.