StaticResource 在设计中工作,在运行时抛出 XamlParseException

发布于 2024-11-14 14:29:26 字数 775 浏览 3 评论 0原文

这看起来很愚蠢,但我已经找了几个小时了,但没有找到解决这个问题的方法。

我正在使用 MvvmLight。我有一个简单的主窗口,除了网格之外什么都没有。我有另一个类,它是包含一个按钮的 WPF UserControl。这两个都

DataContext="{Binding Main, Source={StaticResource Locator}}"

在其节点定义中(Window 或 UserControl)。 App.xaml 在其资源中具有标准条目:

<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />

我正在处理消息传递,并且收到一条消息,指出在主窗口中加载此 UserControl。我可以追踪,一切都运行得非常顺利——直到实例化新的 UserControl 以及后面的代码执行它的 InitializeComponent() 为止。

此时,应用程序在用户控件的 .xaml 文件中停止,并出现 XamlParseException ,抱怨找不到“定位器”(是的,我知道,资源名称区分大小写,谢谢)。

这对我来说完全没有意义。在用户控件的设计模式下,我可以导航到我想要的数据上下文 - 它看到 Locator,Locator 看到 Main,Main 看到我绑定到按钮的属性。设计者在按钮上显示文本。

有人吗???

这是 VS 2010 Ultimate(v10.0.30319.1)中的 C#,在 Windows 7 中运行。

This seems so silly, but I've been looking for a couple of hours and nothing I find fixes this problem.

I'm using MvvmLight. I have a simple MainWindow that has nothing in it but a Grid. I have another class that is a WPF UserControl that contains one button. Both of these have

DataContext="{Binding Main, Source={StaticResource Locator}}"

in their node definition (either Window or UserControl). App.xaml has the standard entry in its Resources:

<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />

I'm playing with messaging, and I've got a message that says load this UserControl in the MainWindow. I can trace through and everything works just peachy -- right up to the point where it is instantiating the new UserControl and the code behind does its InitializeComponent().

At this point the application stops in the user control's .xaml file with the XamlParseException complaining it cannot find "Locator" (and, yes, I know, resource names are case sensitive, thank you).

This makes absolutely no sense to me. In design mode in the user control, I can navigate to the data context I want -- it sees Locator, and Locator sees Main, and Main sees the property I'm binding to the button. And the designer SHOWS the text on the button.

Anybody???

This is C# in VS 2010 Ultimate, v10.0.30319.1, running in Windows 7.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洋洋洒洒 2024-11-21 14:29:26

嗯,关于实例化 xaml 对象(例如,InitializeComponent)的消息传递和顺序确实很重要。在我的原始代码中,我发送消息以从 MainViewModel 构造函数加载 UserControl;这是在 MainWindow 的 InitializeComponent() 调用期间发生的。

从构造函数中删除了消息,并在从 InitializeComponent() 方法返回后简单地将 UserControl 加载到 MainWindow 中。果然,没问题,运行得很好。

然后我向 MainViewModel 添加了一个名为 UIReady() 的方法,并在 InitializeComponent() 完成后从 MainWindow 调用该方法。 MainViewModel 中的 UIReady() 方法只是发出加载 UserControl 的消息。它可以工作,没问题。这是一个额外的方法来协调事物,但它实现了我想要的——MainWindow 不知道什么 UserControl 将被加载到其中,直到 ViewModel 请求特定的 View。然后它加载代表该视图的 UserControl。

我对 InitializeComponent 的所有结束和输出了解不够,但是其中的某些内容不喜欢初始化另一个 xaml 对象,直到它完成自己的初始化;它与诸如应用程序资源之类的事物失去了联系。

Well, it does matter about the messaging and the order of that with regard to instantiating the xaml object (e.g., InitializeComponent). In my original code, I was sending the message to load the UserControl from the MainViewModel constructor; this was occurring during the MainWindow's InitializeComponent() call.

Removed the message from the constructor and simply loaded the UserControl in the MainWindow AFTER returning from the InitializeComponent() method. Sure enough, no problem, it worked fine.

So then I added a method to MainViewModel called UIReady() and called that from MainWindow AFTER InitializeComponent() was done. The UIReady() method in MainViewModel simply issues the message to load the UserControl. And it works, no problem. So an extra method to coordinate things, but it accomplishes what I want -- the MainWindow has no idea what UserControl is going to be loaded in it until a ViewModel requests a particular View. Then it loads the UserControl that represents that View.

I don't know enough about all the ends and outs of InitializeComponent, but SOMETHING in there does not like initializing another xaml obect until it finishes its own initialization; it loses touch with things -- like Application Resources.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文