Chúng tôi đã gặp phải vấn đề tương tự khi chuyển đổi dự án cũ và cần thiết để có một số quyền tự do với việc làm kinh ngạc. Tất cả chúng ta đều tương đối mới đối với nền tảng này nên hãy lấy lời khuyên này bằng một hạt muối. Trang khởi động của chúng tôi là SplashPage.xaml. Chúng tôi sử dụng các UriMapper để chuyển hướng từ các nguồn hiện lần cuối:
private void Application_Activated(object sender, ActivatedEventArgs e) {
IsTombstoned = ! e.IsApplicationInstancePreserved;
if (IsTombstoned) {
//the os wants to return to the last page, but we want it to restart to our splash page
RootFrame.UriMapper = new RestartUriMapper();
}
}
này chuyển hướng đến các SplashPage.xaml và sau đó chúng tôi chuyển hướng khác để xóa rằng trang cuối cùng mà hệ điều hành muốn đi (có thể là duy nhất để chúng tôi thực hiện chuyển hướng.)
protected override void OnNavigatedTo(NavigationEventArgs e) {
base.OnNavigatedTo(e);
if (NavigationContext.QueryString.ContainsKey("restart")) {
var app = Application.Current as App;
//a page redirect mapper was installed to get here from tombstone - reinstate the AssociationUriMapper now
app.RootFrame.UriMapper = App.Root.AssociationUriMapper;
//from tombstone, the last current nav source is still state, so force an initial navigation back to
//a new instance of this splash page and proceed start up from there
App.PageNavigation.Navigate(@"/Pages/SplashPage.xaml?fromtomb=true");
}
}
class RestartUriMapper : UriMapperBase {
Uri restartUri;
public RestartUriMapper() {
restartUri = new Uri(string.Format("/Pages/SplashPage.xaml?restart={0}", true.ToString()), UriKind.Relative);
}
public override Uri MapUri(Uri uri) {
if (restartUri != null) {
Uri nextPageUri = restartUri;
restartUri = null;
return nextPageUri;
}
return uri;
}
}
Để tham khảo, nhiều ứng dụng (đặc biệt là trò chơi) có sẵn trên thị trường không tái lập trạng thái sau khi bị vô hiệu hóa. Đây là một đề xuất, không phải là một quy tắc. Nhưng có, với tư cách là người dùng, nó rất bực bội khi một ứng dụng không lưu trạng thái. –