【发布时间】:2011-10-06 06:22:39
【问题描述】:
好的,我正在尝试创建一个图像列表(使用列表框),左侧是缩略图,右侧是图像标题。我的 XAML 是这样设置的:
<ListBox HorizontalAlignment="Left" Margin="6,6,0,6" Name="CurrentPhotos" Width="184" SelectionChanged="CurrentPhotos_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Converter={StaticResource FilePathConverter}}" />
<sdk:Label Content="{Binding Title}"></sdk:Label>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我已经在 App.xaml 中定义了 FilePathConverter 键并且设置了代码:
public class FilePathConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (targetType == typeof(string))
{
return (value as PhotoSummary).FullThumbPath();
}
else
{
return (value as PhotoSummary).Thumb();
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Convert 和 ConvertBack 方法中都有断点。 ConvertBack 永远不会被触发(因此没有异常等),并且在 Convert 方法中,Thumb 正确返回(由于某些测试原因留下了字符串输入,并且当前未使用。无论如何它都不会触发),而 Thumb 扩展方法是这样的:
public static object Thumb(this PhotoSummary ps)
{
Uri uri = new Uri("http://" + Settings.Host + "/Content/Thumbs/" + ps.Uploaded.Year + "/" + ps.Uploaded.Month + "/" + ps.ID + ".jpg", UriKind.Absolute);
return new BitmapImage(uri);
}
这个被调用了,并且 Uri 被正确构建(经过多次测试)。但是,当我运行该应用程序时,列表仅包含照片的标题,并且没有图像。所有图像都很小(它们只是拇指),本地文件,因此它们需要立即加载,因此也不是加载问题。但好像那里没有图像标签。它只显示照片的标签。转换器正在工作,Uri 正确,完全没有错误,但没有显示图像。
有什么建议吗?
【问题讨论】:
-
您是否尝试过使用 fiddler 来验证 silverlight 是否真的在请求图像以及图像是否真的被返回?
-
试过了,不,没有请求。我可以看到来自同一个应用程序(服务调用)的其他请求,它们可以工作,我可以在 Fiddler 中看到它们,但没有图像请求。很奇怪。
-
奇怪,也许可以尝试订阅 Image 上的 ImageFailed 事件,看看是否存在解析阻止下载开始的 Uri 的问题。
-
另一个想法,检查图像 ActualWidth/ActualHeight 并确保它们不为零,也许尝试将 Width/Height 设置为一个值以查看图像是否没有下载,因为它没有空间在堆栈面板中显示图像。
-
我会使用 Silverlight Spy 来查看可视化树中发生的情况,并确保 ImageSource 在图像上得到正确设置。根据 MerickOWA 的评论,还要检查图像的尺寸。
标签: c# .net silverlight xaml binding