2014-08-28 51 views

回答

0

好了,所以你有10頁,每一頁上要更改通過設置菜單這些網頁的背景顏色。你可以做的是使用Windows Phone IsolatedStorageSettings

首先你要初始化IsolatedStorageSettings。你可以這樣做:

IsolatedStorageSettings MyAppSettings = IsolatedStorageSettings.ApplicationSettings; 

然後你將不得不爲它設置一個默認值,所以它不會引發異常。你可以這樣做:

MyAppSettings.Add("PageBackgroundColor", "#000000"); // you can set whatever the default colour you want here. i.e. Black 

最好的地方,我覺得這是是在添加以下代碼:

private void Application_Launching(object sender, LaunchingEventArgs e) 
{ 

if (IsolatedStorageSettings.ApplicationSettings.Contains("PageBackgroundColor")) 
     { 
      // Don't do anything because you've already set the default background colour for the pages 
     } 
     else 
     { 
      // add the default color 
     } 
} 

現在在你的你的MainPage可以重新初始化IsolatedStorageSettings。完成之後,您將需要獲取設置的值,並根據您希望更改背景顏色的值進行設置。要讀取值:

string Sortval = (string)MyAppSettings["PageBackgroundColor"]; 

可以在補充一點:

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 

} 

或者

public MainPage 
{ 
    InitializeComponent(); 
} 

Remember that public MainPage will only run once and the OnNavigatedTo runs every time the page is loaded so if you want to update the background color right after adding the setting, OnNavigatedTo is the way to go but if you want to apply the changes after a restart, public Mainpage is it.

現在閱讀的價值和改變它,你想要做一些事情:

string val = (string)MyAppSettings["PageBackgroundColor"]; 
if (val == "#000000") 
{ 
    //change to black 
} 
else if (val == "your hex color") 
{ 
    //change to whatever color 
} 
else if (val == "another hex color") 
{ 
    //... 
} 

現在保存的價值要重新初始化IsolatedStorageSettings您的設置頁面,並保存它會是這樣的值:

MyAppSettings.Remove("PageBackgroundColor"); 
MyAppSettings.Add("PageBackgroundColor", "your hex color"); 
MyAppSettings.Save(); 

但是,這是未經測試應該給你非常關於如何在保存和加載設置上應用它然後應用它的基本想法