Windows Store apps开发[47]使用默认程序打开文件
2012-10-29 23:08
615 查看
注:本文由BeyondVincent(破船)原创首发
转载请注明出处:BeyondVincent(破船)@DevDiv.com
更多内容请查看下面的帖子
[DevDiv原创]Windows
8 开发Step by Step
小引
在Windows 8中,有时候,我们电脑中的文件,需要启动别的程序来打开,或者有时候我们需要通过浏览器打开某个链接,这时候我们就需要用到Windows.System.Launcher。今天我就通过代码示例,来介绍如何打开文件或者链接。本文参考了:
http://msdn.microsoft.com/zh-cn/library/windows/apps/windows.system.launcher
打开文件
通过默认程序打开文件
private async void DefaultLaunch(object sender, RoutedEventArgs e) { // Path to the file in the app package to launch string imageFile = @"data\[DevDiv翻译]Metro Revealed_ Building Windows 8 apps with XAML and C#中文翻译合集_2012_09_03.pdf"; var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile); if (file != null) { // Launch the retrieved file var success = await Windows.System.Launcher.LaunchFileAsync(file); if (success) { // File launched } else { // File launch failed } } else { // Could not find file } }
运行效果:
通过程序列表打开文件
private async void DisplayApplicationPicker(object sender, RoutedEventArgs e) { // Path to the file in the app package to launch string imageFile = @"data\7.jpg"; var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile); if (file != null) { // Set the option to show the picker var options = new Windows.System.LauncherOptions(); options.DisplayApplicationPicker = true; // Launch the retrieved file bool success = await Windows.System.Launcher.LaunchFileAsync(file, options); if (success) { // File launched } else { // File launch failed } } else { // Could not find file } }
运行效果
去商店搜索对应的程序来打开文件
private async void RecommendedApp(object sender, RoutedEventArgs e) { // Path to the file in the app package to launch string imageFile = @"data\1.BeyondVincent"; // 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 = "BeyondVincent格式文件程序"; // 设置为该应用的名称 options.PreferredApplicationDisplayName = "BV_Launcher"; // 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 } }
运行效果
打开链接
private async void OpenDevDiv(object sender, RoutedEventArgs e) { // The URI to launch string uriToLaunch = @"http://www.DevDiv.com"; // Create a Uri object from a URI string var uri = new Uri(uriToLaunch); // Launch the URI var success = await Windows.System.Launcher.LaunchUriAsync(uri); if (success) { // URI launched } else { // URI launch failed } }
代码下载地址:
相关文章推荐
- Windows Store apps开发[75]Windows 8 开发31日-第18日-文件关联和程序合约
- [小代码]使用intent用默认的程序打开文件
- Windows Store apps开发[29]使用Blend设计ButtonStyle
- Delphi使用系统默认程序打开外部文件
- Windows Store apps开发[41]FlipView控件的介绍和使用
- 【StackFlow】windows不能正常设置文件的默认打开程序
- Windows Store apps开发[39]Windows 8中的Semantic Zoom(语义缩放)(2)SemanticZoom控件的使用
- Windows Store apps开发[77]Windows 8 开发31日-第19日-文件选择器
- JDK6的新API使用默认关联程序打开文件
- Windows 8开发 WinRT APP使用其他关联应用程序打开文件
- Windows Store apps开发[5]导航栏(AppBar)的使用
- 修改windows的默认使用程序及文件类型与程序关联方法
- Windows Store apps开发[56]使用外部字体
- Windows Store apps开发[3]应用程序栏(AppBar)的使用
- Windows Store apps开发[68]Windows 8 开发31日-第11日-锁屏程序
- Windows Store apps开发[48]MessageDialog的使用
- Windows Store apps开发[27]使用NetworkInformation来判断和监听网络状态
- 右键TXT文件-打开方式-选择默认程序提示 windows无法访问指定设备,路经或文件.您可能没有合适的权限访问这个项目
- 学习Java6(七)Desktop(3)使用默认程序打开所选文件
- Windows Store apps开发[16]编写和使用自己的Windows Runtime组件