Xaml 无法创建“X”的实例

发布于 2024-11-04 09:50:08 字数 1318 浏览 0 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

捎一片雪花 2024-11-11 09:50:08

这是因为实例化ApplicationsSettings 会引发异常。如果将以下内容添加到构造函数中,应该没问题;

try
{
    settings = IsolatedStorageSettings.ApplicationSettings;
}
catch (System.IO.IsolatedStorage.IsolatedStorageException e)
{
    // handle exception
}

This is because instantiating ApplicationsSettings throws an exception. If you add the following to your constructor, you should be fine;

try
{
    settings = IsolatedStorageSettings.ApplicationSettings;
}
catch (System.IO.IsolatedStorage.IsolatedStorageException e)
{
    // handle exception
}
瘫痪情歌 2024-11-11 09:50:08

对于像这样在 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.

℉服软 2024-11-11 09:50:08

一种可能的原因也可能是依赖属性初始化失败。

我在尝试在 XAML 中实例化的类中有以下代码:

public static readonly DependencyProperty ListViewObjectProperty = DependencyProperty.Register(
                                                                                                "ListViewObject",
                                                                                                typeof(ListView),
                                                                                                typeof(WidthConverter),
                                                                                                new UIPropertyMetadata(0));

...其中此依赖项属性用于保存对 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:

public static readonly DependencyProperty ListViewObjectProperty = DependencyProperty.Register(
                                                                                                "ListViewObject",
                                                                                                typeof(ListView),
                                                                                                typeof(WidthConverter),
                                                                                                new UIPropertyMetadata(0));

...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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文