簡體   English   中英

Windows Phone 8 Viewmodel綁定

[英]Windows Phone 8 Viewmodel Binding

我正在開發一個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中創建未標記為顯式公共的對象實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM