2011-07-15 82 views
0

好的,我正在创建的图像列表(使用列表框),左侧是缩略图,右侧是图像标题。我的XAML设置是这样的:将XAML DataTemplate中的图像源绑定到Silverlight 4中的URI

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

有两个断点转换和ConvertBack方法。 ConvertBack永远不会被触发(所以没有例外等),并且在Convert方法中,Thumb正确地返回(字符串输入是由于某些测试原因而留下的,并且目前还没有使用,它不会被触发),并且Thumb扩展方法是this :

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是正确的,根本没有错误,但没有图像显示。

有什么建议吗?

+0

您是否尝试过使用小提琴手,以验证是否Silverlight是实拍的图像,如果真的返回的图像的请求显示? – MerickOWA

+0

试过了,没有,没有请求。我可以看到来自同一应用程序的其他请求(服务调用),它们工作,我可以在提琴手中看到它们,但没有图像请求。很奇怪。 –

+0

Hrm奇怪,也许尝试订阅图像上的ImageFailed事件,看看解析Uri时是否有一些问题阻止下载开始。 – MerickOWA

回答

1

好的,问题是关于SL中的安全限制。它在本地运行,并拨打电话http://localhost ...由于安全限制(至少对于图像)失败。我读过如果我制作了一个测试页面,从本地服务器启动等,然后运行它,错误将消失,但是我没有采取这种解决方法,而是检查了需要提升的信任,并且它突然开始工作。所以问题就解决了。

+0

也许那么另一个解决方案是使用相对URL如果可能,而不是绝对的? – MerickOWA

+0

好吧,我不认为这会产生差异。最后,要调用的URL是相同的URL。我也有clientaccesspolicy.xml,但那不起作用,所以还有其他的东西。由于其他一些原因,绝对的uri是非常适合我的项目而不是相对的。 –

1

您可能必须在转换器中明确加载图像。 MSDN page for BitmapImage显示以下代码片段:

// Create the image element. 
Image simpleImage = new Image();  
simpleImage.Width = 200; 
simpleImage.Margin = new Thickness(5); 

// Create source. 
BitmapImage bi = new BitmapImage(); 
// BitmapImage.UriSource must be in a BeginInit/EndInit block. 
bi.BeginInit(); 
bi.UriSource = new Uri(@"/sampleImages/cherries_larger.jpg",UriKind.RelativeOrAbsolute); 
bi.EndInit(); 
// Set the image source. 


simpleImage.Source = bi; 
+0

这可能会起作用(尽管此代码示例是针对WPF而不是SL,因为SL中没有Begin/End init),但我试图在纯XAML中实现它(至少在XAML中是“声明式方式”可能) –

0

进出口使用本作完全一样的问题,但使用Silverlight 5 IM向eleated信任,不知道它的事项

<DataTemplate x:Key="DataTemplate1"> 
     <Grid> 
      <Image Margin="0" Width="25" Height="25" Source="{Binding EventType, StringFormat=/Icons/EventType\{0:d\}.png}"/> 
     </Grid> 
    </DataTemplate> 

而且效果很好。其DataGrid的数据模板,其中此EventType是枚举类型。

这是小提琴手后来

http://www.localdomain.loc/ClientBin/Icons/EventType1.png 
相关问题