我正在寻找一种正确的方式来声明一个 Avalonia 窗口的设计时 ViewModel。
有些示例建议:
d:DataContext="{d:DesignInstance viewModels:LoginViewModel, IsDesignTimeCreatable=True}"
这会抛出异常
XamlParseException at 5:5: Unable to resolve type DesignInstance from namespace http://schemas.microsoft.com/expression/blend/2008
默认的Avalonia MVVM模板建议
<Design.DataContext>
<vm:MainWindowViewModel/>
</Design.DataContext>
如果ViewModel需要参数,则会抛出异常。
XamlLoadException at 16:10: Unable to find public constructor for type Demo.CloseNonModalDialog:Demo.CloseNonModalDialog.CurrentTimeDialogViewModel()
我猜添加一个默认的无参构造函数是一个选项。
在MvvmLight/WPF中,我过去常常将ViewLocator作为静态资源引用。
DataContext="{Binding Source={StaticResource Locator}, Path=MainWindow}"
这是一个选项,尽管我还没有找到正确的方法来声明和引用资源。
在这里推荐什么方法?如果我想显示设计时数据,我会说只有第三个选项才能起作用。这不是示例中显示的选项。