Visual Studio 2015.c#.如何将映像从本地驱动器设置为映像.源代码窗口通用应用程序

本文关键字:映像 设置 驱动器 源代码 窗口 应用程序 2015 Studio Visual | 更新日期: 2023-09-27 18:06:10

我想在c#中使用本地驱动器中的图像设置图像对象,而不将其添加到我的项目中。

映像存储在我的本地驱动器上:

C:'Users'Nero'Documents'Visual Studio 2015'Projects'App1'Image'Landing Image'Landing image2.png

我试着用下面的代码加载它:

Uri imageUri = new Uri("C:/Users/Nero/Documents/Visual Studio 2015/Projects/App1/Image/Landing Image/Landing image2.png");
landingImage.Source = new BitmapImage(imageUri);

在调试模式下没有错误,但当我运行模拟器时没有显示图像。

Visual Studio 2015.c#.如何将映像从本地驱动器设置为映像.源代码窗口通用应用程序

当您构建和部署应用程序时,映像不再位于该位置(C:'Users'Nero'Documents'Visual Studio 2015'Projects'App1'Image'Landing Image'Landing image2.png)。但是它被封装在部署到模拟器的appx文件中。

要访问项目(以及appx)中的文件,最简单的方法是:

Uri imageUri = new Uri("ms-appx:///Image/Landing Image/Landing image2.png");
landingImage.Source = new BitmapImage(imageUri);

我从未开发过通用应用程序,但我发现了以下链接:

https://msdn.microsoft.com/en-us/library/windows/apps/hh967755.aspx?cs-save-lang=1& cs-lang = csharp # code-snippet-2

它解释了文件访问,可能会对您有所帮助。

Windows.Storage.StorageFolder installedLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync("ms-appx:///file.txt");