为什么我的外部图像无法加载到我的 Silverlight 页面中?

发布于 2024-10-08 17:43:32 字数 798 浏览 0 评论 0原文

为了使这一过程尽可能简单,我创建了一个新的默认 Silverlight 4 应用程序,并向测试网站添加了一个图像文件夹,该文件夹是在生成应用程序时自动创建的。我将同一张图像(我们将其命名为“imageName.png”)放在 ClientBin 文件夹和 Images 文件夹中。现在,当我像这样使用 XAML 在 ClientBin 中使用图像时,一切正常:

<Grid x:Name="LayoutRoot" Background="White">
    <StackPanel>
        <Image Source="imageName.png" Width="16" Height="16"/>
        <TextBox Text="Hello" />
    </StackPanel>
</Grid>

但是当我尝试访问 Images 文件夹中的图像时,图像不会加载,如下所示:

<Grid x:Name="LayoutRoot" Background="White">
    <StackPanel>
        <Image Source="../Images/imageName.png" Width="16" Height="16"/>
        <TextBox Text="Hello" />
    </StackPanel>
</Grid>

我没有收到任何错误,但图像只是不在那里。谁能告诉我发生了什么事吗?我所能找到的一切似乎都表明这应该有效。

To make this as simple as possible, I created a new default Silverlight 4 Application and added an Images folder to the test web site that is automatically created when the application is generated. I put the same image, let's call it "imageName.png" in the ClientBin folder and the Images folder. Now, everything works fine when I just use the image in ClientBin using XAML like this:

<Grid x:Name="LayoutRoot" Background="White">
    <StackPanel>
        <Image Source="imageName.png" Width="16" Height="16"/>
        <TextBox Text="Hello" />
    </StackPanel>
</Grid>

But the image doesn't load when I try to access the image in the Images folder like this:

<Grid x:Name="LayoutRoot" Background="White">
    <StackPanel>
        <Image Source="../Images/imageName.png" Width="16" Height="16"/>
        <TextBox Text="Hello" />
    </StackPanel>
</Grid>

I don't get any errors, but the image just isn't there. Can anyone tell me what's going on? Everything I've been able to find seems to suggest that this should work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感受沵的脚步 2024-10-15 17:43:32

这里的问题是,包含 Xap 的文件夹和 Xap 本身的顶部内容被认为位于路径的根目录。因此,路径“/”映射到 Xap 中的顶部内容以及 Xap 所在的文件夹(通常是 ClientBin)。任何尝试使用超越根路径的父路径都将导致 Silverlight 网络错误。

Xap 所在文件夹之外的任何内容只能使用绝对 Uri 进行访问。

然后需要做的是将包含父路径的相对地址转换为绝对地址,然后将其传递给 Image.Source 属性。我们最好避免将基址绝对地址硬编码到源代码中。

我们可以使用WebClient对象的BaseAddress属性来获取Xap当前获取的绝对Url。由此,我们可以通过将其与相对地址(甚至包含父路径的相对地址)组合来组成所需的绝对地址。

值转换器可以使 Xaml 中的此操作变得更容易:-

public class WebRelativeConverter : IValueConverter
{
    public Uri Base { get; set; }

    public WebRelativeConverter()
    {
        Base = new Uri((new WebClient()).BaseAddress, UriKind.Absolute);
    }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Uri result = null;

        if (value is Uri)
        {
            result = new Uri(Base, (Uri)value);
        }
        else if (value != null)
        {
            result = new Uri(Base, value.ToString());
        }
        return result;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

现在 Xaml 中的固定值将如下所示:-

<Grid x:Name="LayoutRoot" Background="White">
    <Grid.Resources>
         <local:WebRelativeConverter x:Key="wrc" />
    </Grid.Resources>
    <StackPanel>
        <Image DataContext="../Images/imageName.png" Source="{Binding Converter={StaticResource wrc}}" Width="16" Height="16"/>
        <TextBox Text="Hello" />
    </StackPanel>
</Grid>

现在,如果您实际上将源绑定到某个对象的 Uri 或 String 属性,则只需添加转换器,它应该工作。

The problem here is that the folder containing the Xap and the top content in the Xap itself is considered to be at the root of the path. Hence the path "/" maps to the top content in Xap and the folder where the Xap is sited (usually ClientBin). Any attempt to use a parent path past what is considered the root will result in a Silverlight Network Error.

Any content outside of the folder that the Xap is found in can only be accessed using an absolute Uri.

What need to do then is convert the relative address containing a parent path to an absolute address before passing it to the Image.Source property. Preferably we would want to avoid hardcoding the base absolute address into the source code.

We can get the current acquire the absolute Url for the Xap using the BaseAddress property of the WebClient object. From that we can compose the desired absolute address by combining it with the relative address even one that contains a parent path.

A value converter can make this easier in Xaml:-

public class WebRelativeConverter : IValueConverter
{
    public Uri Base { get; set; }

    public WebRelativeConverter()
    {
        Base = new Uri((new WebClient()).BaseAddress, UriKind.Absolute);
    }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Uri result = null;

        if (value is Uri)
        {
            result = new Uri(Base, (Uri)value);
        }
        else if (value != null)
        {
            result = new Uri(Base, value.ToString());
        }
        return result;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Now a fixed value in Xaml would look like this:-

<Grid x:Name="LayoutRoot" Background="White">
    <Grid.Resources>
         <local:WebRelativeConverter x:Key="wrc" />
    </Grid.Resources>
    <StackPanel>
        <Image DataContext="../Images/imageName.png" Source="{Binding Converter={StaticResource wrc}}" Width="16" Height="16"/>
        <TextBox Text="Hello" />
    </StackPanel>
</Grid>

Now if you actually were binding the source to a Uri or String property of some object you only need to add the converter and it should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文