gpt4 book ai didi

c# - 如果类型为 WinRT 中的 ImageSource,则绑定(bind)到 UserControl 属性时出错

转载 作者:行者123 更新时间:2023-11-30 18:39:44 25 4
gpt4 key购买 nike

使用 Windows 8 开发者预览版。

我有一个具有如下属性之一的对象:

    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));
}

这在像这样的 ObservableCollection 中使用:

        var test = new ObservableCollection<object>();

ButtonItem item = new ButtonItem();
item.SetImage(this.basUri, "Data/Images/test.png");

test.png 作为内容包含在内。

此集合用于设置 Grid 的 ItemsSource,如下所示:

ItemGridView.ItemsSource = test;

这个网格有一个数据模板:

        <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>

这个 MyButton 是一个用户控件,它的图像属性是一个依赖属性,如下所示:

    public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register("Image", "ImageSource", typeof(VSButton).FullName, null);

public ImageSource Image
{
get { return (ImageSource)GetValue(ImageSourceProperty); }
set
{
SetValue(ImageSourceProperty, value);
}
}

现在当我运行它时我得到一个异常:

An exception of type 'System.InvalidCastException' occurred in Test.exe but was not handled in user code

Additional information: Unable to cast COM object of type 'Windows.UI.Xaml.Data.Binding' to class type 'Windows.UI.Xaml.Media.ImageSource'

现在...当我将 usercontrol 上的属性转换为字符串类型(并绑定(bind)到字符串)时,一切都按预期工作,所以我一定是做错了什么......什么?

最佳答案

ObservableCollection 在 WinRT 中不能正常工作(至少现在不能),请改用 ObservableVector。有一个 sample提供 IObservableVector 实现。

关于c# - 如果类型为 WinRT 中的 ImageSource,则绑定(bind)到 UserControl 属性时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9273594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com