2013-11-27 57 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) 结束如果'但我似乎得到一个空引用错误。我只需要知道设置是否实际应用。这全部添加到导航事件的主页上。去检查。 –