有一些方法可以专门为设计时创建视图模型。最简单的方法可能是这样的:
<TextBlock Text="{x:Bind ViewModel.SomeExampleText, FallbackValue='Hello!'}"/>
那一个在设计器中显示字符串“Hello”,同时带有Binding
和x:Bind
。
因为Binding
您可以像这样设置设计时数据上下文:
<Page
...
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="using:MyNameSpace.ViewModels"
d:DataContext="{d:DesignInstance Type=viewModels:DesignTimeViewModel, IsDesignTimeCreatable=True}"
mc:Ignorable="d">
DesignTimeViewModel
不需要与您的运行时视图模型有任何特定关系;它只需要具有相同名称的合适属性。如果您绑定到集合,这可能是您最好的选择。