如何在 WinRT XAML 中拥有 DesignTime 数据? [英] How to have DesignTime data in WinRT XAML?

查看:23
本文介绍了如何在 WinRT XAML 中拥有 DesignTime 数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I get DesignTime data in WinRT XAML so the designer shows sample data?

推荐答案

很简单.

像这样创建一个模型:

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

像这样创建一个基本的 ViewModel:

Create a base ViewModel like this:

public class BaseViewModel 
{
    public ObservableCollection<Fruit> Fruits { get; set; }
}

像这样创建一个真正的 ViewModel:

Create a real ViewModel like this:

public class RealViewModel : BaseViewModel
{
    public RealViewModel()
    {
        if (!Windows.ApplicationModel.DesignMode.DesignModeEnabled)
            LoadData();
    }

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

像这样创建一个假数据视图模型:

Create a fake-data ViewModel like this:

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 中执行此操作:

Do this in your XAML:

<Page.DataContext>
    <local:RealViewModel />
</Page.DataContext>

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

玩得开心!

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

PS: you can also attempt to use d:DesignData. That approach also works. I feel it is not as straight forward. In the end, it's up to you how to do it. Either way, don't miss out on DeisgnTime data!

这篇关于如何在 WinRT XAML 中拥有 DesignTime 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆