2017-02-25 107 views
-1

在XAML我創建按鈕,當我點擊按鈕,然後背景將被改爲紅色(因爲我設置了它),但如果我關閉我的應用程序,然後再次啓動應用程序,所以背景不是紅色。我需要做的就是保持背景像以前一樣(當我點擊按鈕),所以有紅色背景,如果我再次啓動應用程序。你的幫助對我來說非常重要。謝謝專家。順便說一句,這是現在的按鈕代碼::)。UWP - 保存項目

private void MenuFlyoutItem_Click(object sender, RoutedEventArgs e) 
    { 
     background.Background = new SolidColorBrush(Colors.Red); 
    } 
+0

只儲存它在App設置:https://docs.microsoft.com/en-us/windows/uwp/應用程序設置/應用程序設置和數據 – UnholySheep

回答

0

您需要保存選定的顏色;或者節省一些,你可以確定它必須是什麼顏色。在構造函數或頁面中讀取該值並設置正確的顏色。

你可以使用localsettings來保存這個。

ApplicationData.Current.LocalSettings.Values["BGColor"] = "Red"; 

例如。

在構造函數中:

if (ApplicationData.Current.LocalSettings.Values["BGColor"] == "Red") 
{ 
background.Background = new SolidColorBrush(Colors.Red); 
} 

全樣本:

public MainPage() 
     { 
      this.InitializeComponent(); 

      if ((string)ApplicationData.Current.LocalSettings.Values["BGColor"] == "Red") 
       LayoutRoot.Background = new SolidColorBrush(Colors.Red); 

     } 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      ApplicationData.Current.LocalSettings.Values["BGColor"] = "Red"; 
      LayoutRoot.Background = new SolidColorBrush(Colors.Red); 
     } 
+0

完全同意..但沒有足夠的上下文來建議更好的東西或需要在示例中定義爲枚舉。所以你完全有效的點 –

+0

男人謝謝你的怒火,但它會是非常好的,如果你能告訴我更多。 – theoodd

+0

我寫了你的例子在我的應用程序的構造函數興奮地相同,但它不工作。 – theoodd