【问题标题】:WP7 DesignData with custom Object带有自定义对象的 WP7 DesignData
【发布时间】:2012-05-03 13:53:46
【问题描述】:

我正在为 Windows Phone 7.0 开发一个应用程序,并且我正在尝试添加一个示例设计数据文件,以便在我使用 Visual Studio 时获得一些图形。 String 或 int 类型可以正常工作,但自定义对象不行。

我解释一下:

我有一个这样的类:(代表我的自定义对象)

public class CustomObject
{
    public string Title { get; set; }
} 

这是我的视图模型:

public class MainViewModel
{
    public MainViewModel()
    {
        this.Custom= new CustomObject();
        this.Custom.Title = "Hey, i'm the Runtime Custom";
    }

    public CustomObject Custom{ get; set; }

    public string TestProperty { get; set; }
}

我已经创建了一个这样的示例数据文件:

<local:MainViewModel 
    xmlns:local="clr-namespace:Project.ViewModels"
    TestProperty ="I'm the Design testProperty and I work">

    <local:MainViewModel.Custom Title="Hey, i'm the Design Custom, and I don't work" />

</local:MainViewModel>

在我的应用程序的主页中,我在电话标签中添加了这个:

d:DataContext="{d:DesignData SampleData.xaml}"

好吧,当我使用这样的 TestProperty 变量 (Text="{Binding TestProperty}") 进行测试时,它可以正常工作,但是当我尝试使用这样的对象 (Text="{Binding Custom.Title}") 时,它就不起作用了...

我找到的所有资源都不能谈论自定义对象。

有人知道吗?

坦克。

PS:我尝试添加 DesignData 标签(最能描述我的问题)但我不允许:(

编辑: 没有编译,Visual Studio 不显示任何内容,当我编译时,它显示 Hey, i'm the Runtime Custom... 它绕过了我的示例数据文件,但仅针对此自定义对象。

【问题讨论】:

    标签: windows-phone-7 xaml data-binding


    【解决方案1】:

    试试这个:

    <local:MainViewModel 
        xmlns:local="clr-namespace:Project.ViewModels"
        TestProperty ="I'm the Design testProperty and I work"
        xmlns:local2="clr-namespace:Project.NamespaceDefinitionOfTheCustomClass">
    
        <local:MainViewModel.Custom>
            <local2:Custom Title="Hey, i'm the Design Custom, and I don't work" />
        </local:MainViewModel.Custom>
    
    </local:MainViewModel>
    

    如果您的“Custom”类未在“MainViewModel”类的同一命名空间中定义,则像我一样添加命名空间声明,否则只需删除它并将 local2 范围重命名为 local;

    它对我有用。希望这对你有用。

    【讨论】:

      【解决方案2】:

      试试稍微简单一点的:

      <local:MainViewModel 
          xmlns:local="clr-namespace:Project.ViewModels"
          TestProperty ="I'm the Design testProperty and I work">
      
          <local:Custom Title="Hey, i'm the Design Custom, and I don't work" />
      </local:MainViewModel>
      

      【讨论】:

      • 不幸的是它不起作用......当我尝试时,Visual Studio 在local:Custom 下划线并说:找不到类型本地:自定义。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-22
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      • 2014-11-24
      • 2016-04-23
      相关资源
      最近更新 更多