简体   繁体   English

可混合性来自何处?

[英]Where does the Blendability come from?

In the last days I studied MVVM and I downloaded a couple of samples. 在过去的几天里,我学习了MVVM,并下载了一些样本。

The following article is very good and it included the source code: 以下文章非常好,其中包括源代码:
Better User and Developer Experiences – From Windows Forms to WPF with MVVM 更好的用户和开发人员体验–从Windows窗体到带有MVVM的WPF
http://reedcopsey.com/series/windows-forms-to-mvvm/ http://reedcopsey.com/series/windows-forms-to-mvvm/

When I first opened the project in Visual Studio 2013 all the fields and lists were empty like expected. 当我第一次在Visual Studio 2013中打开项目时,所有字段和列表都为空,这与预期的一样。 The program is a RSS reader and I loaded one feed with lots of feed items which filled in all the fields in the form. 该程序是RSS阅读器,我在一个提要中加载了很多提要项,这些提要项填充了表单中的所有字段。 But now even when I stopped the program I can see all the data in the design. 但是现在,即使我停止了程序,也可以看到设计中的所有数据。

所有字段和列表均以设计方式填写

This is obviously very good because it makes it a lot easier to see and possibly amend the design. 这显然非常好,因为它使查看和修改设计变得更加容易。
I think what I saw is covered under the name Blendability – but I am not sure about this. 我认为我所看到的名称包含在“可混合性”这个名称下-但我对此不确定。

Now my question: Where does this Blendability come from? 现在我的问题是:这种可混合性来自何处? I looked at the source code of the project and I don't find where the data comes from in design mode. 我查看了项目的源代码,但没有找到设计模式下数据的来源。

Over the last days I installed a couple of extensions in Visual Studio and maybe I installed something which causes this behaviour. 在过去的几天里,我在Visual Studio中安装了几个扩展,也许我安装了一些导致这种行为的扩展。 Or maybe this is some special function in this example (which is not mentioned anywhere in the article). 也许这是此示例中的某些特殊功能(本文中未提及)。

If possible please let me know where this Blendability comes from and how I can implement it in my own projects. 如果可能,请让我知道此可混合性来自何处以及如何在自己的项目中实现它。

Design time has a nifty feature where if you were to set the DataContext to a class, then that class will actually be instantiated at design-time. 设计时具有一个漂亮的功能,如果您将DataContext设置为一个类,那么该类实际上将在设计时实例化

It's quite probable that the DataContext of this particular window is a class with a constructor that executes the RSS reading code. 这个特定窗口的DataContext很可能是一个带有构造函数的类,该构造函数执行RSS读取代码。

Take this for example: 以这个为例:

public class MyRssReaderViewModel
{
    public MyRssReaderViewModel()
    {
        //Read RSS and populate properties
        LoadRSS();
    }

    public void LoadRSS()
    {
        ...
    }
    ...
}

Now if you were to set the DataContext like this: 现在,如果要像这样设置DataContext

<Window.DataContext>
    <ViewModels:MyRssReaderViewModel/>
</Window.DataContex>

The designer will instantiate the class at design-time, and by extension call the LoadRSS method. 设计人员将在设计时实例化该类,并通过扩展调用LoadRSS方法。 Any elements that reference properties in your view model will update to display the bound data. 引用视图模型中的属性的任何元素都将更新以显示绑定数据。

One important thing to note is that the class will be instantiated every time you build the project. 需要注意的重要一件事是,每次构建项目时都会实例化该类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM