Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Erfahren Sie, wie Sie die App-Aktivierung behandeln, indem Sie die Application.OnLaunched--Methode außer Kraft setzen.
Hinweis
Informationen zum Umgang mit der Aktivierung in einer Desktop-App finden Sie unter Abrufen von Aktivierungsinformationen für gepackte Apps. Siehe auch AppLifecycle – umfassende Aktivierung auf GitHub.
Starthandler überschreiben
Wenn eine App aktiviert wird, sendet das System aus irgendeinem Grund das CoreApplicationView.Activated-Ereignis. Eine Liste der Aktivierungstypen finden Sie in der ActivationKind Aufzählung.
Die Windows.UI.Xaml.Application-Klasse definiert Methoden, die Sie überschreiben können, um die verschiedenen Aktivierungstypen zu behandeln. Einige der Aktivierungstypen weisen eine bestimmte Methode auf, die Sie außer Kraft setzen können. Überschreiben Sie für die anderen Aktivierungstypen die OnActivated-Methode.
Definieren Sie die Klasse für Ihre Anwendung.
<Application
x:Class="AppName.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
Überschreiben Sie die Methode OnLaunched. Diese Methode wird immer aufgerufen, wenn der Benutzer die App startet. Der Parameter LaunchActivatedEventArgs enthält den vorherigen Zustand Ihrer App und die Aktivierungsargumente.
Hinweis
Unter Windows ruft das Starten einer angehaltenen App über die Startkachel oder App-Liste diese Methode nicht auf.
using System;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;
namespace AppName
{
public partial class App
{
async protected override void OnLaunched(LaunchActivatedEventArgs args)
{
EnsurePageCreatedAndActivate();
}
// Creates the MainPage if it isn't already created. Also activates
// the window so it takes foreground and input focus.
private MainPage EnsurePageCreatedAndActivate()
{
if (Window.Current.Content == null)
{
Window.Current.Content = new MainPage();
}
Window.Current.Activate();
return Window.Current.Content as MainPage;
}
}
}
Class App
Protected Overrides Sub OnLaunched(args As LaunchActivatedEventArgs)
Window.Current.Content = New MainPage()
Window.Current.Activate()
End Sub
End Class
...
#include "MainPage.h"
#include "winrt/Windows.ApplicationModel.Activation.h"
#include "winrt/Windows.UI.Xaml.h"
#include "winrt/Windows.UI.Xaml.Controls.h"
...
using namespace winrt;
using namespace Windows::ApplicationModel::Activation;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
struct App : AppT<App>
{
App();
/// <summary>
/// Invoked when the application is launched normally by the end user. Other entry points
/// will be used such as when the application is launched to open a specific file.
/// </summary>
/// <param name="e">Details about the launch request and process.</param>
void OnLaunched(LaunchActivatedEventArgs const& e)
{
Frame rootFrame{ nullptr };
auto content = Window::Current().Content();
if (content)
{
rootFrame = content.try_as<Frame>();
}
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == nullptr)
{
// Create a Frame to act as the navigation context and associate it with
// a SuspensionManager key
rootFrame = Frame();
rootFrame.NavigationFailed({ this, &App::OnNavigationFailed });
if (e.PreviousExecutionState() == ApplicationExecutionState::Terminated)
{
// Restore the saved session state only when appropriate, scheduling the
// final launch steps after the restore is complete
}
if (e.PrelaunchActivated() == false)
{
if (rootFrame.Content() == nullptr)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
rootFrame.Navigate(xaml_typename<BlankApp1::MainPage>(), box_value(e.Arguments()));
}
// Place the frame in the current Window
Window::Current().Content(rootFrame);
// Ensure the current window is active
Window::Current().Activate();
}
}
else
{
if (e.PrelaunchActivated() == false)
{
if (rootFrame.Content() == nullptr)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
rootFrame.Navigate(xaml_typename<BlankApp1::MainPage>(), box_value(e.Arguments()));
}
// Ensure the current window is active
Window::Current().Activate();
}
}
}
};
using namespace Windows::ApplicationModel::Activation;
using namespace Windows::Foundation;
using namespace Windows::UI::Xaml;
using namespace AppName;
void App::OnLaunched(LaunchActivatedEventArgs^ args)
{
EnsurePageCreatedAndActivate();
}
// Creates the MainPage if it isn't already created. Also activates
// the window so it takes foreground and input focus.
void App::EnsurePageCreatedAndActivate()
{
if (_mainPage == nullptr)
{
// Save the MainPage for use if we get activated later
_mainPage = ref new MainPage();
}
Window::Current->Content = _mainPage;
Window::Current->Activate();
}
Wiederherstellen von Anwendungsdaten, wenn die App angehalten und dann beendet wurde
Wenn der Benutzer zu Ihrer beendeten App wechselt, sendet das System das Ereignis Activated, wobei der Typ auf Launch festgelegt ist und der VorherigeAusführungszustand auf Beendet oder VomBenutzerGeschlossenfestgelegt ist. Die App sollte die gespeicherten Anwendungsdaten laden und den angezeigten Inhalt aktualisieren.
async protected override void OnLaunched(LaunchActivatedEventArgs args)
{
if (args.PreviousExecutionState == ApplicationExecutionState.Terminated ||
args.PreviousExecutionState == ApplicationExecutionState.ClosedByUser)
{
// TODO: Populate the UI with the previously saved application data
}
else
{
// TODO: Populate the UI with defaults
}
EnsurePageCreatedAndActivate();
}
Protected Overrides Sub OnLaunched(args As Windows.ApplicationModel.Activation.LaunchActivatedEventArgs)
Dim restoreState As Boolean = False
Select Case args.PreviousExecutionState
Case ApplicationExecutionState.Terminated
' TODO: Populate the UI with the previously saved application data
restoreState = True
Case ApplicationExecutionState.ClosedByUser
' TODO: Populate the UI with the previously saved application data
restoreState = True
Case Else
' TODO: Populate the UI with defaults
End Select
Window.Current.Content = New MainPage(restoreState)
Window.Current.Activate()
End Sub
void App::OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs const& e)
{
if (e.PreviousExecutionState() == ApplicationExecutionState::Terminated ||
e.PreviousExecutionState() == ApplicationExecutionState::ClosedByUser)
{
// Populate the UI with the previously saved application data.
}
else
{
// Populate the UI with defaults.
}
...
}
void App::OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ args)
{
if (args->PreviousExecutionState == ApplicationExecutionState::Terminated ||
args->PreviousExecutionState == ApplicationExecutionState::ClosedByUser)
{
// TODO: Populate the UI with the previously saved application data
}
else
{
// TODO: Populate the UI with defaults
}
EnsurePageCreatedAndActivate();
}
Wenn der Wert von PreviousExecutionStateNotRunningist, konnte die App ihre Anwendungsdaten nicht erfolgreich speichern, und die App sollte neu gestartet werden, als ob sie ursprünglich gestartet wurde.
Bemerkungen
Hinweis
Apps können die Initialisierung überspringen, wenn bereits Inhalte im aktuellen Fenster festgelegt sind. Sie können die LaunchActivatedEventArgs.TileId-Eigenschaft überprüfen, um zu bestimmen, ob die App von einer primären oder einer sekundären Kachel gestartet wurde, und basierend auf diesen Informationen entscheiden, ob Sie ein neues oder fortgesetztes App-Erlebnis bieten sollten.