简体   繁体   English

将 XAML DataTemplate 中的图像源绑定到 Silverlight 4 中的 URI

[英]Binding Image's Source in XAML DataTemplate to a URI in Silverlight 4

Okay, what I'm trying to create is a list of images (using a listbox) with a thumbnail on the left, and image title on the right.好的,我要创建的是一个图像列表(使用列表框),左侧是缩略图,右侧是图像标题。 My XAML is set up this way:我的 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>

I've got the FilePathConverter key defined in App.xaml and the code is set up:我在 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();
    }
}

There are breakpoints in both Convert and ConvertBack methods. Convert 和 ConvertBack 方法中都有断点。 ConvertBack never gets fired (so there's no exception etc.), and in Convert method, the Thumb returns correctly (string input is left for some test reasons, and not currently used. it's not fired anyways), and the Thumb extension method is this: 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);
}

This one gets called, and the Uri is built up correctly (tested several times).这个被调用,并且 Uri 被正确构建(测试了几次)。 However, when I run the app, the list only contains the Title of the photo, and no image is there.但是,当我运行该应用程序时,列表仅包含照片的标题,并且没有图像。 All the images are small (they are just thumbs), local files, so they need to load up instantly so it's not a loading issue either.所有图像都很小(它们只是拇指),本地文件,因此它们需要立即加载,因此也不是加载问题。 But it's as if there's no Image tag there.但好像那里没有图像标签。 It just displays the labels of the photos.它只显示照片的标签。 Converter is working, Uri is correct, there are no errors at all, but no image shows up.转换器正在工作,Uri 是正确的,完全没有错误,但没有显示图像。

Any suggestions?有什么建议么?

You might have to load the image explicitly in your converter.您可能必须在转换器中显式加载图像。 The MSDN page for BitmapImage shows the following code snippet: BitmapImage 的 MSDN 页面显示了以下代码片段:

// 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;

Okay, the problem was about a security restriction in SL.好的,问题是关于 SL 中的安全限制。 It was running locally, and making a call to http://localhost ... was failing due to security restriction (at least, for images).它在本地运行,并调用http://localhost ... 由于安全限制(至少对于图像)而失败。 I've read that if I made a test page, launch from the local server etc. and then run it, the error would go away, but instead of that workaround, I just checked require elevated trust, and it suddenly started to work.我已经读过,如果我创建了一个测试页面,从本地服务器等启动然后运行它,错误将 go 消失,但我没有使用这种解决方法,而是检查了需要提升的信任,它突然开始工作。 So question is solved.所以问题解决了。

Im using this for exactly same problem, But Im using Silverlight 5 with eleated trust, dont know it that matters我用它来解决完全相同的问题,但是我使用 Silverlight 5 并获得了高度信任,不知道这很重要

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

And it works well.而且效果很好。 Its data template for DataGrid, where this EventType is enum of types.它的 DataGrid 数据模板,其中此 EventType 是类型的枚举。

this is what fiddler shows later这是提琴手稍后展示的

http://www.localdomain.loc/ClientBin/Icons/EventType1.png

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

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