单击按钮打开PDF文件

时间:2013-04-13 04:13:42

标签: c# file pdf windows-runtime

我想在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(在将它包含在images文件夹中之后)并将M.pdf的构建内容设置为Embedded Resource,运行程序但是它显示错误

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

1 个答案:

答案 0 :(得分:1)

在将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
        }
    }