我正在做一个Windows phone8项目,我正在做一些我在WPF和WP7中做了很长时间的事情,但它在Windows phone8中似乎不起作用。我创建了另一个项目,并重现了这个问题的一个更简单的形式。我创建了一个新的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.Resources3)在MainPage.xaml中添加DataContext="{StaticResource MainVM}"。
在启动我的应用程序时,我得到了以下异常:
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中做完全相同的事情,而且它会工作得很好。
发布于 2013-03-08 02:24:00
不能在XAML中创建未显式标记为public的对象的实例。
https://stackoverflow.com/questions/15229049
复制相似问题