设为首页 收藏本站
查看: 1351|回复: 0

重新想象 Windows 8 Store Apps (33)

[复制链接]

尚未签到

发表于 2015-5-22 10:01:43 | 显示全部楼层 |阅读模式
  [源码下载]




重新想象 Windows 8 Store Apps (33) - 关联启动: 使用外部程序打开一个文件或uri, 关联指定的文件类型或协议  
作者:webabcd

介绍
重新想象 Windows 8 Store Apps 之 关联启动


  • 使用外部程序打开一个文件
  • 使用外部程序打开一个 Uri
  • 关联指定的文件类型(即用本程序打开指定类型的文件)
  • 关联指定的协议(即用本程序处理指定的协议)
  
示例
1、演示如何使用外部程序打开一个文件
AssociationLaunching/LaunchFile.xaml














  AssociationLaunching/LaunchFile.xaml.cs



/*
* 演示如何使用外部程序打开一个文件
*/
using System;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace XamlDemo.AssociationLaunching
{
public sealed partial class LaunchFile : Page
{
public LaunchFile()
{
this.InitializeComponent();
}
private async void btnLaunchFile_Click_1(object sender, RoutedEventArgs e)
{
/*
* Launcher - 用于启动与指定文件相关的应用程序
*     LaunchFileAsync(IStorageFile file) - 打开指定的文件
*     LaunchFileAsync(IStorageFile file, LauncherOptions options) - 打开指定的文件
*
* LauncherOptions - 启动外部应用程序时的相关选项
*     TreatAsUntrusted - 使用默认应用程序打开指定的文件时,是否弹出安全警告
*     DisplayApplicationPicker - 是否弹出“打开方式”对话框
*     UI.InvocationPoint - 指定“打开方式”对话框的显示位置
*     
* 当指定的文件不被任何应用程序支持时,可以用以下下两种方法处理
* 1、指定 LauncherOptions.FallbackUri: 打开浏览器并跳转到指定的地址
* 2、指定 PreferredApplicationDisplayName 和 PreferredApplicationPackageFamilyName
*    PreferredApplicationDisplayName - 指定在弹出的“在商店搜索”对话框中所显示的应用程序名称
*    PreferredApplicationPackageFamilyName - 用户点击“在商店搜索”后,会在商店搜索指定 PackageFamilyName
*/

// 指定需要打开的文件
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"Assets\Logo.png");
// 指定打开文件过程中相关的各种选项
var options = new Windows.System.LauncherOptions();
if (radWarning.IsChecked.Value)
{
options.TreatAsUntrusted = true;
}
if (radOpenWith.IsChecked.Value)
{
Point openWithPosition = GetOpenWithPosition(btnLaunchFile);
options.DisplayApplicationPicker = true;
options.UI.InvocationPoint = openWithPosition;
}
// 使用外部程序打开指定的文件
bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
if (success)
{
lblMsg.Text = "打开成功";
}
else
{
lblMsg.Text = "打开失败";
}
}
// 获取“打开方式”对话框的显示位置,即关联 Button 的左下角点的坐标
private Windows.Foundation.Point GetOpenWithPosition(FrameworkElement element)
{
Windows.UI.Xaml.Media.GeneralTransform buttonTransform = element.TransformToVisual(null);
Point desiredLocation = buttonTransform.TransformPoint(new Point());
desiredLocation.Y = desiredLocation.Y + element.ActualHeight;
return desiredLocation;
}
}
}
  
2、演示如何使用外部程序打开指定的 Uri
AssociationLaunching/LaunchUri.xaml














  AssociationLaunching/LaunchUri.xaml.cs



/*
* 演示如何使用外部程序打开指定的 Uri
*/
using System;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace XamlDemo.AssociationLaunching
{
public sealed partial class LaunchUri : Page
{
public LaunchUri()
{
this.InitializeComponent();
}
private async void btnLaunchUri_Click_1(object sender, RoutedEventArgs e)
{
/*
* Launcher - 用于启动与指定 Uri 相关的应用程序
*     LaunchUriAsync(Uri uri) - 打开指定的 Uri
*     LaunchUriAsync(Uri uri, LauncherOptions options) - 打开指定的 Uri
*
* LauncherOptions - 启动外部应用程序时的相关选项
*     TreatAsUntrusted - 使用默认应用程序打开指定的文件时,是否弹出安全警告
*     DisplayApplicationPicker - 是否弹出“打开方式”对话框
*     UI.InvocationPoint - 指定“打开方式”对话框的显示位置
*     
* 当指定的 Uri 不被任何应用程序支持时,可以用以下下两种方法处理
* 1、指定 LauncherOptions.FallbackUri: 打开浏览器并跳转到指定的地址
* 2、指定 PreferredApplicationDisplayName 和 PreferredApplicationPackageFamilyName
*    PreferredApplicationDisplayName - 指定在弹出的“在商店搜索”对话框中所显示的应用程序名称
*    PreferredApplicationPackageFamilyName - 用户点击“在商店搜索”后,会在商店搜索指定 PackageFamilyName
*/
// 指定需要打开的 Uri
var uri = new Uri("http://webabcd.iyunv.com");
// 指定打开 Uri 过程中相关的各种选项
var options = new Windows.System.LauncherOptions();
if (radWarning.IsChecked.Value)
{
options.TreatAsUntrusted = true;
}
if (radOpenWith.IsChecked.Value)
{
Point openWithPosition = GetOpenWithPosition(btnLaunchUri);
options.DisplayApplicationPicker = true;
options.UI.InvocationPoint = openWithPosition;
}
// 使用外部程序打开指定的 Uri
bool success = await Windows.System.Launcher.LaunchUriAsync(uri, options);
if (success)
{
lblMsg.Text = "打开成功";
}
else
{
lblMsg.Text = "打开失败";
}
}
// 获取“打开方式”对话框的显示位置,即关联 Button 的左下角点的坐标
private Windows.Foundation.Point GetOpenWithPosition(FrameworkElement element)
{
Windows.UI.Xaml.Media.GeneralTransform buttonTransform = element.TransformToVisual(null);
Point desiredLocation = buttonTransform.TransformPoint(new Point());
desiredLocation.Y = desiredLocation.Y + element.ActualHeight;
return desiredLocation;
}
}
}
  
3、演示如何关联指定的文件类型(即用本程序打开指定类型的文件)
AssociationLaunching/FileTypeAssociation.xaml






本程序可以打开 .webabcd 类型的文件

测试方法:在桌面新建一个文本文件,随便输一些字符,然后将后缀名改为 .webabcd,最后打开文件,本程序会显示其文本内容



  AssociationLaunching/FileTypeAssociation.xaml.cs



/*
* 演示如何关联指定的文件类型(即用本程序打开指定类型的文件)
*
* 1、在 Package.appxmanifest 中新增一个“文件类型关联”声明,并做相关配置
* 2、在 App.xaml.cs 中 override void OnFileActivated(FileActivatedEventArgs args),以获取相关的文件信息
*
* FileActivatedEventArgs - 通过打开文件激活应用程序时的事件参数
*     Files - 相关的文件信息
*     PreviousExecutionState, Kind, SplashScreen - 各种激活 app 的方式的事件参数基本上都有这些属性,就不多说了
*/
using System;
using Windows.ApplicationModel.Activation;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace XamlDemo.AssociationLaunching
{
public sealed partial class FileTypeAssociation : Page
{
private FileActivatedEventArgs _fileActivated;
public FileTypeAssociation()
{
this.InitializeComponent();
this.Loaded += FileTypeAssociation_Loaded;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// 获取 FileActivatedEventArgs 对象
_fileActivated = e.Parameter as FileActivatedEventArgs;
}
async void FileTypeAssociation_Loaded(object sender, RoutedEventArgs e)
{
// 获取文件中的文本内容,并显示
if (_fileActivated != null)
{
var isf = _fileActivated.Files[0] as IStorageFile;
lblMsg.Text = await FileIO.ReadTextAsync(isf);
}
}
}
}
  App.xaml.cs



// 通过打开文件激活应用程序时所调用的方法
protected override void OnFileActivated(FileActivatedEventArgs args)
{
Frame rootFrame = new Frame();
rootFrame.Navigate(typeof(MainPage), args);
Window.Current.Content = rootFrame;
Window.Current.Activate();
}
  
4、演示如何关联指定的协议(即用本程序处理指定的协议)
AssociationLaunching/ProtocolAssociation.xaml







本程序可以处理 webabcd:// 协议





  AssociationLaunching/ProtocolAssociation.xaml.cs



/*
* 演示如何关联指定的协议(即用本程序处理指定的协议)
*
* 1、在 Package.appxmanifest 中新增一个“协议”声明,并做相关配置
* 2、在 App.xaml.cs 中 override void OnActivated(IActivatedEventArgs args),以获取相关的协议信息
*
* ProtocolActivatedEventArgs - 通过协议激活应用程序时的事件参数
*     Uri - 协议的 uri
*     PreviousExecutionState, Kind, SplashScreen - 各种激活 app 的方式的事件参数基本上都有这些属性,就不多说了
*/
using System;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace XamlDemo.AssociationLaunching
{
public sealed partial class ProtocolAssociation : Page
{
private ProtocolActivatedEventArgs _protocolActivated;
public ProtocolAssociation()
{
this.InitializeComponent();
this.Loaded += ProtocolAssociation_Loaded;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// 获取 ProtocolActivatedEventArgs 对象
_protocolActivated = e.Parameter as ProtocolActivatedEventArgs;
}
void ProtocolAssociation_Loaded(object sender, RoutedEventArgs e)
{
// 显示协议的详细信息
if (_protocolActivated != null)
lblMsg.Text = _protocolActivated.Uri.AbsoluteUri;
}
private async void btnProtocol_Click_1(object sender, RoutedEventArgs e)
{
// 打开自定义协议 webabcd://data
var uri = new Uri("webabcd://data");
await Windows.System.Launcher.LaunchUriAsync(uri);
// 打开 IE 浏览器,在地址栏输入 webabcd://data,即会打开本程序来处理此协议
        }
}
}
  App.xaml.cs



protected override void OnActivated(IActivatedEventArgs args)
{
// 通过协议激活应用程序时
if (args.Kind == ActivationKind.Protocol)
{
ProtocolActivatedEventArgs protocolArgs = args as ProtocolActivatedEventArgs;
Frame rootFrame = new Frame();
rootFrame.Navigate(typeof(MainPage), protocolArgs);
Window.Current.Content = rootFrame;
Window.Current.Activate();
}
}
  
OK
[源码下载]

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-69443-1-1.html 上篇帖子: Windows 8 学习笔记(六)—Accelerormeter和GeoLocation 下篇帖子: Windows 8 的 Metro 程序
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表