按下按钮打开PDF文件

4
我希望能通过单击按钮在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的构建内容设置为嵌入式资源,运行程序,但出现了错误:

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

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

1
是的,它起作用了 :-) 我也尝试过,运行得很好 :-) 。谢谢你... :) - Adi
你能否请在这里查看一下:http://stackoverflow.com/questions/22011326/open-pdf-file-in-emulator - user2056563

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接