简体   繁体   中英

Windows Phone 8 Viewmodel Binding

I'm working on a Windows Phone 8 project, and I'm doing something that I've done in WPF and WP7 for ages, and it doesn't seem to work in Windows Phone 8. I created another project, and reproduced a simpler form of this problem. I create a new WP8 project, and do the following:

1) Add a new class, 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) Modify App.xaml so that <Application.Resources /> looks like this:

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

3) Add DataContext="{StaticResource MainVM}" to MainPage.xaml .

Upon booting my app, I get the following exception:

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()

Anyone have an idea as to what's going on? As I said, I can do exactly the same thing in WP7 and it'll work fine.

您无法在XAML中创建未标记为显式公共的对象实例。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM