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.
TestVM
supposed to be aViewModel
? then why is it aDependencyObject
? – Federico Berasategui