我有一个按钮,当点击它将打开其他应用程序(如果您已经有应用程序)或向Windows商店(如果它没有应用程序)。但我有一个问题,那就是:当我有应用程序并单击按钮时,仅显示启动画面而无法打开应用程序。
XAML:
<Button x:Name="miBtn" Width="50" Height="50" Margin="25,0,0,0" Click="miBtn_Click" Style="{StaticResource ButtonStyle1}" BorderBrush="{x:Null}">
<Button.Background>
<ImageBrush Stretch="Uniform" ImageSource="image/new (3.0)/menu/menu bawah/MI-ikon-200.png"/>
</Button.Background>
</Button>
代码:
private async void miBtn_Click(object sender, RoutedEventArgs e)
{
var options = new Windows.System.LauncherOptions();
options.PreferredApplicationPackageFamilyName = "MahoniGlobalPT.MajalahIndonesia_rm0rfdtcrak1p";
options.PreferredApplicationDisplayName = "Majalah Indonesia";
// Launch the URI and pass in the recommended app
var uriMI = new Uri("mi1:");
// in case the user has no apps installed to handle the URI
var success = await Windows.System.Launcher.LaunchUriAsync(uriMI, options);
}
如何处理?
答案 0 :(得分:2)
当我有应用程序并单击按钮时,仅显示启动屏幕,无法打开应用程序。
使用Windows.System.Launcher.LaunchUriAsync打开应用程序时。 OnLaunchedMethod
内的目标应用App.xaml.cs
将不会被触发。因此,不会为目标应用创建根帧。因此,您只能看到启动画面。
要解决此问题,您需要手动创建目标应用程序的根框架。您可以在Application.OnActivated中实现此目的:打开目标应用程序并在App.xaml.cs中添加以下代码:
private Frame CreateRootFrame()
{
Frame rootFrame = Window.Current.Content as Frame;
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
// Set the default language
rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];
rootFrame.NavigationFailed += OnNavigationFailed;
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
return rootFrame;
}
更新:创建框架的过程应采用OnActivated
方法:
protected override void OnActivated(IActivatedEventArgs args)
{
var rootFrame = CreateRootFrame();
rootFrame.Navigate(typeof(MainPage));//here navigate to typeof(YourPageName)
Window.Current.Activate();
}
答案 1 :(得分:0)
此示例对您有所帮助,请点击How to launch an UWP app from another app。
在您启动的应用中,您需要配置package.appxmanifest
。以下XML是核心配置。
<Package>
<Applications>
<Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="CSReceiveUri.App">
<Extensions>
<uap:Extension Category="windows.protocol">
<uap:Protocol Name="test-launchmainpage">
<uap:DisplayName>test-launchmainpage</uap:DisplayName>
</uap:Protocol>
</uap:Extension>
<uap:Extension Category="windows.protocol">
<uap:Protocol Name="test-launchpage1">
<uap:DisplayName>test-launchpage1</uap:DisplayName>
</uap:Protocol>
</uap:Extension>
</Extensions>
</Application>
</Applications>
</Package>
在已启动应用的app.cs中,您需要覆盖OnActivated
事件处理程序,如下所示:
protected override void OnActivated(IActivatedEventArgs args)
{
if (args.Kind == ActivationKind.Protocol)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
rootFrame = new Frame();
Window.Current.Content = rootFrame;
rootFrame.NavigationFailed += OnNavigationFailed;
}
//because this is in (args.Kind == ActivationKind.Protocol) block, so the type of args must is ProtocolActivatedEventArgs
//convert to type ProtocolActivatedEventArgs, and we can visit Uri property in type ProtocolActivatedEventArgs
var protocolEventArgs = args as ProtocolActivatedEventArgs;
//Switch to a view by Scheme
switch (protocolEventArgs.Uri.Scheme)
{
//under case is the protocol scheme in the Package.appxmanifest
//Navigate to target page with Uri as parameter
case "test-launchmainpage":
rootFrame.Navigate(typeof(MainPage), protocolEventArgs.Uri);
break;
case "test-launchpage1":
rootFrame.Navigate(typeof(Page1), protocolEventArgs.Uri);
break;
default:
rootFrame.Navigate(typeof(MainPage), protocolEventArgs.Uri);
break;
}
//start show UI
Window.Current.Activate();
}
}