C# 如果WinRT中的类型为ImageSource,则绑定到UserControl属性时出错
使用Windows8开发人员预览 我有一个对象具有如下属性之一:C# 如果WinRT中的类型为ImageSource,则绑定到UserControl属性时出错,c#,data-binding,windows-runtime,C#,Data Binding,Windows Runtime,使用Windows8开发人员预览 我有一个对象具有如下属性之一: private ImageSource _Image = null; public ImageSource Image { get { return this._Image; } set { if (this._Image != value) {
private ImageSource _Image = null;
public ImageSource Image
{
get
{
return this._Image;
}
set
{
if (this._Image != value)
{
this._Image = value;
this.OnPropertyChanged("Image");
}
}
}
public void SetImage(Uri baseUri, String path)
{
Image = new BitmapImage(new Uri(baseUri, path));
}
var test = new ObservableCollection<object>();
ButtonItem item = new ButtonItem();
item.SetImage(this.basUri, "Data/Images/test.png");
这在如下的ObservableCollection中使用:
private ImageSource _Image = null;
public ImageSource Image
{
get
{
return this._Image;
}
set
{
if (this._Image != value)
{
this._Image = value;
this.OnPropertyChanged("Image");
}
}
}
public void SetImage(Uri baseUri, String path)
{
Image = new BitmapImage(new Uri(baseUri, path));
}
var test = new ObservableCollection<object>();
ButtonItem item = new ButtonItem();
item.SetImage(this.basUri, "Data/Images/test.png");
这个网格有一个数据模板:
<DataTemplate x:Key="testtemp">
<Grid HorizontalAlignment="Left" Background="White">
<StackPanel Orientation="Horizontal" Margin="10,10,0,0">
<my:MyButton Image="{Binding Image}"></my:MyButton>
</StackPanel>
</Grid>
</DataTemplate>
现在,当我运行此命令时,会出现一个异常:
Test.exe中发生“System.InvalidCastException”类型的异常,但未在用户代码中处理
其他信息:无法将类型为“Windows.UI.Xaml.Data.Binding”的COM对象强制转换为类型为“Windows.UI.Xaml.Media.ImageSource”的类
现在。。。当我将usercontrol上的属性转换为字符串类型(并绑定到字符串)时,一切都按预期进行,因此我一定是做错了什么。。什么?ObservaleCollection在WinRT中无法正常工作(至少目前还不能),请改用Observalevector。有一个提供IObservableVector实现的应用程序。我知道,我也知道。。为了简单起见,我在本期中将其更改为ObservableCollection。不起作用哦,那就不是问题了。看了你的代码,我觉得很好。注册时尝试初始化PropertyMetadata。我将尝试运行此代码。设置PropertyMetaData也没有帮助。