Association startup: Open a file or URI using an external program, associating the specified file type or protocol
Introduced
Re-imagine the Windows 8 Store Apps associated with startup
Open a file using an external program
To open a Uri using an external program
Associates the specified file type (that is, open a file of the specified type with this program)
Associates the specified protocol (that is, handling the specified protocol with this procedure)
Example
1. Demonstrates how to use an external program to open a file
Associationlaunching/launchfile.xaml
<page x:class= "XamlDemo.AssociationLaunching.LaunchFile" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/ Presentation "xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "xmlns:local=" Using:XamlDemo.Launcher "xml Ns:d= "http://schemas.microsoft.com/expression/blend/2008" xmlns:mc= "http://schemas.openxmlformats.org/ markup-compatibility/2006 "mc:ignorable=" D "> <grid background=" Transparent "> <stackpanel margin= "0 0 0" > <textblock name= "lblmsg" margin= "0 0 0"/> <radiobutton Co ntent= "Open file with default open name=" Raddefault "groupname=" Launchtype "True" ischecked=/> "<radiobutton" Open the file by using the default open, pop-up warning box "name=" radwarning "groupname=" Launchtype "/> <radiobutton content=" To open file by selecting the open mode specified Name= "Radopenwith" groupname= "launchtype/> <button content=" opens a. png format file "Name=" btn LaunchFile "click=" Btnlaunchfile_click_1 " margin= "0 0 0"/> </StackPanel> </Grid> </Page>
Associationlaunching/launchfile.xaml.cs
* * Demonstrates how to use an external program to open a file * * using System;
Using Windows.foundation;
Using Windows.UI.Xaml;
Using Windows.UI.Xaml.Controls; Namespace Xamldemo.associationlaunching {public sealed partial class Launchfile:page {public Launchfil E () {this.
InitializeComponent ();
Private async void Btnlaunchfile_click_1 (object sender, RoutedEventArgs e) {/* * Launcher-for starting the application associated with the specified file * Launchfileasync (istoragefile file)-Opens the specified file * Lau Nchfileasync (istoragefile file, launcheroptions options)-Open the specified file * * launcheroptions-Start external applications Related options * treatasuntrusted-When using the default application to open the specified file, whether to eject the security warning * displayapplicationpicker-whether to play Out the Open With dialog box * UI. Invocationpoint-Specify the display location of the Open With dialog box * * When the specified file is not supported by any application, the following two methods can be used to process * 1, specify L Auncheroptions.fallbackuri: Opens the browser and jumps to the specifiedAddress * 2, designated Preferredapplicationdisplayname and Preferredapplicationpackagefamilyname * preferred ApplicationDisplayName-Specifies the name of the application that appears in the "Store Search" dialog box * preferredapplicationpackagefamilyname-user clicks "Search in Store" After the store is searched for the specified packagefamilyname///Specify the file to be opened var file = await Windows.appli
CationModel.Package.Current.InstalledLocation.GetFileAsync (@ "Assets\logo.png");
Specifies various options related to opening a file 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; ///Use external program to open the specified file bool success = await Windows.System.Launcher.LaunchFileAsync (file, options);
if (success) {Lblmsg.text = "open successfully";
else {Lblmsg.text = "open Failed"; }//Get the display location of the Open With dialog box, that is, the coordinates of the lower left corner of the associated Button private Windows.Foundation.Point getopenwithposit Ion (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. Demonstrates how to use an external program to open a specified Uri
Associationlaunching/launchuri.xaml
<page x:class= "XamlDemo.AssociationLaunching.LaunchUri" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/p Resentation "xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "xmlns:local=" Using:XamlDemo.Launcher "XMLN" S:d= "http://schemas.microsoft.com/expression/blend/2008" xmlns:mc= "http://schemas.openxmlformats.org/ markup-compatibility/2006 "mc:ignorable=" D "> <grid background=" Transparent "> <stackpanel margin= "0 0 0" > <textblock name= "lblmsg" margin= "0 0 0"/> <radiobutton Co Ntent= "Open the specified Uri using default open" Name= "Raddefault" groupname= "Launchtype" ischecked= "True"/> <radiobutton Ent= "opens the specified Uri using the default open, before opening the pop-up warning box" name= "radwarning" groupname= "Launchtype"/> <radiobutton content= "to select the specified Open to open the specified URI "name=" Radopenwith "groupname=" launchtype/> <button content= "open a URI" Name= "Btnlaunchuri" click= "btnlaunchuri_clIck_1 "margin=" 0 0 0 "/> </StackPanel> </Grid> </Page>