【问题标题】:How can I open a file I've added to my Windows Store App project programatically?如何以编程方式打开已添加到我的 Windows 应用商店应用项目的文件?
【发布时间】:2015-01-07 13:04:02
【问题描述】:

我想加载 PDF 文件以响应 Tapped 事件。

我将文件添加到我的项目中(添加 > 现有项目),将“构建操作”设置为“内容”,将“复制到输出目录”设置为“如果较新则复制”

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

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

如果我是对的,我需要作为 Uri 传递什么?不然怎么实现?

更新

在相关说明中,要使用建议的方案将图像添加到 XAML,我认为这会起作用:

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

...但事实并非如此。

更新 2

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

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

...导致这个运行时异常,在上面第一行抛出:

更新 3

有了这个,改编自提供的链接:

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

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

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

-和:

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

...在最后一行。

更新 4

根据 Lecrenski、Netherlands、Sanders 和 Ashely 撰写的“Pro Windows 8 Programming”第 76 页,这应该可行:

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

...(IOW,“ms-appx:///”爵士乐是不必要的),它或多或少确实如此。在我的特殊情况下,对于我的(大)图像,我必须这样做:

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

如果没有设置宽度和高度,图像会显示得比犀牛大,并紧贴弹出窗口的右侧。

更新 5

我发现这可以打开 PDF 文件(“PlatypusTut.pdf”已添加到项目中,“构建操作”设置为“内容”,“复制到输出目录”设置为“如果更新则复制” ):

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() 就足够了吗?

【问题讨论】:

标签: c# windows-runtime windows-store-apps buildaction output-directory


【解决方案1】:

我们就是这样做的:

public async Task OpenResearchAsync(string path)
{
    if (path.ToLower().StartsWith("http://"))
    {
        await Launcher.LaunchUriAsync(new Uri(path));
    }
    else
    {
        IStorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
        IStorageFile file = await folder.GetFileAsync(path);
        await Launcher.LaunchFileAsync(file);
    }
}

【讨论】:

    【解决方案2】:

    使用 ms-appx 协议(例如 ms-appx:///assets/image.png )引用应用程序包中的项目。见How to load file resources (XAML)

    更新:

    使用 GetFileFromApplicationUriAsync 和 ms-appx 在应用包中查找文件。如果文件被标记为内容并包含在应用程序包中,那么它在部署后将可用,而不仅仅是在调试器中。 ms-appx:///PlatypusTut.pdf 会在应用包的根目录下找到 PlatypusTut.pdf。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-30
      • 1970-01-01
      • 2014-02-03
      • 1970-01-01
      • 2015-06-08
      • 1970-01-01
      相关资源
      最近更新 更多