2013-11-27 60 views
0

所以我創建了一個名爲「設置」的頁面。很明顯,在這個頁面中是應用程序的設置。在設置頁面中,我添加了2個ToggleSwitches和1個Listpicker。使用諾基亞開發者網站關於保存和讀取設置的基本知識,我設法將其關閉,以便保存切換開關和列表選擇器的狀態。WP7 - 添加設置頁面。從不同的頁面讀取值?

我現在遇到的問題是,我需要一種方式來讀取應用程序啓動時第一頁上保存的這些設置值,以便它可以相應地準備應用程序。洙到目前爲止,這是我在設置頁面:

Imports System.IO.IsolatedStorage 
Partial Public Class Settings 
Inherits PhoneApplicationPage 
Private AppSettings As IsolatedStorageSettings 
Public Sub New() 
    InitializeComponent() 
    AppSettings = IsolatedStorageSettings.ApplicationSettings 
    ListPicker1.Items.Add("Saved Notes") 
    ListPicker1.Items.Add("Important") 
End Sub 
Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs) 
    Try 
     Tg1.IsChecked = CBool(AppSettings("UseAccentColor")) 
     Tg2.IsChecked = CBool(AppSettings("GoBack")) 
     ListPicker1.SelectedIndex = CByte(AppSettings("StartListFalse")) 
    Catch ex As KeyNotFoundException 
     AppSettings.Add("UseAccentColor", False) 
     AppSettings.Add("GoBack", False) 
     AppSettings.Add("StartListFalse", False) 
     AppSettings.Save() 
    End Try 
End Sub 
Protected Overrides Sub OnNavigatedFrom(e As NavigationEventArgs) 
    System.Diagnostics.Debug.WriteLine("Exiting, so save now") 
    AppSettings("UseAccentColor") = Tg1.IsChecked 
    AppSettings("GoBack") = Tg2.IsChecked 
    AppSettings("StartListFalse") = ListPicker1.SelectedIndex 
    AppSettings.Save() 
End Sub 
End Class 

所以洙到目前爲止,它節省了退出,但我需要一種方法來啓動,即我的加載的MainPage這些。就像參考這個頁面的方法一樣,根據這些設置改變需要改變的任何需求。

我怎樣才能做到這一點? 謝謝!

回答

1

您設法將設置保存到IsolatedStorage,並且IsolatedStorage可以從應用程序的任何頁面訪問。因此在MainPage中,只需從IsolatedStorage中讀取這些設置,而不是Settings頁面。

編輯: 你可以做到這一點,就像在的OnNavigatedTo方法設置頁

Private AppSettings As IsolatedStorageSettings = IsolatedStorageSettings.ApplicationSettings 
'Tg1.IsChecked is analog with useAccentColor 
Dim useAccentColor As Boolean = CBool(AppSettings("UseAccentColor")) 
'Tg2.IsChecked = goBack 
Dim goBack As Boolean = CBool(AppSettings("GoBack")) 
'ListPicker1.SelectedIndex = startListFalse 
Dim startListFalse As Byte = CByte(AppSettings("StartListFalse")) 
+0

你有任何想法如何,我可以做到這一點?因爲當我試圖把代碼放在MainPages onnavigated事件時,我得到了很多'沒有聲明,因爲控件在不同的頁面。 –

+0

我加了一些代碼(雖然我在VB中不是很流暢)。這些設置從IsolatedStorage中讀取,並存儲在變量中。然後,您可以相應地準備應用程序。 – har07

+0

謝謝你,但現在我已經將該代碼添加到主頁我將如何知道是否實際檢查了切換開關(Tg1,Tg2)?我添加了:'Dim mp = TryCast(DirectCast(Application.Current,App).RootFrame.Content,Settings) If mp.Tg1.IsChecked = True Then MessageBox.Show(「」,「」,MessageBoxButton.OK) 結束如果'但我似乎得到一個空引用錯誤。我只需要知道設置是否實際應用。這全部添加到導航事件的主頁上。去檢查。 –