在设计时如何使IDE识别MVVM中的View-ViewModel绑定?

问题描述

在我的应用程序中,我通过App.xaml中的中央ResourceDictionary将视图绑定到ViewModels,就像这样:

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="_styles/SomeStyle.xaml" />
                ...
                <ResourceDictionary>
                    <DataTemplate DataType="{x:Type viewModels:MyViewModel}">
                        <views:MyView />
                    </DataTemplate>
                    ...
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

这很好。但是,IDE(带有ReSharper和Rider的Visual Studio)无法识别视图中的绑定,因此例如重命名属性很危险。

如何使IDE识别绑定?

解决方法

添加到您的窗口或用户控件。要使用IsDesignTimeCreatable = True,您必须具有无参数的构造函数。

d:DataContext="{d:DesignInstance Type=local:YourVm,IsDesignTimeCreatable=True}"

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...