[英]MessageBox Display Once
I was wondering if there was a way to display a messagebox in WP8 just once ie on app opening. 我想知道是否可以在WP8中一次显示消息框,即在打开应用程序时。
I have the following code already, very basic. 我已经有以下代码,非常基本。
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
MessageBox.Show("Hi");
}
However, this shows every time the app is opened. 但是,每次打开应用程序时都会显示。 I only want it to show the first time.
我只希望它第一次显示。
Is that possible? 那可能吗?
Since you need to persist a state across sessions, an isolated storage key-value pair is a good choice. 因为您需要在会话之间保持状态,所以隔离存储键值对是一个不错的选择。 Just check before, and then update:
只需检查一下,然后再更新:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
var settings = IsolatedStorageSettings.ApplicationSettings;
if (settings.ContainsKey("messageShown") && (bool)settings["messageShown"] == true)
{
MessageBox.Show("Hi");
settings["messageShown"] = true;
}
}
I have used this successfully in WP 8.0 Silverlight apps. 我已经在WP 8.0 Silverlight应用程序中成功使用了此功能。 Create a reusable class, OneTimeDialog:
创建一个可重用的类OneTimeDialog:
using System.Windows;
using System.IO.IsolatedStorage;
namespace MyApp
{
public static class OneTimeDialog
{
private static readonly IsolatedStorageSettings _settings = IsolatedStorageSettings.ApplicationSettings;
public static void Show(string uniqueKey, string title, string message)
{
if (_settings.Contains(uniqueKey)) return;
MessageBox.Show(message, title, MessageBoxButton.OK);
_settings.Add(uniqueKey, true);
_settings.Save();
}
}
}
Then use it anywhere in your app, like this: 然后在应用程序中的任何地方使用它,如下所示:
OneTimeDialog.Show("WelcomeDialog", "Welcome", "Welcome to my app! You'll only see this once.")
Showing a "Hint" or "Welcome" dialog just once is helpful in lots of different types of apps, so I actually have the code above in a Portable Class Library so I can reference it from multiple projects. 一次显示“提示”或“欢迎”对话框对许多不同类型的应用程序很有帮助,因此我实际上在可移植类库中具有上面的代码,因此可以从多个项目中引用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.