Xaml 设计器中未显示将Image.Source绑定到属性的操作
我想将WinRT XAML中的映像的源绑定到名为ServiceEnvironment的视图模型的ImageSource属性,该视图模型指向项目的/Assets目录中的映像。以下是视图模型:Xaml 设计器中未显示将Image.Source绑定到属性的操作,xaml,data-binding,windows-runtime,windows-phone-8.1,winrt-xaml,Xaml,Data Binding,Windows Runtime,Windows Phone 8.1,Winrt Xaml,我想将WinRT XAML中的映像的源绑定到名为ServiceEnvironment的视图模型的ImageSource属性,该视图模型指向项目的/Assets目录中的映像。以下是视图模型: public class ServiceEnvoy { public ServiceEnvoy(string name, ImageSource source) { this.Name = name; this.ImageSource = source;
public class ServiceEnvoy
{
public ServiceEnvoy(string name, ImageSource source)
{
this.Name = name;
this.ImageSource = source;
}
public string Name { get; private set; }
public ImageSource ImageSource { get; private set; }
}
<ListView ItemsSource="{Binding Services}"
...>
<ListView.ItemTemplate>
<DataTemplate>
<Button>
<Button.Content>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/> <!--Works fine-->
<Image Source="{Binding ImageSource}"/> <!--Does not-->
</StackPanel>
</Button.Content>
</Button>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
下面是我要公开视图模型的ListView的XAML:
public class ServiceEnvoy
{
public ServiceEnvoy(string name, ImageSource source)
{
this.Name = name;
this.ImageSource = source;
}
public string Name { get; private set; }
public ImageSource ImageSource { get; private set; }
}
<ListView ItemsSource="{Binding Services}"
...>
<ListView.ItemTemplate>
<DataTemplate>
<Button>
<Button.Content>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/> <!--Works fine-->
<Image Source="{Binding ImageSource}"/> <!--Does not-->
</StackPanel>
</Button.Content>
</Button>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在Pastebin上找到的ServiceEnvoyExtractor类通过附加ms来提取视图模型-appx:///Assets/ 使用该图像创建Uri/BitmapImage。我不确定这是否是问题所在,但我尝试将JSON更改为Image:/Assets/onedrive.png或ms-appx:///Assets/onedrive.png 和重建,但设计师仍然没有显示图像。但是,当我在手机上运行它时,它工作正常
我的d:DataContext是:d:DataContext={Binding Source={d:DesignData Source=/Data/ServiceRecords.json,Type=Data:DesignableServiceEnvoyCollection}}好吧,这相当简单 在几个小时的时间里,我对ImageSource的类型感到沮丧,对它进行了重命名,测试了其他控件,并摆弄了JSON,之后我意识到,虽然它的关键是图像,但我绑定到了ImageSource 将其目录更改为Assets/onedrive.png解决了这个问题
编辑:XAML似乎还抛出了一个关于ImageSource属性如何无法序列化的错误;将属性更改为string类型修复了该问题。尝试使用公共BitmapImage ImageSource{get;private set;}而不是公共ImageSource ImageSource{get;private set;}