3
votes

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.

1
can you add the whole inner exceptionZaki
Is that TestVM supposed to be a ViewModel? then why is it a DependencyObject?Federico Berasategui
I would highly recommend using MVVM Light, it makes things like this much easier.robertk
Strange thing is, there is no inner exception.Matthew Kennedy
try to make TestVM publicDenis

1 Answers

7
votes

You can't create an instance of an object in XAML that isn't marked as explicitly public.