2016-08-09 68 views
0

Ich habe eine Schaltfläche, die nach dem Klicken andere Apps (wenn Sie bereits die App haben) oder in Richtung Windows-Store (wenn es nicht die App hatte) zu öffnen. Aber ich habe ein Problem, nämlich: Wenn ich die Anwendung habe und auf die Schaltfläche klicke, wird nur der Begrüßungsbildschirm angezeigt und die Anwendung kann nicht geöffnet werden.Starten zu einer anderen Anwendung

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

Wie es behandeln?

Antwort

2

Wenn ich die Anwendung habe und auf die Schaltfläche klicke, wird nur der Begrüßungsbildschirm angezeigt und die Anwendung kann nicht geöffnet werden.

Wenn Sie eine App mit Windows.System.Launcher.LaunchUriAsync öffnen. Die OnLaunchedMethod der Ziel-App innerhalb App.xaml.cs wird nicht ausgelöst. Es wird also kein Root-Frame für die Ziel-App erstellt. Daher können Sie nur den Begrüßungsbildschirm sehen.

Um das Problem zu lösen, müssen Sie manuell root Frame der Ziel-App erstellen. Sie können dies in Application.OnActivated erreichen: Öffnen Sie Ihre Ziel App und innerhalb App.xaml.cs und fügen Sie die folgenden 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: Der Prozess Rahmen zu schaffen, in OnActivated Methode sein sollte:

protected override void OnActivated(IActivatedEventArgs args) 
{ 
    var rootFrame = CreateRootFrame(); 
    rootFrame.Navigate(typeof(MainPage));//here navigate to typeof(YourPageName) 
    Window.Current.Activate(); 
} 
+0

Ich habe versucht, oben auf App.xaml.cs den Code hinzufügen, aber es immer noch zeigt nur einen Begrüßungsbildschirm an – Rose

+0

Haben Sie die Codes zur Ziel-App hinzugefügt? –

+0

Ja, ich füge den Code zur Ziel-App hinzu und führe die App aus – Rose

0

Dieses Beispiel ist hilfreich für Sie, klicken Sie auf How to launch an UWP app from another app. In Ihrer gestarteten App müssen Sie package.appxmanifest konfigurieren. Unter XML steht die Kernkonfiguration.

<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 dem app.cs gestarteten App, müssen Sie OnActivated Event-Handler, wie dies außer Kraft setzen:

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