我怎么能打开我已经添加到我的Windows Store应用编程项目的文件? [英] How can I open a file I've added to my Windows Store App project programatically?

查看:280
本文介绍了我怎么能打开我已经添加到我的Windows Store应用编程项目的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欲加载的PDF文件响应于螺纹事件。

I want to load a PDF file in response to a Tapped event.

我添加的文件到我的项目(​​添加>现有项),将建设行动,以内容和复制到输出目录为复制,如果新

I added the file to my project (Add > Existing Item), set "Build Action" to "Content" and "Copy to Output Directory" to "Copy if newer"

我想我需要的可能是这样的代码:

I'm thinking the code I need may be something like this:

async Task LoadTutorial()
{
    await Launcher.LaunchUriAsync(new Uri("what should be here to access the output folder?"));
}

如果我是正确的,什么才是我需要传递的URI?否则,这是怎么做到的呢?

If I'm right, what do I need to pass as the Uri? Otherwise, how is this accomplished?

在一个相关的说明,将图像添加到XAML使用建议的方案,我认为这将工作:

On a related note, to add an image to the XAML using the suggested scheme, I thought this would work:

<Image Source="ms-appx:///assets/axXAndSpaceLogo.jpg"></Image>



...但事实并非如此。

...but it doesn't.

尝试这个打开PDF文件(位于项目的根,而不是在一个子文件夹):

Trying this to open the PDF file (which is located in the root of the project, not in a subfolder):

async private void OpenTutorial()
{
    IStorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
    IStorageFile file = await folder.GetFileAsync("ms-appx:///PlatypusTutorial.pdf");
    await Launcher.LaunchFileAsync(file);
}



...导致此运行时异常,扔在第一线之上:

...resulted in this runtime exception, thrown on the first line above:

和本,改编自提供的链接:

And with this, adapted from the link provided:

var uri = new System.Uri("ms-appx:///ClayShannonResume.pdf");
var file = Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
await Launcher.LaunchFileAsync(file);



...我得到的编译时错误:

...I get the compile time errors:

的最好重载方法匹配'Windows.System.Launcher.LaunchFileAsync(Windows.Storage.IStorageFile)'有一些无效参数

- 和:

参数1:无法从Windows.Foundation.IAsyncOperation'转换为'Windows.Storage.IStorageFile

...在最后一行。

据临Windows 8的编程由Lecrenski,荷兰,桑德斯和希礼的第76页,这应该工作:

According to page 76 of "Pro Windows 8 Programming" by Lecrenski, Netherlands, Sanders, and Ashely, this should work:

<Image Source="Assets/axXAndSpaceLogo.jpg" Stretch="None"></Image>



...(IOW,在 MS-APPX:/// 爵士乐是不必要),以及它或多或少一样。在我的特殊情况下,用我的(大)的形象,我不得不这样做:

...(IOW, the "ms-appx:///" jazz is unnecessary), and it more or less does. In my particular case, with my (large) image, I had to do this:

<Image Source="Assets/axXAndSpaceLogo.jpg" Width="120" Height="80" HorizontalAlignment="Left"></Image>



没有宽度和高度设置,该图像显示得比犀牛大,拥抱的右侧弹出按钮。

Without the width and height settings, the image displayed bigger than a rhinoceros, and hugging the right side of the flyout.

我觉得这工程,以打开PDF文件(PlatypusTut.pdf 已被添加到项目中,以建设行动设置为内容和复制到输出Diretory设置为复制,如果新):

I find that this works to open a PDF file ("PlatypusTut.pdf" has been added to the project, with "Build Action" set to "Content" and "Copy to Output Diretory" set to "Copy if newer"):

IStorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
IStorageFile file = await folder.GetFileAsync("PlatypusTut.pdf");
bool success = await Launcher.LaunchFileAsync(file);
if (!success)
{
    MessageDialog dlgDone = new MessageDialog("Unable to open the Tutorial at this time. Try again later.");
    await dlgDone.ShowAsync();
}



...但我不知道这是否会只在设计时的工作,本地。就当用户的机器上安装这个可以吗? IOW,它是不够的,只是通过PlatypusTut.pdf来GetFileAsync()?

...but I wonder if this will only work at design-time, locally. Will this work when installed on user's machines, too? IOW, is it enough to simply pass "PlatypusTut.pdf" to GetFileAsync()?

推荐答案

使用MS-APPX协议(例如MS-APPX:///assets/image.png)引用在应用程序包中的物品。见如何加载文件资源(XAML)

Use the ms-appx protocol (e.g. ms-appx:///assets/image.png )to reference items in the apps package. See How to load file resources (XAML)

更新:

使用GetFileFromApplicationUriAsync使用MS-APPX找到文件该应用程序包。如果该文件被标记为内容,并包含在应用包,那么一旦部署,而不仅仅是在调试器是可用。 MS-APPX:///PlatypusTut.pdf会发现在应用程序包的根PlatypusTut.pdf

Use GetFileFromApplicationUriAsync with ms-appx to find the file in the app package. If the file is marked as content and included in the app package then it will be available once deployed and not just from in the debugger. ms-appx:///PlatypusTut.pdf will find the PlatypusTut.pdf in the root of the app package.

StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///PlatypusTut.pdf"));
await Launcher.LaunchFileAsync(file);

这篇关于我怎么能打开我已经添加到我的Windows Store应用编程项目的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆