MessageBox 显示一次

MessageBox Display Once

我想知道是否有办法在 WP8 中仅显示一次消息框,即在应用程序打开时。

我已经有了下面的代码,非常基础。

protected override void OnNavigatedTo(NavigationEventArgs e)
{
  base.OnNavigatedTo(e);
  MessageBox.Show("Hi");
}

但是,每次打开应用程序时都会显示。我只希望它第一次显示。

这可能吗?

由于您需要跨会话保持状态,因此 isolated storage 键值对是一个不错的选择。刚查过,再更新:

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;
  }
}

我已经在 WP 8.0 Silverlight 应用程序中成功使用了它。创建可重复使用的 class, 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();
        }
    }
}

然后在您的应用中的任何位置使用它,如下所示:

OneTimeDialog.Show("WelcomeDialog", "Welcome", "Welcome to my app! You'll only see this once.")

只显示一次 "Hint" 或 "Welcome" 对话框对很多不同类型的应用程序都有帮助,所以我实际上在便携式 Class 库中有上面的代码,所以我可以从多个项目中引用它。