gpt4 book ai didi

c# - 快速应用恢复不工作

转载 作者:太空宇宙 更新时间:2023-11-03 13:13:14 24 4
gpt4 key购买 nike

我正在尝试为 Windows Phone 8 实现 Fast app resume。我关注了 the link at MSDN .

这是 XAML 中的代码:

<Tasks>
<DefaultTask Name="_default" NavigationPage="MainPage.xaml" ActivationPolicy="Resume"/>
</Tasks>

这是 app.xaml.cs 中的代码

    public static PhoneApplicationFrame RootFrame { get; private set; }
bool wasRelaunched = false;
bool mustClearPagestack = false;
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

enum SessionType
{
None,
Home,
DeepLink
}

private SessionType sessionType = SessionType.None;

public App()
{
UnhandledException += Application_UnhandledException;
InitializeComponent();
InitializePhoneApplication();
InitializeLanguage();
if (Debugger.IsAttached)
{
Application.Current.Host.Settings.EnableFrameRateCounter =true;
PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
}
}

private void Application_Launching(object sender, LaunchingEventArgs e)
{
RemoveCurrentDeactivationSettings();
}

private void Application_Activated(object sender, ActivatedEventArgs e)
{

mustClearPagestack = CheckDeactivationTimeStamp();

if (!e.IsApplicationInstancePreserved)
{
RestoreSessionType();
}
}

private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
SaveCurrentDeactivationSettings();
}

private void Application_Closing(object sender, ClosingEventArgs e)
{
RemoveCurrentDeactivationSettings();
}

private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
if (Debugger.IsAttached)
{

Debugger.Break();
}
}

private void Application_UnhandledException(object sender,ApplicationUnhandledExceptionEventArgs e)
{
if (Debugger.IsAttached)
{

Debugger.Break();
}
}

private bool phoneApplicationInitialized = false;

private void InitializePhoneApplication()
{
if (phoneApplicationInitialized)
return;


RootFrame= new PhoneApplicationFrame();

RootFrame.Background = new SolidColorBrush(Color.FromArgb(255, 27, 200, 174));
RootFrame.Navigated += CompleteInitializePhoneApplication;


RootFrame.NavigationFailed += RootFrame_NavigationFailed;


RootFrame.Navigated += CheckForResetNavigation;
RootFrame.Navigating += RootFrame_Navigating;


phoneApplicationInitialized = true;
}

void RootFrame_Navigating(object sender, NavigatingCancelEventArgs e)
{
if (e.Uri.ToString().Contains(@"/MainPage.xaml") == true && !AppPrefManager.Instance.IsFastAppResumeEnabled)
{
RootFrame.Dispatcher.BeginInvoke(delegate
{
if (!AppPrefManager.Instance.IsVirginLaunchCompleted)
{
RootFrame.Navigate(new Uri(Constants.kIntroPage, UriKind.Relative));
}
else
{
RootFrame.Navigate(new Uri(Constants.kMainPage, UriKind.Relative));
}
});
e.Cancel = true;
}


if (sessionType == SessionType.None && e.NavigationMode == NavigationMode.New)
{

if (e.Uri.ToString().Contains("DeepLink=true"))
{
sessionType = SessionType.DeepLink;
}
else if (e.Uri.ToString().Contains("/MainPage.xaml"))
{
sessionType = SessionType.Home;
}
}


if (e.NavigationMode == NavigationMode.Reset)
{

wasRelaunched = true;
}
else if (e.NavigationMode == NavigationMode.New && wasRelaunched)
{

wasRelaunched = false;

if (e.Uri.ToString().Contains("DeepLink=true"))
{


sessionType = SessionType.DeepLink;

}
else if (e.Uri.ToString().Contains("/MainPage.xaml"))
{

if (sessionType == SessionType.DeepLink)
{

sessionType = SessionType.Home;
}
else
{
if (!mustClearPagestack)
{

e.Cancel = true;
RootFrame.Navigated -= ClearBackStackAfterReset;
}
}
}

mustClearPagestack = false;
}
}

private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
{

if (RootVisual != RootFrame)
RootVisual = RootFrame;

RootFrame.Navigated -= CompleteInitializePhoneApplication;
}

private void CheckForResetNavigation(object sender, NavigationEventArgs e)
{

if (e.NavigationMode == NavigationMode.Reset)
RootFrame.Navigated += ClearBackStackAfterReset;
}

private void ClearBackStackAfterReset(object sender, NavigationEventArgs e)
{

RootFrame.Navigated -= ClearBackStackAfterReset;


if (e.NavigationMode != NavigationMode.New && e.NavigationMode != NavigationMode.Refresh)
return;


while (RootFrame.RemoveBackEntry() != null)
{
;
}
}

private void InitializeLanguage()
{
try
{
FlowDirection flow = (FlowDirection)Enum.Parse(typeof(FlowDirection), AppResources.ResourceFlowDirection);
RootFrame.FlowDirection = flow;
}
catch
{
if (Debugger.IsAttached)
{
Debugger.Break();
}

throw;
}
}

bool CheckDeactivationTimeStamp()
{
DateTimeOffset lastDeactivated;

if (settings.Contains("DeactivateTime"))
{
lastDeactivated = (DateTimeOffset)settings["DeactivateTime"];
}

var currentDuration = DateTimeOffset.Now.Subtract(lastDeactivated);

return TimeSpan.FromSeconds(currentDuration.TotalSeconds) > TimeSpan.FromSeconds(30);
}

public bool AddOrUpdateValue(string Key, Object value)
{
bool valueChanged = false;


if (settings.Contains(Key))
{

if (settings[Key] != value)
{

settings[Key] = value;
valueChanged = true;
}
}

else
{
settings.Add(Key, value);
valueChanged = true;
}
return valueChanged;
}

public void RemoveValue(string Key)
{

if (settings.Contains(Key))
{
settings.Remove(Key);
}
}

public void SaveCurrentDeactivationSettings()
{
if (AddOrUpdateValue("DeactivateTime", DateTimeOffset.Now))
{
settings.Save();
}

if (AddOrUpdateValue("SessionType", sessionType))
{
settings.Save();
}

}

public void RemoveCurrentDeactivationSettings()
{
RemoveValue("DeactivateTime");
RemoveValue("SessionType");
settings.Save();
}

void RestoreSessionType()
{
if (settings.Contains("SessionType"))
{
sessionType = (SessionType)settings["SessionType"];
}
}

假设我在 ThirdPage 中。我按下 Windows 按钮。然后我从开始屏幕按我的应用程序图标。而不是应用程序从 ThirdPage 恢复。它首先显示 ThirdPage,然后从 MainPage 开始。

最佳答案

默认情况下,当应用程序通过应用程序磁贴启动时,应用程序仍会导航到默认页面。如果愿意,您可以在 RootFrame_Navigated 方法中检查 session 类型并取消该导航。

默认模板在 app.xaml.cs 中添加了一个 CheckNavigation 方法,该方法在使用 NavigationMode Reset 导航后清除后台堆栈。

您可以在那里检查您的应用是否应该停留在最后一页,或者是否最好重置并重新开始。

可以在此处找到处理不同激活类型的示例: MSDN Fast Resume Sample, App.xaml.cs

(方法:RootFrame_Navigated)

关于c# - 快速应用恢复不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27567852/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com