代码之家  ›  专栏  ›  技术社区  ›  Madeleine

未设置Silverlight 4用户控件中的图像

  •  0
  • Madeleine  · 技术社区  · 14 年前

    我有一个Silverlight用户控件,其中包含一个图像控件。此图像需要由包含上述控件的另一页通过绑定项列表的数据成员设置。当前,当URL发送到用户控件时,不会设置图像。实际上,除了 SetValue(ImageSourceProperty, value); 好像被击中了。

    用户控件中的XAML如下所示:

    <Grid x:Name="LayoutRoot" Background="Black">
        <Image Name="ContentImage"/>
    </Grid>
    

    在后面的代码中,我将DependencyObject设置为接收图像的URL并设置图像的数据源:

            public static DependencyProperty ImageSourceProperty =
        DependencyProperty.Register("ImageSource", typeof(String), typeof(MediaItemControl), null);
    
    public String ImageSource
        {
            get { return (String)GetValue(ImageSourceProperty); }
            set
            {
                SetValue(ImageSourceProperty, value);
                Dispatcher.BeginInvoke(() => this.ContentImage.Source = new BitmapImage(new Uri(value, UriKind.RelativeOrAbsolute)));
            }
        }
    

    xaml在itemsSource的itemsTemplate中保存对此用户控件的引用,该itemsSource在代码中设置为一个对象列表,该列表具有一个名为imageurl的属性。此数据源在代码隐藏中设置正确。这是主页的XAML:

    <UserControl x:Class="ExampleSilverlightApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:medc="clr-namespace:ExampleSilverlightApp.Controls" mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel Name="ItemsControlStackPanel">
            <ItemsControl Name="ContentItemControl" Visibility="Visible">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"  />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid Width="126" HorizontalAlignment="Center">
                            <medc:MediaItemControl x:Name="CustomMediaItemControl"   
                                                   ImageSource="{Binding ImageURL}"
                                                   >
                            </medc:MediaItemControl>
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </StackPanel>
    </Grid>
    

    我的问题是,在用户控件上没有设置图像源。依赖对象的setter似乎没有被击中。如果我在控件上放置了一个按钮,当按下该按钮时,将设置ImageSource属性,然后单击该设置并显示图像。有趣的是,ImageSource值实际上已正确设置,但在使用按钮设置之前,图像的数据源为空。

    有人知道为什么会这样吗?

    1 回复  |  直到 14 年前
        1
  •  1
  •   Peter    14 年前

    尝试在依赖项属性的PropertyChangedCallback委托中设置ContentImage.Source-如下所示:

    public static DependencyProperty ImageSourceProperty = 
    DependencyProperty.Register("ImageSource", typeof(String), typeof(MediaItemControl), 
        new PropertyMetadata(delegate(DependencyObject sender, DependencyPropertyChangedEventArgs args)
                {
                    (sender as MediaItemControl).ContentImage.Source = new BitmapImage(new Uri((string)args.NewValue, UriKind.RelativeOrAbsolute));
                }));
    

    当您进行数据绑定时,后台的数据绑定引擎调用DependencyObject.setValue(ImageSourceProperty,NewValue),并且不引用实际属性(即它不调用MediaItemControl.ImageSource=NewValue),因此不会调用setter中的逻辑。

    嗯!