我想加载一个PDF文件以响应Tapped事件。
我将文件添加到我的项目中(添加>现有项目),将“Build Action”设置为“Content”并将“Copy to output Directory”设置为“Copy if newer”
我认为我需要的代码可能是这样的:
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>
......但事实并非如此。
尝试此操作打开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);
}
...导致此运行时异常,抛出在上面的第一行:
有了这个,改编自提供的链接:
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”
......在最后一行。
根据Lecrenski,荷兰,桑德斯和阿什利的“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>
没有宽度和高度设置,图像显示比犀牛大,并且拥抱弹出窗口的右侧。
我发现这可以打开PDF文件(“PlatypusTut.pdf”已添加到项目中,“Build Action”设置为“Content”,“Copy to Output Diretory”设置为“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()即可吗?
答案 0 :(得分:4)
使用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);
答案 1 :(得分: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);
}
}