gpt4 book ai didi

c# - 单击按钮打开 PDF 文件

转载 作者:行者123 更新时间:2023-11-30 12:47:44 24 4
gpt4 key购买 nike

我想通过单击一个按钮在 winRT 应用程序(metro 风格应用程序)中打开一个 pdf 文件,文件应该在 windows8 默认阅读器中打开。我试过这段代码,其中按钮点击方法名称是 DefaultLaunch_click():

async void DefaultLaunch_click()
{
// Path to the file in the app package to launch
string imageFile = @"images\ret.png";

// Get the image file from the package's image directory
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);



if (file != null)
{
// Set the recommended app
var options = new Windows.System.LauncherOptions();
options.PreferredApplicationPackageFamilyName = “Contoso.FileApp_8wknc82po1e”;
options.PreferredApplicationDisplayName = “Contoso File App”;




// Launch the retrieved file pass in the recommended app
// in case the user has no apps installed to handle the file
bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
else
{
// Could not find file
}
}

它适用于 .png 文件,但我想要 .pdf 文件,我用 M.pdf 替换了 1.png(在将其包含在图像文件夹中之后)并将 M.pdf 的构建内容设置为 Embedded Resource ,运行程序但是它显示错误

**The system cannot find the file specified. (Exception from HRESULT: 0x80070002)**

最佳答案

在我将 PDF 文件构建操作设置为内容并始终复制到输出目录后,此代码对我有用。

private async void Button_Click(object sender, RoutedEventArgs e)
{
string imageFile = @"images\somepdffile.pdf";

// Get the image file from the package's image directory
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);
if (file != null)
{
bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
else
{
// Could not find file
}
}

关于c# - 单击按钮打开 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15984154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com