A group blog from members of the VB team
I have experienced lot of fun creating Windows Phone 7 applications in Visual Basic and writing about them here. In our last post, I explained how to create a Bing Maps application for Windows Phone 7. In this blog, I want to share a sample that will help you to create a settings page for Windows Phone 7. This application will display two setting pages - one where the setting changes will be updated immediately, and the other where the user will have to confirm the changes.
I will now demonstrate how easy it is to create a settings page for Windows Phone 7, using Visual Basic for Windows Phone Developer Tools. The settings page can be created in 6 simple steps as follows:
Prerequisites:
To create a settings page for Windows Phone 7, let’s follow the 6 simple steps mentioned earlier:
Partial Public Class MainPage
Inherits PhoneApplicationPage
' Constructor
Public Sub New()
InitializeComponent()
End Sub
End Class
SupportedOrientations = SupportedPageOrientation.Portrait
' Add an Application Bar with a 'setting menu item.
ApplicationBar = New ApplicationBar()
ApplicationBar.IsMenuEnabled = True
ApplicationBar.IsVisible = True
ApplicationBar.Opacity = 1.0
Dim settingsItem As New ApplicationBarMenuItem("settings")
AddHandler settingsItem.Click, AddressOf settings_Click
ApplicationBar.MenuItems.Add(settingsItem)
Private Sub settings_Click(ByVal sender As Object, ByVal e As EventArgs)
Me.NavigationService.Navigate(New Uri("/SettingsWithoutConfirmation.xaml", UriKind.Relative))
Creating a settings class is one of the important tasks. This class is required to hold, save, and then load the settings from isolated storage. This class will contain a method to add or update a value to isolated storage. This method will also assign a default value if the setting has never been set earlier.
Imports System.IO.IsolatedStorage
Imports System.Diagnostics
Public Class AppSettings
' Our isolated storage settings
Private isolatedStore As IsolatedStorageSettings
' The isolated storage key names of our settings
Private Const CheckBoxSettingKeyName As String = "CheckBoxSetting"
Private Const ListBoxSettingKeyName As String = "ListBoxSetting"
Private Const RadioButton1SettingKeyName As String = "RadioButton1Setting"
Private Const RadioButton2SettingKeyName As String = "RadioButton2Setting"
Private Const RadioButton3SettingKeyName As String = "RadioButton3Setting"
Private Const UsernameSettingKeyName As String = "UsernameSetting"
Private Const PasswordSettingKeyName As String = "PasswordSetting"
' The default value of our settings
Private Const CheckBoxSettingDefault As Boolean = True
Private Const ListBoxSettingDefault As Integer = 0
Private Const RadioButton1SettingDefault As Boolean = True
Private Const RadioButton2SettingDefault As Boolean = False
Private Const RadioButton3SettingDefault As Boolean = False
Private Const UsernameSettingDefault As String = ""
Private Const PasswordSettingDefault As String = ""
''' Constructor that gets the application settings.
Try
' Get the settings for this application.
isolatedStore = IsolatedStorageSettings.ApplicationSettings
Catch e As Exception
Debug.WriteLine("Exception while using IsolatedStorageSettings: " & e.ToString())
End Try
''' Update a setting value for our application. If the setting does not
''' exist, then add the setting.
Public Function AddOrUpdateValue(ByVal Key As String, ByVal value As Object) As Boolean
Dim valueChanged As Boolean = False
' if new value is different, set the new value.
If isolatedStore(Key) IsNot value Then
isolatedStore(Key) = value
valueChanged = True
End If
Catch e1 As KeyNotFoundException
isolatedStore.Add(Key, value)
Catch e2 As ArgumentException
Return valueChanged
End Function
''' Get the current value of the setting, or if it is not found, set the
''' setting to the default setting.
Public Function GetValueOrDefault(Of valueType)(ByVal Key As String, ByVal defaultValue As valueType) As valueType
Dim value As valueType
value = CType(isolatedStore(Key), valueType)
value = defaultValue
Return value
''' Save the settings.
Public Sub Save()
isolatedStore.Save()
''' Property to get and set a CheckBox Setting Key.
Public Property CheckBoxSetting() As Boolean
Get
Return GetValueOrDefault(Of Boolean)(CheckBoxSettingKeyName, CheckBoxSettingDefault)
End Get
Set(ByVal value As Boolean)
AddOrUpdateValue(CheckBoxSettingKeyName, value)
Save()
End Set
End Property
''' Property to get and set a ListBox Setting Key.
Public Property ListBoxSetting() As Integer
Return GetValueOrDefault(Of Integer)(ListBoxSettingKeyName, ListBoxSettingDefault)
Set(ByVal value As Integer)
AddOrUpdateValue(ListBoxSettingKeyName, value)
''' Property to get and set a RadioButton Setting Key.
Public Property RadioButton1Setting() As Boolean
Return GetValueOrDefault(Of Boolean)(RadioButton1SettingKeyName, RadioButton1SettingDefault)
AddOrUpdateValue(RadioButton1SettingKeyName, value)
''' Property to get and set a RadioButton Setting Key. Public Property RadioButton2Setting() As Boolean Get Return GetValueOrDefault(Of Boolean)(RadioButton2SettingKeyName, RadioButton2SettingDefault) End Get Set(ByVal value As Boolean) AddOrUpdateValue(RadioButton2SettingKeyName, value) Save() End Set End Property ''' Property to get and set a RadioButton Setting Key. Public Property RadioButton3Setting() As Boolean Get Return GetValueOrDefault(Of Boolean)(RadioButton3SettingKeyName, RadioButton3SettingDefault) End Get Set(ByVal value As Boolean) AddOrUpdateValue(RadioButton3SettingKeyName, value) Save() End Set End Property ''' Property to get and set a Username Setting Key. Public Property UsernameSetting() As String Get Return GetValueOrDefault(Of String)(UsernameSettingKeyName, UsernameSettingDefault) End Get Set(ByVal value As String) AddOrUpdateValue(UsernameSettingKeyName, value) Save() End Set End Property ''' Property to get and set a Password Setting Key. Public Property PasswordSetting() As String Get Return GetValueOrDefault(Of String)(PasswordSettingKeyName, PasswordSettingDefault) End Get Set(ByVal value As String) AddOrUpdateValue(PasswordSettingKeyName, value) Save() End Set End Property End Class
Public Property RadioButton2Setting() As Boolean
Return GetValueOrDefault(Of Boolean)(RadioButton2SettingKeyName, RadioButton2SettingDefault)
AddOrUpdateValue(RadioButton2SettingKeyName, value)
Public Property RadioButton3Setting() As Boolean
Return GetValueOrDefault(Of Boolean)(RadioButton3SettingKeyName, RadioButton3SettingDefault)
AddOrUpdateValue(RadioButton3SettingKeyName, value)
''' Property to get and set a Username Setting Key.
Public Property UsernameSetting() As String
Return GetValueOrDefault(Of String)(UsernameSettingKeyName, UsernameSettingDefault)
Set(ByVal value As String)
AddOrUpdateValue(UsernameSettingKeyName, value)
''' Property to get and set a Password Setting Key.
Public Property PasswordSetting() As String
Return GetValueOrDefault(Of String)(PasswordSettingKeyName, PasswordSettingDefault)
AddOrUpdateValue(PasswordSettingKeyName, value)
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:local="clr-namespace:SettingsSample"
<phone:PhoneApplicationPage.Resources>
<local:AppSettings x:Key="appSettings"></local:AppSettings>
</phone:PhoneApplicationPage.Resources>
<CheckBox Content="CheckBox Setting" Height="Auto" HorizontalAlignment="Left" Margin="60,20,0,0" Name="checkBoxSetting" VerticalAlignment="Top" IsChecked="{Binding Source={StaticResource appSettings}, Path=CheckBoxSetting, Mode=TwoWay}" />
<ListBox Height="140" HorizontalAlignment="Left" Margin="70,150,0,0" Name="listBoxSetting" VerticalAlignment="Top" Width="360" SelectedIndex="{Binding Source={StaticResource appSettings}, Path=ListBoxSetting, Mode=TwoWay}">
<ListBoxItem Content="Times New Roman" FontSize="24" FontFamily="Times New Roman" />
<ListBoxItem Content="Arial" FontSize="24" FontFamily="Arial" />
<ListBoxItem Content="Comic Sans MS" FontSize="24" FontFamily="Comic Sans MS" />
</ListBox>
<RadioButton Content="Choice One" Height="Auto" HorizontalAlignment="Left" Margin="60,0,0,235" Name="radioButton1" VerticalAlignment="Bottom" GroupName="GroupOne" IsChecked="{Binding Source={StaticResource appSettings}, Path=RadioButton1Setting, Mode=TwoWay}" />
<RadioButton Content="Choice Two" Height="Auto" HorizontalAlignment="Left" Margin="60,350,0,0" Name="radioButton2" VerticalAlignment="Top" GroupName="GroupOne" IsChecked="{Binding Source={StaticResource appSettings}, Path=RadioButton2Setting, Mode=TwoWay}"/>
<RadioButton Content="Choice Three" Height="Auto" HorizontalAlignment="Left" Margin="60,400,0,0" Name="radioButton3" VerticalAlignment="Top" GroupName="GroupOne" IsChecked="{Binding Source={StaticResource appSettings}, Path=RadioButton3Setting, Mode=TwoWay}"/>
<Button Content="Settings With Confirmation" Height="80" HorizontalAlignment="Left" Margin="60,450,0,0" Name="buttonAdditional" Click="buttonAdditional_Click" />
<TextBlock Height="60" HorizontalAlignment="Left" Margin="65,12,0,0" Name="textBlock1" Text="Username" VerticalAlignment="Top" Width="169" />
<TextBox Height="78" HorizontalAlignment="Left" Margin="60,60,0,0" Name="textBoxUsername" VerticalAlignment="Top" Width="274" />
<TextBlock Height="60" HorizontalAlignment="Left" Margin="65,160,0,0" Name="textBlock2" Text="Password" VerticalAlignment="Top" Width="169" />
<PasswordBox Height="78" HorizontalAlignment="Left" Margin="60,208,0,0" Name="passwordBoxPassword" VerticalAlignment="Top" Width="274" />
Partial Public Class SettingsWithConfirmation
Private settings As New AppSettings()
' Add an Application Bar that has a 'done' confirmation button and
' a 'cancel' button
Dim doneButton As New ApplicationBarIconButton(New Uri("/Images/appbar.check.rest.png", UriKind.Relative))
doneButton.Text = "done"
AddHandler doneButton.Click, AddressOf doneButton_Click
Dim cancelButton As New ApplicationBarIconButton(New Uri("/Images/appbar.cancel.rest.png", UriKind.Relative))
cancelButton.Text = "cancel"
AddHandler cancelButton.Click, AddressOf cancelButton_Click
ApplicationBar.Buttons.Add(doneButton)
ApplicationBar.Buttons.Add(cancelButton)
' Copy the current settings into the text boxes as the new values
' entered in will not be saved until the user clicks the 'done' button.
textBoxUsername.Text = settings.UsernameSetting
passwordBoxPassword.Password = settings.PasswordSetting
Private Sub doneButton_Click(ByVal sender As Object, ByVal e As EventArgs)
settings.UsernameSetting = textBoxUsername.Text
settings.PasswordSetting = passwordBoxPassword.Password
NavigationService.GoBack()
Private Sub cancelButton_Click(ByVal sender As Object, ByVal e As EventArgs)
There you are! Now your settings page application for Windows Phone 7 is ready! You just need to build and debug the application.
Note: To stop debugging the application, select Debug > Stop Debugging.
Finally, to submit your application to the market place, you can refer to upload your application walkthrough.
Bravo! Buenos! You have successfully created a settings page for Windows Phone 7, that too in just 6 simple steps! You can find the full source code for the settings page application here. This application uses general Silverlight and Visual Basic features that are applicable for different application types including Windows Phone application.
This was great info!
<a href="http://bargainoutlet.org">Bargain Outlet</a>
Nice Info...
Please visit my blog in kangrangga.blogspot.com
Are the settings supposed to clear every time I run the app in the emulator?
@Josh yes because Isolated Storage is cleared out if you power off the emulator