Как я могу использовать await Windows.System.Launcher.LaunchUriAsync(new Uri("protocol://"));
для перехода к определенному представлению приложения uwp
Для этого, во-первых, вам нужно добавить объявление Protocol в ваш Package.appxmanifest файл.(Перейдите на вкладку объявлений и добавьте Protocol из доступных протоколов).( MSDN Doc )
Здесь я использую " app-protocol " в качестве имени протокола.
Как только это будет сделано, вам необходимо переопределить метод OnActivation () в вашем App.xaml.CS .Этот метод будет вызываться при запуске приложения с использованием протокола.Аргументы, которые мы передаем при вызове протокола, могут быть найдены здесь, и на основе этого вы можете показать свою страницу или, возможно, передать этот параметр своей странице и позволить ей обрабатывать навигацию.
Например, если наш Uriapp-protocol:login?param1=true
, когда вы получите ProtocolActivatedEventArgs eventArgs
в методе onActivated()
, вы получите доступ ко всему Uri.
Вы можете использовать eventArgs.Uri
для доступа ко всем свойствам Uri.
В любом случае ваш код должен выглядеть примерно так:
C #
protected override void OnActivated(IActivatedEventArgs args)
{
if (args.Kind == ActivationKind.Protocol)
{
ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;
// Get the root frame
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();
rootFrame.NavigationFailed += OnNavigationFailed;
if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application
}
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
//URI : app-protocol:login?param1=true
//Logic for showing different pages/views based on parameters passed
if (eventArgs.Uri.PathAndQuery != string.Empty)//PathAndQuery:login?param1=true
{
var absolutePath = eventArgs.Uri.AbsolutePath;//AbsolutePath:login
if (absolutePath.Equals("login"))
{
rootFrame.Navigate(typeof(LoginPage));
}
else
{
rootFrame.Navigate(typeof(MainPage));
}
}
else
{
rootFrame.Navigate(typeof(MainPage));
}
}
// Ensure the current window is active
Window.Current.Activate();
}
Есть ли способ вывести приложение на экран, если оно было свернуто или скрыто за другими приложениями?
Мы звоним Window.Current.Activate();
, чтобы убедиться в этом.