c# - Windows Phone 8 View 模型绑定(bind)

标签 c# xaml mvvm windows-phone-8

我正在做一个 Windows Phone 8 项目,我正在做一些我在 WPF 和 WP7 中做过多年的事情,但它似乎在 Windows Phone 8 中不起作用。我创建了另一个项目,并重现了这个问题的一个更简单的形式。我创建一个新的 WP8 项目,并执行以下操作:

1) 添加一个新类,TestVM.cs

class TestVM : DependencyObject
{
    public string TestProperty
    {
        get { return (string)GetValue(TestPropertyProperty); }
        set { SetValue(TestPropertyProperty, value); }
    }

    // Using a DependencyProperty as the backing store for TestProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TestPropertyProperty =
        DependencyProperty.Register("TestProperty", typeof(string), typeof(TestVM), new PropertyMetadata(string.Empty));
}

2) 修改App.xaml 使得<Application.Resources />看起来像这样:

<!--Application Resources-->
<Application.Resources>
    <local:TestVM x:Key="MainVM" />
    <local:LocalizedStrings xmlns:local="clr-namespace:VMTest" x:Key="LocalizedStrings"/>
</Application.Resources

3) 添加DataContext="{StaticResource MainVM}"MainPage.xaml .

启动我的应用程序时,出现以下异常:

System.Windows.Markup.XamlParseException: Cannot create instance of type 'VMTest.TestVM' [Line: 11 Position: 29]
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   at VMTest.App.InitializeComponent()
   at VMTest.App..ctor()

有人知道发生了什么事吗?正如我所说,我可以在 WP7 中做完全相同的事情,而且效果很好。

最佳答案

不能在 XAML 中创建未标记为明确公开的对象的实例。

关于c# - Windows Phone 8 View 模型绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15229049/

相关文章:

C# 缩放 GDI 位置但不缩放字体大小或线条粗细

c# - ASP.NET Core Razor Pages OnGet of Error 模型未被执行

c# - Winforms WPF Interop - WPF 内容无法绘制

带有 DataTemplate 的 WPF TabControl 行为很奇怪

c# - Wpf gridview 选定项

c# - 未调用 EF 4.1 OnModelCreating

wpf - C++/CLI、XAML 和事件处理程序

c# - 在 WPF DataGrid 中手动添加行

wpf - 如何在这种情况下应用 MVVM

c# - 无法将枚举绑定(bind)到组合框 wpf mvvm