【发布时间】:2011-11-01 22:02:39
【问题描述】:
我已经成功地将存储在独立存储中的图像绑定到 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 方法的任何想法或提示?
谢谢!
这是我尝试过的:
-
实现 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);
}
-
使用 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”没有被填充。
感谢您的任何想法...
附:下面是我如何定义 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 属性。
谢谢大家。我希望这会对那里的人有所帮助。
【问题讨论】:
-
我很困惑。您是说完全相同的 XAML 在 ListBox 之外可以正常工作,但不能在其中一个?
-
嗨 Gabe,我是说我在同一个项目中有另一个 XAML,它使用与上面显示的相同的“绑定语法”,并且一切正常。唯一的区别是该特定 XAML 使用列表框中的图像元素。
-
如果您愿意,还提供转换器代码吗?里面可能有些东西可能会有所帮助
-
布拉科门:嗨;我添加了转换的签名只是为了确保它是正确的。我没有添加整个实现,因为我什至没有到达 Convert 方法的第一行,所以所有其他细节都毫无意义;我的 Convert 方法根本没有被调用,除非我向图像元素添加“DataContext”。请看我对问题的更新,因为这个解决方案仍然对我不起作用。谢谢。
标签: c# silverlight xaml windows-phone-7