Windows 8 Store Apps(33)-関連起動を再想像する:外部プログラムを使用してファイルまたはuriを開き、指定されたファイルタイプまたはプロトコルを関連付ける.
38587 ワード
[ソースのダウンロード]
Windows 8 Store Apps(33)-関連起動を再想像する:外部プログラムを使用してファイルまたはuriを開き、指定されたファイルタイプまたはプロトコルを関連付ける.
作者:webabcd紹介Windows 8 Store Appsの再想像 関連開始外部プログラムを使ってファイルを開く 外部プログラムを使ってUri を開きます.関連指定されたファイルタイプ(つまり、このプログラムで指定されたタイプのファイルを開く) 関連指定されたプロトコル(すなわち、本プログラムで指定されたプロトコルを処理する) 例1、外部プログラムを使ってファイルを開く方法を示すAsociation Launching/LaunchFile.xaml
Windows 8 Store Apps(33)-関連起動を再想像する:外部プログラムを使用してファイルまたはuriを開き、指定されたファイルタイプまたはプロトコルを関連付ける.
作者:webabcd紹介Windows 8 Store Appsの再想像 関連開始
<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"
xmlns: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="120 0 0 0">
<TextBlock Name="lblMsg" Margin="0 0 0 10" />
<RadioButton Content=" " Name="radDefault" GroupName="LaunchType" IsChecked="True" />
<RadioButton Content=" , " Name="radWarning" GroupName="LaunchType" />
<RadioButton Content=" " Name="radOpenWith" GroupName="LaunchType" />
<Button Content=" .png " Name="btnLaunchFile" Click="btnLaunchFile_Click_1" Margin="0 10 0 0" />
</StackPanel>
</Grid>
</Page>
Asociation Launching/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、外部プログラムを使って指定のUriAsociation Launching/LaunchUri.xamlをどうやって開きますか?<Page
x:Class="XamlDemo.AssociationLaunching.LaunchUri"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Launcher"
xmlns: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="120 0 0 0">
<TextBlock Name="lblMsg" Margin="0 0 0 10" />
<RadioButton Content=" Uri" Name="radDefault" GroupName="LaunchType" IsChecked="True" />
<RadioButton Content=" Uri, " Name="radWarning" GroupName="LaunchType" />
<RadioButton Content=" Uri" Name="radOpenWith" GroupName="LaunchType" />
<Button Content=" uri" Name="btnLaunchUri" Click="btnLaunchUri_Click_1" Margin="0 10 0 0" />
</StackPanel>
</Grid>
</Page>
Asociation Launching/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.cnblogs.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、指定されたファイルタイプをどのように関連付けるか(つまり、このプログラムで指定されたタイプのファイルを開く)Asociation Launching/FileType Asociation.xamlをデモします.<Page
x:Class="XamlDemo.AssociationLaunching.FileTypeAssociation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Launch"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="Transparent">
<TextBlock Name="lblMsg" FontSize="24" TextWrapping="Wrap" Margin="120 0 0 0">
<Run> .webabcd </Run>
<LineBreak />
<Run> : , , .webabcd, , </Run>
</TextBlock>
</Grid>
</Page>
Asociation Launching/FileType Asociation.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、指定されたプロトコルにどのように関連するかをデモする(すなわち、本プログラムで指定されたプロトコルを処理する)Asociation Launching/Protocol Asociation.xaml<Page
x:Class="XamlDemo.AssociationLaunching.ProtocolAssociation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.AssociationLaunching"
xmlns: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="120 0 0 0">
<TextBlock Name="lblMsg" FontSize="24">
<Run> webabcd:// </Run>
</TextBlock>
<Button Name="btnProtocol" Content=" webabcd://data" Click="btnProtocol_Click_1" Margin="0 10 0 0" />
</StackPanel>
</Grid>
</Page>
Asociation Launching/Protocol Asociation.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.csprotected 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 [ソースのダウンロード]