zoukankan      html  css  js  c++  java
  • 08、AssociationLaunching

      1、Launching a file:

          注: 1)、2)、3)、4) 都使用了  Windows.System.LaunchFileAsync 方法进行打开。

    //本例中用到的程序包中的图片资源
      string fileToLaunch = @"images\Icon.Targetsize-256.png";

          1)  加载本程序中的一张 .png 图片,并且用系统中默认的于本文件相关联的应用程序打开

              .xaml 页面:

    <Button x:Name="LaunchFileButton" Content="Launch Default Handler" 
         Click="LaunchFileButton_Click" Margin="0,0,10,0"/>

               对应的 C# 页面:

      //加载包中的一张 .png 图片
            private async void LaunchFileButton_Click(object sender, RoutedEventArgs e)
            {
                // 首先获取本程序中 image 文件夹下的图片
                var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(fileToLaunch);
    
                // 启动与指定的文件相关联的默认应用程序。
                bool success = await Windows.System.Launcher.LaunchFileAsync(file);
                if (success)
                {
                   //图片打开成功
                   ....
                }
                else
                {
                    //图片打开失败
                   .....
                }
            }

                     显示结果:

                                  (图片 Sample.png )

                2)   加载图片前,显示警告弹出框。 通过设置 Windows.System.LauncherOptions.TreatAsUntrusted  为 true, 从而让用户选择是否打开文件。

               .xaml 页面

     <Button x:Name="LaunchFileWithWarningButton" Content="Launch with Warning"
     Click="LaunchFileWithWarningButton_Click" Margin="0,0,10,0"/>

               相应的 C# 页面:

            private async void LaunchFileWithWarningButton_Click(object sender, RoutedEventArgs e)
            {
                    var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(fileToLaunch);
    
                // 指定默认应用程序的启动选项。
                var options = new Windows.System.LauncherOptions();
    
               // 获取或设置一个值,该值指示启动与文件或协议关联的应用程序时系统是否应显示文件或 URI 可能会不安全的警告。 设置为 true 从而显示警告
                options.TreatAsUntrusted = true;
    
                // 异步加载图片
                bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
                if (success)
                {
                   // succeed
                }
                else
                {
                    //fail
                }
            }

           单击按钮,显示警告:

          如果单击是,则显示  1) 中的 "(图片 Sample.png )"。 单击 否 则无操作。

               3)  用户选择 图片打开方式 进行打开。

                  .xaml :

     <Button x:Name="LaunchFileOpenWithButton" Content="Launch Open With" 
    Click="LaunchFileOpenWithButton_Click" Margin="0,0,10,0"/>

                 相应的 C# 代码:

          // 用户 文件打开方式对话框  选择图片的打开方式 进行打开
            private async void LaunchFileOpenWithButton_Click(object sender, RoutedEventArgs e)
            {
                 // 获取图片
                   var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(fileToLaunch);
    
                  // 计算对话框打开的位置
                    // 调用下面代码块中的方法, 获取 UI 页面中 按钮的位置
                    Point openWithPosition = GetOpenWithPosition(LaunchFileOpenWithButton);
    
                   // 配置打开对话框
                     var options = new Windows.System.LauncherOptions();
              
                   // 获取或设置一个值,该值指示每当调用关联启动 API 时是否要显示 “打开方式” 对话框。
                     options.DisplayApplicationPicker = true;
    
                  //获取或设置用户在其上打开文件或协议的屏幕的点。
                    options.UI.InvocationPoint = openWithPosition;
               
                  // 将该上下文菜单放置在选择矩形的下方。
                    options.UI.PreferredPlacement = Windows.UI.Popups.Placement.Below;
    
                 // 异步打开图片
                   bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
                
    if (success) { //succeed } else { // fail } }

              

           // 计算 UI 元素的坐标点
            private Windows.Foundation.Point GetOpenWithPosition(FrameworkElement element)
            {
               // 返回变换对象,该变换对象可用于将 UIElement 中的坐标变换为指定的对象。
                Windows.UI.Xaml.Media.GeneralTransform buttonTransform = element.TransformToVisual(null);
    
                Point desiredLocation = buttonTransform.TransformPoint(new Point());  //变换指定的点并返回结果。
                desiredLocation.Y = desiredLocation.Y + element.ActualHeight;
    
                return desiredLocation;
            }


    选择 打开方式 对话框 :

          4)用户选择文件进行打开:

                .xaml 页面:

     <Button x:Name="PickAndLaunchFileButton" Content="Pick and Launch" 
    Click="PickAndLaunchFileButton_Click" Margin="0,0,10,0"/>

               对应的方法:

       // 如果用户选择了图片,就加载它
            private async void PickAndLaunchFileButton_Click(object sender, RoutedEventArgs e)
            {           
                //用户使用打开文件对话框时,窗口应处于 非snapped 状态。  
                if (Windows.UI.ViewManagement.ApplicationView.Value == Windows.UI.ViewManagement.ApplicationViewState.Snapped)
                {
                   // 尝试展开以前已对齐的应用程序。
                    if (!Windows.UI.ViewManagement.ApplicationView.TryUnsnap())
                    {
                        rootPage.NotifyUser("Unable to unsnap the sample.", NotifyType.ErrorMessage);
                        return;
                    }
                }
    
                var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
                openPicker.FileTypeFilter.Add("*"); //所有类型的文件
    
                Windows.Storage.StorageFile file = await openPicker.PickSingleFileAsync();
              //如果用户选择了文件,则打开它
    
                if (file != null)
                {
                         bool success = await Windows.System.Launcher.LaunchFileAsync(file);
                    if (success)
                    {
                         //succeed
                    }
                    else
                    {
                         //fail
                    }
                }
                else
                {
                    //用户没有选择文件
                }
            }

              显示结果与之前的类似。

    2、Launching a URI:

           本例中是使用的是 Windows.System.LaunchUriAsync 方法,使用的默认 URI:

     string uriToLaunch = @"http://www.bing.com";

         1) 用系统默认程序打开 指定的URI (http://www.bing.com):

          .xaml 文件:

      <Button x:Name="LaunchUriButton" Content="Launch Default Handler" 
    Click="LaunchUriButton_Click" Margin="0,0,10,0"/>

         相应的 C# 页面 :

     private async void LaunchUriButton_Click(object sender, RoutedEventArgs e)
            {
                 var uri = new Uri(uriToLaunch);
    
                  bool success = await Windows.System.Launcher.LaunchUriAsync(uri);
                if (success)
                {
                   //succeed
                }
                else
                {
                    //fail
                }
            }

          2)  加载指定的 URI 前,打开警告对话框:

               通过设置  Windows.System.LauncherOptions.TreatAsUntrusted 为 true,来显示警告对话框。

            .xaml 页面:

     <Button x:Name="LaunchUriWithWarningButton" Content="Launch with Warning" 
    Click="LaunchUriWithWarningButton_Click" Margin="0,0,10,0"/>

             相应的 C# 页面:

            private async void LaunchUriWithWarningButton_Click(object sender, RoutedEventArgs e)
            {
                var uri = new Uri(uriToLaunch);
    
                  var options = new Windows.System.LauncherOptions();
              //  获取或设置一个值,该值指示启动与文件或协议关联的应用程序时系统是否应显示文件或 URI 可能会不安全的警告。
                options.TreatAsUntrusted = true;
    
                  bool success = await Windows.System.Launcher.LaunchUriAsync(uri, options);
                if (success)
                {
                   //succeed
                }
                else
                {
                    //fail
                }
    
            }


           3)   用户选择打开 URI 的方式:

             .xaml 页面:

     <Button x:Name="LaunchUriOpenWithButton" Content="Launch Open With" 
    Click="LaunchUriOpenWithButton_Click" Margin="0,0,10,0"/>

             对应的 C# 页面:

      private async void LaunchUriOpenWithButton_Click(object sender, RoutedEventArgs e)
            {           
                var uri = new Uri(uriToLaunch);
    
                // 计算按钮的位置,调用下面代码块的方法
                Point openWithPosition = GetOpenWithPosition(LaunchUriOpenWithButton);
    
                // 同上
                var options = new Windows.System.LauncherOptions();
                options.DisplayApplicationPicker = true;
                options.UI.InvocationPoint = openWithPosition;
                options.UI.PreferredPlacement = Windows.UI.Popups.Placement.Below;
    
                 bool success = await Windows.System.Launcher.LaunchUriAsync(uri, options);
                if (success)
                {
                   //succeed
                }
                else
                {
                   //fail
                }
            }


             获取元素的位置:

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


             结果与上面类似。

  • 相关阅读:
    C++拷贝构造函数具体解释
    兼容安卓的javaproject1.0
    php课程 12-40 抽象类的作用是什么
    php中类文件名的命名的规则是什么
    妙味css3课程---1-2、css3中新增的伪类和伪元素有哪些
    excel表如何实现多if选择结构多分支判断
    php如何读写excel
    php课程 12-39 继承中parent的作用是什么
    Dcloud课程9 天气小助手如何实现
    Dcloud课程8 开心一刻应用如何实现
  • 原文地址:https://www.cnblogs.com/hebeiDGL/p/2687323.html
Copyright © 2011-2022 走看看