Blend 不会为 VM 生成样本数据

发布于 2024-10-02 21:47:07 字数 185 浏览 8 评论 0原文

在 Blend 4 中,我尝试从 VM 类生成示例数据源。该类有一个返回接口的 observablecollection 的属性和另一个带有类的 observablecollection 的属性。生成示例数据源时,Blend 生成类属性的数据,但不生成接口的数据。有办法解决这个问题吗?我的代码绝对需要具有界面,但同时我希望能够看到为设计时生成的示例数据。

In Blend 4, i am trying to generate sample data source from my VM class. The class has a property that returns observablecollection of an interface and another property with observablecollection of a class. When generating sample data source, Blend generates data for the class property but not the interface. Is there a way around this? My code absolutely requires to have the interface, but at the same i want to able to see the sample data generated for design time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悍妇囚夫 2024-10-09 21:47:07

这里的问题是 Blend 不知道要创建什么样的对象作为 IDataInterface 的具体实现。我建议创建两个设计时数据源,一个用于 MyVM,另一个用于具体的 IDataInterface 实现:

namespace SilverlightApplication1
{
    public interface IDataInterface 
    { 
        string Stuff { get; set; } 
    }

    public class PartialViewModel<M> 
    { 
        public M Model { get; private set; } 
    }

    public class ConcreteDataInterface : IDataInterface
    {
        public ConcreteDataInterface()
        {
            this.Stuff = "Concrete Stuff!";
        }

        public string Stuff {get;set;}
    }

    public class MyVM 
    { 
        public PartialViewModel<IDataInterface> Partial 
        { 
            get; 
            private set; 
        } 
    }
}

然后 XAML 将是:

<UserControl x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot"
        d:DataContext="{d:DesignData /SampleData/MyVMSampleData.xaml}">
      <Grid DataContext="{Binding Partial.Model}" 
            d:DataContext="{d:DesignData /SampleData/ConcreteDataInterfaceSampleData.xaml}">
          <TextBlock Text="{Binding Stuff}"/>
      </Grid>
    </Grid>
</UserControl>

The problem here is that Blend doesn't know what kind of object to create as the concrete implementation of IDataInterface. I would suggest creating two design-time data sources, one for the MyVM and one for the concrete IDataInterface implementation:

namespace SilverlightApplication1
{
    public interface IDataInterface 
    { 
        string Stuff { get; set; } 
    }

    public class PartialViewModel<M> 
    { 
        public M Model { get; private set; } 
    }

    public class ConcreteDataInterface : IDataInterface
    {
        public ConcreteDataInterface()
        {
            this.Stuff = "Concrete Stuff!";
        }

        public string Stuff {get;set;}
    }

    public class MyVM 
    { 
        public PartialViewModel<IDataInterface> Partial 
        { 
            get; 
            private set; 
        } 
    }
}

and then the XAML would be:

<UserControl x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot"
        d:DataContext="{d:DesignData /SampleData/MyVMSampleData.xaml}">
      <Grid DataContext="{Binding Partial.Model}" 
            d:DataContext="{d:DesignData /SampleData/ConcreteDataInterfaceSampleData.xaml}">
          <TextBlock Text="{Binding Stuff}"/>
      </Grid>
    </Grid>
</UserControl>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文