[英]Boolean Resets When App Loads Wp8
嗨,我還是C#和Windows Phone的新手。
應用加載時,我想彈出窗口詢問用戶是否想要做某事
MessageBoxResult m = MessageBox.Show("Info.", "Question?", MessageBoxButton.OKCancel);
if (m == MessageBoxResult.Cancel)
{ }
else if (m == MessageBoxResult.OK)
{ //Do Something }
現在可以正常工作了,如果用戶說不,我想要一個彈出窗口詢問用戶是否想在下次提醒,所以U
MessageBoxResult m = MessageBox.Show("Info.", "Question?", MessageBoxButton.OKCancel);
if (m == MessageBoxResult.Cancel)
{
MessageBoxResult r = MessageBox.Show("", "Would You Like Reminding Next Time ?",MessageBoxButton.OKCancel);
if (r == MessageBoxResult.Cancel)
{ }
else if (r == MessageBoxResult.OK)
{ }
}
else if (m == MessageBoxResult.OK)
{ //Do Something }
我需要某種開關,因此當應用程序首次啟動時,如果應用程序檢查開關處於打開狀態,則會向他們詢問問題
如果回答取消,則會詢問是否要提醒
如果他們回答“否”,請將開關設置為關閉
我嘗試使用布爾值,但是當應用程序關閉時,它只是重置為true,如果我使用字符串,它說字符串不能用作布爾值
有什么建議嗎?
例如,使用IsolatedStorageSettings.ApplicationSettings快速保存較小的值
// this will save my "your_key" to false;
IsolatedStorageSettings.ApplicationSettings.Add("your_key", false);
IsolatedStorageSettings.ApplicationSettings.Save(); // make sure you call save
// so the next time the app runs I can get it back doing this
bool your_key = (bool) IsolatedStorageSettings.ApplicationSettings["your_key"];
但是,應始終將其包含在try catch中,因為該密鑰可能不存在
bool your_key = false; // or default value
try
{
your_key = (bool) IsolatedStorageSettings.ApplicationSettings["your_key"];
}
catch(Exception ex)
{
}
更多信息可以在這里找到:
if(!IsolatedStorageSettings.ApplicationSettings.Contains("first"))
{
// Do your stuff
IsolatedStorageSettings.ApplicationSettings["first"] = true;
IsolatedStorageSettings.ApplicationSettings.Save();
}
這就是您需要的所有代碼。 僅在首次啟動時將要執行的所有操作放入此if語句。 然后在主頁Loaded
event或OnNavigatedTo
執行此代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.