当通过 DataTemplate 应用时,为什么我的 WPF 视图在没有代码隐藏的情况下无法工作?

发布于 2024-09-30 08:53:21 字数 402 浏览 5 评论 0 原文

我正在使用 MVVM 开发一个大型 WPF 项目。目前,我们仍在决定使用代码隐藏的程度,但到目前为止,我们还没有任何进展(Windows 上的 InitializeComponent 除外)。但是,我最近开始使用类型化 DataTemplates 将视图应用到我的视图模型,并且根据 这篇文章,我想他们会的。 DataTemplate 只是声明一个视图。当我删除视图的代码隐藏文件时,视图模型呈现完全空白。当我把它留在里面时,就没事了。有什么想法为什么我可能会看到这种行为吗?

I'm working on a large WPF project using MVVM. We're currently still deciding the extent to which we'll use code-behind, but so far we've gotten along fine with none at all (except for InitializeComponent on windows). However, I recently started using typed DataTemplates to apply views to my view models, and it seems these views, like windows, do not work without the InitializeComponent call, when, according to this article, I thought they would. The DataTemplate just declares a view. When I delete the view's code-behind file, the view model renders completely blank. When I leave it in, it's fine. Any ideas why I might be seeing this behavior?

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

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

发布评论

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

评论(2

多孤肩上扛 2024-10-07 08:53:21

首先,您可能忽略了一些重要的事情:我在学习 MVVM/WPF 时也大量使用了那篇文章,而且我从未想过它建议消除 View.cs 中的 InitializeComponent 调用。

事实上,快速搜索会发现以下内容(在中继命令逻辑下)[强调我的]:

应用程序中的每个视图都有一个空的
代码隐藏文件,除了
调用的标准样板代码
类中的InitializeComponent
构造函数。

我一直在应用您描述的相同模式,同时保留每个视图的默认代码隐藏,到目前为止一切顺利。 :)

此外:如果您检查默认 InitializeComponent() 的定义,您将看到生成的代码包含以下语句:

System.Windows.Application.LoadComponent(this, ResourceLocater);

我还没有测试来确保情况确实如此,但我敢打赌,阻止该行执行将影响您视图的渲染... ;)

First, you may be overlooking something important: I used that article heavily when learning MVVM/WPF as well, and I never thought it suggested eliminating InitializeComponent calls from the View.cs.

In fact, doing a quick search reveals the following (under Relaying Command Logic) [emphasis mine]:

Every view in the app has an empty
codebehind file, except for the
standard boilerplate code that calls
InitializeComponent in the class's
constructor.

I've been applying the same pattern you describe while leaving the default code-behind for each view in place, and so far it's smooth sailing. :)

Further: If you check out the definition for the default InitializeComponent(), you'll see that the generated code contains the following statement:

System.Windows.Application.LoadComponent(this, resourceLocater);

I haven't tested to make sure this is the case, but I'll wager a fiddle of gold against your soul that preventing that line from executing is going to affect the rendering of your view... ;)

灵芸 2024-10-07 08:53:21

根据 djacobson 的说法,即使是我在问题中引用的 Josh Smith 的解决方案也无法在没有代码隐藏的情况下呈现其视图,因此说明可以完成此操作的行具有误导性。似乎避免视图代码隐藏的唯一方法是根本不将它们放入 UserControls 中,而只是将 XAML 直接保留在 << 中。数据模板>标签。

According to djacobson, even the solution by Josh Smith that I referenced in my question cannot render its views without the code-behind, so the line stating this can be done is misleading. It seems the only way to avoid code-behind for your views is by not putting them into UserControls at all, but just keeping the XAML directly within a < DataTemplate > tag.

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