[英]How can I open a file I've added to my Windows Store App project programatically?
我想加载一个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>
......但事实并非如此。
试着打开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()就足够了吗?
使用ms-appx协议(例如ms-appx:///assets/image.png)来引用应用包中的项目。 请参见如何加载文件资源(XAML)
更新:
使用带有ms-appx的GetFileFromApplicationUriAsync查找应用程序包中的文件。 如果文件被标记为内容并包含在应用程序包中,那么它将在部署后可用,而不仅仅是在调试器中。 ms-appx:///PlatypusTut.pdf将在应用包的根目录中找到PlatypusTut.pdf。
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///PlatypusTut.pdf"));
await Launcher.LaunchFileAsync(file);
我们这样做了:
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.