Reputation: 611
I have a button that when clicked will open other apps (if you already have the app) or toward the windows store (if it did not have the app). But I have a problem, that is: when I have the application and clicking the button displays only the splash screen only and cannot open the application.
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>
Code:
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);
}
How to handle it?
Upvotes: 0
Views: 643
Reputation: 1118
This Sample will be helpful for you, Click How to launch an UWP app from another app.
In your launched app, you need to config package.appxmanifest
. below XML is the core configuration.
<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>
In the app.cs of launched app, you need override OnActivated
event handler, like this:
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();
}
}
Upvotes: 0
Reputation: 10831
when I have the application and clicking the button displays only the splash screen only and cannot open the application.
When you are opening an App using Windows.System.Launcher.LaunchUriAsync. The OnLaunchedMethod
of target App inside App.xaml.cs
won't be triggered. So, there won't be a root Frame created for target App. Thus, you can see splash screen only.
To solve the problem, you need to manually create root Frame of target App. You can achieve this in Application.OnActivated: Open your target App and inside App.xaml.cs and add the following codes:
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;
}
Update: The process of creating Frame should be in OnActivated
method:
protected override void OnActivated(IActivatedEventArgs args)
{
var rootFrame = CreateRootFrame();
rootFrame.Navigate(typeof(MainPage));//here navigate to typeof(YourPageName)
Window.Current.Activate();
}
Upvotes: 2