在空白应用程序中显示图像通用 Windows - C++/CX

问题描述

项目类型:

enter image description here

我的代码

Image^ img = ref new Image();
img->Width = piece;
img->Height = piece;
Canvas::SetLeft(img,x * field + field / 2 - piece / 2);
Canvas::SetTop(img,y * field + field / 2 - piece / 2);
img->Source = ref new BitmapImage(ref new Uri("E:\\C++\\Assets\\Ball.png"));
csmain->Children->Append(img);

它不会显示存储在路径中的图像。图像甚至不可见。我没有线索。 我是 C++ 新手,但我非常了解 C#。

提前致谢:)

解决方法

它不会显示存储在路径中的图像。图像甚至不可见。

UWP 平台不能直接显示带路径的图片。 (img->Source = ref new BitmapImage(ref new Uri("E:\\C++\\\\Assets\\Ball.png"));) 如果你确实想显示以上路径中的图片文件,请使用filepicker打开它,并将其转换为BitmapImage,或者使用windows storage api访问该文件。前提是你需要开启broadFileSystemAccess能力。如需更多信息,请参阅此document