gpt4 book ai didi

windows-8 - 如何在 WinRT XAML 中拥有 DesignTime 数据?

转载 作者:行者123 更新时间:2023-12-04 01:32:38 26 4
gpt4 key购买 nike

如何在 WinRT XAML 中获取 DesignTime 数据以便设计器显示示例数据?

最佳答案

足够简单。

像这样创建一个模型:

public class Fruit 
{
public string Name { get; set; }
}

像这样创建一个基本的 ViewModel:
public class BaseViewModel 
{
public ObservableCollection<Fruit> Fruits { get; set; }
}

像这样创建一个真正的 ViewModel:
public class RealViewModel : BaseViewModel
{
public RealViewModel()
{
if (!Windows.ApplicationModel.DesignMode.DesignModeEnabled)
LoadData();
}

public void LoadData()
{
// TODO: load from service
}
}

像这样创建一个假数据 ViewModel:
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"},
};
}
}

在您的 XAML 中执行此操作:
<Page.DataContext>
<local:RealViewModel />
</Page.DataContext>

<d:Page.DataContext>
<local:FakeViewModel />
</d:Page.DataContext>

玩得开心!

PS:您也可以尝试使用 d:DesignData .
这种方法也有效。我觉得它没有那么简单。
最后,这取决于你如何去做。
无论哪种方式,都不要错过 DeisgnTime 数据!

关于windows-8 - 如何在 WinRT XAML 中拥有 DesignTime 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10965853/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com