问题描述
|
我试图在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的行(local:AppSettings行)上,我收到一条错误消息,指出\“无法创建AppSettings \的实例”。
解决方法
这是因为实例化ApplicationsSettings会引发异常。如果将以下内容添加到构造函数中,就可以了;
try
{
settings = IsolatedStorageSettings.ApplicationSettings;
}
catch (System.IO.IsolatedStorage.IsolatedStorageException e)
{
// handle exception
}
,对于像这样在xaml中引用的对象,它们需要有一个默认的无参数构造函数。我会仔细检查这种情况。
其他潜在问题可能是构造函数中引发的异常。
,一种可能的原因也可能是依赖属性初始化失败。
我试图在XAML中实例化该类中的以下代码:
public static readonly DependencyProperty ListViewObjectProperty = DependencyProperty.Register(
\"ListViewObject\",typeof(ListView),typeof(WidthConverter),new UIPropertyMetadata(0));
...此依赖项属性用于保存对ListView的引用。但是VS的默认“ propdp”代码片段生成了这个“新UIPropertyMetadata(0)”,这在引用变量的情况下有点不对。它应该是\“ new UIPropertyMetadata(null)\”。
更改此设置对我来说解决了这个问题。由于某种原因,我在运行时不会收到任何可见的异常。