2012-02-14 53 views
3

使用Windows 8 Developer Preview。如果类型为WinRT中的ImageSource,则会绑定到UserControl属性的错误

我有这样的属性之一的对象:

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被包括作为内容。

此集合用于设置一个网格的的ItemsSource,像这样:

ItemGridView.ItemsSource = test; 

而这个网格具有的DataTemplate:

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

现在,当我运行这个我得到一个前ception:

“”类型的异常出现在将Test.exe但在用户代码中没有处理

其他信息:无法转换类型的COM对象“Windows.UI.Xaml。 Data.Binding”类类型‘Windows.UI.Xaml.Media.ImageSource’

现在...当我转换属性的用户控件字符串类型(和绑定为一个字符串),一切正常如预期的那样,所以我必须做一些其他的事情..什么?

回答

0

ObservableCollection在WinRT中无法正常工作(至少尚未),请改用ObservableVector。有一个提供IObservableVector实现的sample

+0

我知道了,我做了..为了简单起见,我将此问题更改为ObservableCollection。不工作 – Flores 2012-02-14 18:44:53

+0

哦,那么这不是问题。通过你的代码看,它看起来很好。尝试在注册时初始化您的PropertyMetadata。我要试着运行这段代码。 – sarvesh 2012-02-14 18:49:57

+0

设置PropertyMetaData也没有帮助。 – Flores 2012-02-14 22:02:15

相关问题