[英]MessageBox Display Once
我想知道是否可以在WP8中一次顯示消息框,即在打開應用程序時。
我已經有以下代碼,非常基本。
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
MessageBox.Show("Hi");
}
但是,每次打開應用程序時都會顯示。 我只希望它第一次顯示。
那可能嗎?
因為您需要在會話之間保持狀態,所以隔離存儲鍵值對是一個不錯的選擇。 只需檢查一下,然后再更新:
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應用程序中成功使用了此功能。 創建一個可重用的類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.")
一次顯示“提示”或“歡迎”對話框對許多不同類型的應用程序很有幫助,因此我實際上在可移植類庫中具有上面的代碼,因此可以從多個項目中引用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.