2016-08-09 4 views
0

다른 응용 프로그램 (이미 응용 프로그램이있는 경우)을 열거 나 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); 
     } 

어떻게 처리 하는가?

답변

2

내가 응용 프로그램을 가지고 단추를 클릭하면 시작 화면 만 표시되고 응용 프로그램을 열 수 없습니다.

Windows.System.Launcher.LaunchUriAsync를 사용하여 응용 프로그램을 여는 경우. App.xaml.cs 안에 대상 앱의 OnLaunchedMethod이 실행되지 않습니다. 따라서 대상 앱에 대해 만들어진 루트 프레임이 없습니다. 따라서 스플래시 화면 만 볼 수 있습니다.

문제를 해결하려면 대상 응용 프로그램의 루트 프레임을 수동으로 만들어야합니다. 당신은 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; 
} 

업데이트 :

protected override void OnActivated(IActivatedEventArgs args) 
{ 
    var rootFrame = CreateRootFrame(); 
    rootFrame.Navigate(typeof(MainPage));//here navigate to typeof(YourPageName) 
    Window.Current.Activate(); 
} 
:
이 프레임을 만드는 과정은 OnActivated 방법에 있어야
+0

,하지만 여전히 그것을 : 시작 응용 프로그램의 app.cs에서

<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> 

,이 같은 OnActivated 이벤트 핸들러를 오버라이드 (override)가 필요 스플래시 화면 만 표시 – Rose

+0

대상 응용 프로그램에 코드를 추가 했습니까? –

+0

예, 대상 앱에 코드를 추가하고 앱을 실행하십시오 – Rose

0

이 샘플은 도움이 될 것입니다. 클릭 How to launch an UWP app from another app.

출시 된 앱에서 package.appxmanifest을 설정해야합니다. XML 아래의 핵심 구성입니다. 내가 App.xaml.cs를에 위의 코드를 추가하려고했습니다

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(); 
    } 
} 
관련 문제