【问题标题】:Why does XAML image source property contain "/MyApp;component"?为什么 XAML 图像源属性包含“/MyApp;component”?
【发布时间】:2011-04-21 12:54:06
【问题描述】:

我正在开发一个 Windows Phone 应用程序。

我正在使用图像,当我使用“属性”面板选择图片时,我得到以下 XAML:

<Image x:Name="GameImage" Margin="8" Source="/MyApp;component/Assets/Icons/GameImage.png"/>

为什么我收到“/MyApp;component/...”? (有没有更好的办法?)

如果我尝试做Image.Source="Assets/Icons/GameImage.png" 为什么它不起作用?

【问题讨论】:

    标签: image xaml windows-phone-7


    【解决方案1】:

    这是因为您的图像将其构建操作设置为 Resource(这是默认设置)。如果您将其切换为 Content,您可以像这样在 XAML 中设置源代码:

    <Image x:Name="GameImage" Margin="8" Source="/Assets/Icons/GameImage.png"/>
    

    要在代码中设置它,您可以这样做:

    BitmapImage tn = new BitmapImage();
    tn.SetSource(Application.GetResourceStream(new Uri(@"Assets/Icons/GameImage.png", UriKind.Relative)).Stream);
    Image.Source = tn;
    

    出于性能原因,您应该使用内容。更多详情请看这篇文章:http://www.windowsphonegeek.com/tips/wp7-working-with-images-content-vs-resource-build-action

    【讨论】:

      【解决方案2】:

      别忘了补充:

      using System.Windows.Media.Imaging;
      
      BitmapImage tn = new BitmapImage();
      tn.SetSource(Application.GetResourceStream(new Uri(@"Assets/Icons/GameImage.png", UriKind.Relative)).Stream);
      Image.Source = tn;
      

      【讨论】:

        【解决方案3】:

        你可以使用:

        BitmapImage obj = new BitmapImage();
        obj.UriSource = new Uri(mera_image.BaseUri,file.Path);
        

        【讨论】:

        • FrameworkElement.BaseUri 仅适用于新的 Windows Phone 8.1 和 Windows 应用商店应用;但是,此请求是关于 Windows Phone 7 应用程序的。
        【解决方案4】:

        任何标记为嵌入资源的内容都是从程序集中加载的。因此,使用站点需要知道指定资源嵌入的程序集。在您的情况下,这是MyApp

        【讨论】:

          猜你喜欢
          • 2015-03-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-07-23
          • 2018-02-26
          • 2021-02-05
          相关资源
          最近更新 更多