Windows Phone 8.1:在运行时加载一个BitmapImage
Windows Phone 8.1: loading a BitmapImage at runtime
我对 Windows Phone 8.1 的 C# + XAML 环境编程经验不多。最近我一直在开发一个在 运行 时间加载图像和音频资源的应用程序。虽然我没有音频问题,但我无法加载图像。我已经尝试了很多建议的解决方案但没有成功。
基本上在我的 MainPage.xaml 文件中有这一行:
<Image Source="{Binding Portrait}"/>
在我写的相关 C# 代码中:
using Windows.UI.Xaml.Media.Imaging;
...
Portrait.Source = new BitmapImage(new Uri("ms-appx:///Assets/Portraits/path/to specific/portrait.jpg", UriKind.Absolute));
当我 运行 应用程序时,抛出了 NullReferenceException,我几乎可以肯定它是抛出的,因为 Source 设置为 null
。
我检查了路径,我完全确定它是正确的,而且我将资源构建操作设置为内容(我也尝试使用嵌入式资源)。
我保持上下文简单,但请告诉我是否需要更多详细信息。
表达式
Portrait.Source = new BitmapImage(...);
表示您已将 Portrait
属性 声明为 Image
,如
public Image Portrait { get; set; }
除了 属性 值未初始化并且 Portrait
在进行赋值时为 null
之外, 属性 实际上应该是类型 ImageSource
(或 BitmapSource
)因为 ImageSource
是您在 XAML:
中绑定的 Image.Source
属性 的类型
public ImageSource Portrait { get; set; }
你应该这样分配它:
Portrait = new BitmapImage(...);
我对 Windows Phone 8.1 的 C# + XAML 环境编程经验不多。最近我一直在开发一个在 运行 时间加载图像和音频资源的应用程序。虽然我没有音频问题,但我无法加载图像。我已经尝试了很多建议的解决方案但没有成功。
基本上在我的 MainPage.xaml 文件中有这一行:
<Image Source="{Binding Portrait}"/>
在我写的相关 C# 代码中:
using Windows.UI.Xaml.Media.Imaging;
...
Portrait.Source = new BitmapImage(new Uri("ms-appx:///Assets/Portraits/path/to specific/portrait.jpg", UriKind.Absolute));
当我 运行 应用程序时,抛出了 NullReferenceException,我几乎可以肯定它是抛出的,因为 Source 设置为 null
。
我检查了路径,我完全确定它是正确的,而且我将资源构建操作设置为内容(我也尝试使用嵌入式资源)。
我保持上下文简单,但请告诉我是否需要更多详细信息。
表达式
Portrait.Source = new BitmapImage(...);
表示您已将 Portrait
属性 声明为 Image
,如
public Image Portrait { get; set; }
除了 属性 值未初始化并且 Portrait
在进行赋值时为 null
之外, 属性 实际上应该是类型 ImageSource
(或 BitmapSource
)因为 ImageSource
是您在 XAML:
Image.Source
属性 的类型
public ImageSource Portrait { get; set; }
你应该这样分配它:
Portrait = new BitmapImage(...);