繁体   English   中英

图像绑定,Silverlight,C#,WP7

[英]Image Binding, Silverlight, C#, WP7

我已经成功地将存储在独立存储中的图像绑定到XAML中定义的列表框中的“图像”元素,而没有任何问题。

我要做的是使用一个属性,该属性将图像的名称和位置存储在隔离的存储区中,并使用IValueConverter实际打开文件并获取其流。

令我惊讶的是,我无法通过更简单的设置在新的XAML中做到这一点。 我所拥有的是:

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Grid.Resources>
            <c:BinaryToImageSRCConverter x:Key="imageConverter" />
        </Grid.Resources>
        <Image Name="TheImage" Source="{Binding PhotoName, Converter={StaticResource imageConverter}}" Width="430" Height="430" VerticalAlignment="Center" Stretch="UniformToFill">
        </Image>
    </Grid>

这就是我在ModelView中所拥有的:

        public string PhotoName { get; set; }
        PhotoName = "Images\\0.jpg";

文件的路径和名称正确,因此这不是“找不到文件”的问题。

我的问题是我的IValueConverter的Convert方法永远不会被调用。

如前所述,我已经在另一个XAML(在同一项目中)中成功使用了此转换器,并且一切正常。 唯一的区别是我绑定到ListBox使用的图像。

关于为什么在这种情况下不调用IValueConverter的Convert方法的任何想法或提示?

谢谢!

这是我尝试过的:

  1. 实现INotifyPropertyChanged

     public class PhotoNameClass : INotifyPropertyChanged { public string PhotoNameValue = string.Empty; public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } public string PhotoName { get { return this.PhotoNameValue; } set { if (value != this.PhotoNameValue) { this.PhotoNameValue = value; NotifyPropertyChanged("PhotoName"); } } } } 

这是我分配给该属性的方式:

    public partial class ShowContent : PhoneApplicationPage
{
    PhotoNameClass photoClass = new PhotoNameClass();
}
   protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
      photoClass.PhotoName = "Images\\0.jpg";
      base.OnNavigatedTo(e);
    }
  1. 使用DependencyProperty

     public partial class ShowContent : PhoneApplicationPage 

    {// PhotoNameClass photoClass = new PhotoNameClass();

     public string PhotoName { get { return (string)GetValue(PhotoNameProperty); } set { SetValue(PhotoNameProperty, value); } } public static readonly DependencyProperty PhotoNameProperty = DependencyProperty.Register("PhotoName", typeof(string), typeof(ShowContent), new PropertyMetadata("")); protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { PhotoName = "Images\\\\0.jpg"; } 

这是XAML的相关部分:

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Grid.Resources>
            <c:BinaryToImageSRCConverter x:Key="imageConverter" />
        </Grid.Resources>
        <Image Name="TheImage" Source="{Binding PhotoName, Converter={StaticResource imageConverter}}" Width="430" Height="430" VerticalAlignment="Center" Stretch="UniformToFill">
        </Image>
    </Grid>

其中,c定义为:

    xmlns:c="clr-namespace:ImageApplication"

谢谢。

更新

我正在使用INotifyPropertyChanged实现公开我的属性。

我已将XAML代码更改为此:

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Grid.Resources>
            <c:PhotoNameClass x:Key="photoClass" />
            <c:BinaryToImageSRCConverter x:Key="imageConverter" />
        </Grid.Resources>
            <Image Source="{Binding PhotoName, Converter={StaticResource imageConverter}}" DataContext="{StaticResource photoClass}" Width="430" Height="430" VerticalAlignment="Center" Stretch="UniformToFill">
            </Image>
    </Grid>
</Grid>

因此,我添加了图像元素的“ DataContext”属性,该属性指定了包装属性的类的名称。 在这种情况下, IValueConverter的Convert方法称为 但是,这是不对的,因为在添加DataContext时,我在XAML中遇到错误:“无法确定调用者的应用程序身份”,当然,进入到Convert方法中,我的“值”字符串为空,因此,“右”“ PhotoName”未填充。

感谢您的任何想法...

PS这是我定义IValueConverter的Convert方法的方式:

namespace ImageApplication
{
  public class BinaryToImageSRCConverter : IValueConverter
  { 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    { // <-- Breakpoint here. Never gets triggered except after I added the "DataContext" attribute to the XAML image element.
      // but in that case, "value" is an empty string.
        if (value != null && value is string)
        {
               ... 

已修复更新

我已经解决了这个问题。 我已将包装我的属性的类名称分配给XAML代码中定义的图像的DataContext属性。

谢谢你们。 我希望这会对外面的人有所帮助。

因为您的属性不会引发PropertyChanged通知,所以它永远不会被调用。 当设置属性值时,您需要使其成为DependencyProperty或实现INotifyPropertyChanged并引发PropertyChanged事件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM