Windows 8 如何在WinRT XAML中拥有设计时数据?
如何在WinRT XAML中获取设计时数据,以便设计器显示示例数据?非常简单 创建一个如下所示的模型:Windows 8 如何在WinRT XAML中拥有设计时数据?,windows-8,winrt-xaml,Windows 8,Winrt Xaml,如何在WinRT XAML中获取设计时数据,以便设计器显示示例数据?非常简单 创建一个如下所示的模型: public class Fruit { public string Name { get; set; } } public class BaseViewModel { public ObservableCollection<Fruit> Fruits { get; set; } } public class RealViewModel : BaseView
public class Fruit
{
public string Name { get; set; }
}
public class BaseViewModel
{
public ObservableCollection<Fruit> Fruits { get; set; }
}
public class RealViewModel : BaseViewModel
{
public RealViewModel()
{
if (!Windows.ApplicationModel.DesignMode.DesignModeEnabled)
LoadData();
}
public void LoadData()
{
// TODO: load from service
}
}
public class FakeViewModel : BaseViewModel
{
public FakeViewModel()
{
this.Fruits = new ObservableCollection<Fruit>
{
new Fruit{ Name = "Blueberry"},
new Fruit{ Name = "Apple"},
new Fruit{ Name = "Banana"},
new Fruit{ Name = "Orange"},
new Fruit{ Name = "Strawberry"},
new Fruit{ Name = "Mango"},
new Fruit{ Name = "Kiwi"},
new Fruit{ Name = "Rasberry"},
new Fruit{ Name = "Blueberry"},
};
}
}
创建一个基本视图模型,如下所示:
public class Fruit
{
public string Name { get; set; }
}
public class BaseViewModel
{
public ObservableCollection<Fruit> Fruits { get; set; }
}
public class RealViewModel : BaseViewModel
{
public RealViewModel()
{
if (!Windows.ApplicationModel.DesignMode.DesignModeEnabled)
LoadData();
}
public void LoadData()
{
// TODO: load from service
}
}
public class FakeViewModel : BaseViewModel
{
public FakeViewModel()
{
this.Fruits = new ObservableCollection<Fruit>
{
new Fruit{ Name = "Blueberry"},
new Fruit{ Name = "Apple"},
new Fruit{ Name = "Banana"},
new Fruit{ Name = "Orange"},
new Fruit{ Name = "Strawberry"},
new Fruit{ Name = "Mango"},
new Fruit{ Name = "Kiwi"},
new Fruit{ Name = "Rasberry"},
new Fruit{ Name = "Blueberry"},
};
}
}
创建一个伪数据视图模型,如下所示:
public class Fruit
{
public string Name { get; set; }
}
public class BaseViewModel
{
public ObservableCollection<Fruit> Fruits { get; set; }
}
public class RealViewModel : BaseViewModel
{
public RealViewModel()
{
if (!Windows.ApplicationModel.DesignMode.DesignModeEnabled)
LoadData();
}
public void LoadData()
{
// TODO: load from service
}
}
public class FakeViewModel : BaseViewModel
{
public FakeViewModel()
{
this.Fruits = new ObservableCollection<Fruit>
{
new Fruit{ Name = "Blueberry"},
new Fruit{ Name = "Apple"},
new Fruit{ Name = "Banana"},
new Fruit{ Name = "Orange"},
new Fruit{ Name = "Strawberry"},
new Fruit{ Name = "Mango"},
new Fruit{ Name = "Kiwi"},
new Fruit{ Name = "Rasberry"},
new Fruit{ Name = "Blueberry"},
};
}
}
公共类FakeViewModel:BaseViewModel
{
公共FakeViewModel()
{
this.Fruits=新的可观察集合
{
新水果{Name=“蓝莓”},
新水果{Name=“Apple”},
新水果{Name=“香蕉”},
新水果{Name=“Orange”},
新水果{Name=“草莓”},
新水果{Name=“Mango”},
新水果{Name=“Kiwi”},
新水果{Name=“Rasberry”},
新水果{Name=“蓝莓”},
};
}
}
在XAML中执行此操作:
<Page.DataContext>
<local:RealViewModel />
</Page.DataContext>
<d:Page.DataContext>
<local:FakeViewModel />
</d:Page.DataContext>
玩得开心
PS:您也可以尝试使用。
这种方法也有效。我觉得这不那么直截了当。
最后,这取决于你怎么做。
无论哪种方式,都不要错过设计时间数据 以下是d:DesignInstance示例: 我还将使用Jerry的水果类,但我不会在这里使用MVVM,因为您不需要它来工作 基本上,我们需要创建我们想要拥有设计数据的数据模型类(例如,ViewModel或model)(例如,在本例中,我创建了一个子类,但您不需要) 然后在我们的XAML中,我们可以使用d:DataContext绑定子类
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"
DataContext="{Binding}"
d:DataContext="{Binding Source={d:DesignInstance Type=local:SampleFruit, IsDesignTimeCreatable=True}}">
<TextBlock Text="{Binding Name}"
HorizontalAlignment="Center" VerticalAlignment="Center"
FontSize="42"/>
</Grid>
现在,您应该可以在VisualStudioDesigner和Blend上看到设计时数据
另外,在Blend 2013中,还有一个数据选项卡,可以让您创建示例数据。希望看到使用d:DesignData:)的示例。实际上,无论您是否找到它,请参见以下内容:。这更好,因为您只需通过xaml即可完成,不需要在代码中进行太多操作,这更有意义(对我来说)。在Windows8中尝试其他方法后,请与我联系。其他方法非常有效。我从real view模型派生了一个类,只插入了伪数据,然后在xaml中使用了以下代码:d:DataContext=“{d:DesignInstance Type=my:MyDerievedSampleDataClass,IsDesignTimeCreatable=True}”非常棒,感谢您的更新!为什么不把你的解决方案作为这个问题的答案贴出来,让别人看到呢?太棒了。简直太棒了。谢谢