zoukankan      html  css  js  c++  java
  • Windows Phone学习笔记(10) — — 设置页面

      要想对页面进行某些设置就必须把设置存储起来,那么在WP中我们可以把这些信息存储在手机的独立存储中,并且从中获取和设置。想要用到独立存储的键值对设置就要用到IsolatedStorageSettings类。下面是一个存储页面设置的小示例。首先我们先创建一个类用来存储和获取信息。AppSettings:

    View Code
        public class AppSettings
        {
    
            // 独立存储设置
            IsolatedStorageSettings isolatedStore;
    
            //独立存储的key名称的设置
            const string CheckBoxSettingKeyName = "CheckBoxSetting";
            const string ListBoxSettingKeyName = "ListBoxSetting";
            const string RadioButton1SettingKeyName = "RadioButton1Setting";
            const string RadioButton2SettingKeyName = "RadioButton2Setting";
            const string RadioButton3SettingKeyName = "RadioButton3Setting";
            const string UsernameSettingKeyName = "UsernameSetting";
            const string PasswordSettingKeyName = "PasswordSetting";
    
            // 我们设置的默认值
            const bool CheckBoxSettingDefault = true;
            const int ListBoxSettingDefault = 0;
            const bool RadioButton1SettingDefault = true;
            const bool RadioButton2SettingDefault = false;
            const bool RadioButton3SettingDefault = false;
            const string UsernameSettingDefault = "";
            const string PasswordSettingDefault = "";
    
            /// <summary>
            /// 构造函数获取应用程序的设置。
            /// </summary>
            public AppSettings()
            {
                try
                {
                    // 获取该应用程序的设置。Get the settings for this application.
                    isolatedStore = IsolatedStorageSettings.ApplicationSettings;
    
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString());
                }
            }
    
            /// <summary>
            /// 更新我们的应用程序的设置值。如果设置不存在,则添加设置。
            /// </summary>
            /// <param name="Key"></param>
            /// <param name="value"></param>
            /// <returns></returns>
            public bool AddOrUpdateValue(string Key, Object value)
            {
                bool valueChanged = false;
    
                // 如果键值存在的话
                if (isolatedStore.Contains(Key))
                {
                    // 如果值改变了的话
                    if (isolatedStore[Key] != value)
                    {
                        // 存储新的值
                        isolatedStore[Key] = value;
                        valueChanged = true;
                    }
                }
                // 否则创建键值
                else
                {
                    isolatedStore.Add(Key, value);
                    valueChanged = true;
                }
    
                return valueChanged;
            }
    
    
            /// <summary>
            /// 获得当前值的设置,或如果它没有发现,设置为默认设置。
            /// </summary>
            /// <typeparam name="valueType"></typeparam>
            /// <param name="Key"></param>
            /// <param name="defaultValue"></param>
            /// <returns></returns>
            public valueType GetValueOrDefault<valueType>(string Key, valueType defaultValue)
            {
                valueType value;
    
                // 如果键存在,检索值。
                if (isolatedStore.Contains(Key))
                {
                    value = (valueType)isolatedStore[Key];
                }
                // 否则,使用默认值。
                else
                {
                    value = defaultValue;
                }
    
                return value;
            }
    
    
            /// <summary>
            /// 保存设置。
            /// </summary>
            public void Save()
            {
                isolatedStore.Save();
            }
    
    
            /// <summary>
            /// 属性来获取和设置一个复选框设置关键。
            /// </summary>
            public bool CheckBoxSetting
            {
                get
                {
                    return GetValueOrDefault<bool>(CheckBoxSettingKeyName, CheckBoxSettingDefault);
                }
                set
                {
                    AddOrUpdateValue(CheckBoxSettingKeyName, value);
                    Save();
                }
            }
    
    
            /// <summary>
            /// 属性来获取和设置一个列表框设定键。
            /// </summary>
            public int ListBoxSetting
            {
                get
                {
                    return GetValueOrDefault<int>(ListBoxSettingKeyName, ListBoxSettingDefault);
                }
                set
                {
                    AddOrUpdateValue(ListBoxSettingKeyName, value);
                    Save();
                }
            }
    
    
            /// <summary>
            /// 属性来获取和设置一个RadioButton设定键。
            /// </summary>
            public bool RadioButton1Setting
            {
                get
                {
                    return GetValueOrDefault<bool>(RadioButton1SettingKeyName, RadioButton1SettingDefault);
                }
                set
                {
                    AddOrUpdateValue(RadioButton1SettingKeyName, value);
                    Save();
                }
            }
    
    
            /// <summary>
            /// 属性来获取和设置一个RadioButton设定键。
            /// </summary>
            public bool RadioButton2Setting
            {
                get
                {
                    return GetValueOrDefault<bool>(RadioButton2SettingKeyName, RadioButton2SettingDefault);
                }
                set
                {
                    AddOrUpdateValue(RadioButton2SettingKeyName, value);
                    Save();
                }
            }
    
            /// <summary>
            /// 属性来获取和设置一个RadioButton设定键。
            /// </summary>
            public bool RadioButton3Setting
            {
                get
                {
                    return GetValueOrDefault<bool>(RadioButton3SettingKeyName, RadioButton3SettingDefault);
                }
                set
                {
                    AddOrUpdateValue(RadioButton3SettingKeyName, value);
                    Save();
                }
            }
    
            /// <summary>
            /// 属性来获取和设置用户名设置关键。
            /// </summary>
            public string UsernameSetting
            {
                get
                {
                    return GetValueOrDefault<string>(UsernameSettingKeyName, UsernameSettingDefault);
                }
                set
                {
                    AddOrUpdateValue(UsernameSettingKeyName, value);
                    Save();
                }
            }
    
            /// <summary>
            /// 属性获取并设置一个密码设置关键。
            /// </summary>
            public string PasswordSetting
            {
                get
                {
                    return GetValueOrDefault<string>(PasswordSettingKeyName, PasswordSettingDefault);
                }
                set
                {
                    AddOrUpdateValue(PasswordSettingKeyName, value);
                    Save();
                }
            }
        }

      MainPage.xaml页面为起始页面,在这里主要是添加一个菜单项,跳转到SettingsWithoutConfirmation页面:

    View Code
    <phone:PhoneApplicationPage 
        x:Class="sdkSettingsCS.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
        xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
        FontFamily="{StaticResource PhoneFontFamilyNormal}"
        FontSize="{StaticResource PhoneFontSizeNormal}"
        Foreground="{StaticResource PhoneForegroundBrush}"
        SupportedOrientations="Portrait" Orientation="Portrait"
        shell:SystemTray.IsVisible="True">
    
        <!--LayoutRoot is the root grid where all page content is placed-->
        <Grid x:Name="LayoutRoot" Background="Transparent">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
    
            <!--TitlePanel contains the name of the application and page title-->
            <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
                <TextBlock x:Name="ApplicationTitle" Text="Windows Phone SDK Sample" Style="{StaticResource PhoneTextNormalStyle}"/>
                <TextBlock x:Name="PageTitle" Text="Settings Sample" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" FontSize="64"/>
            </StackPanel>
    
            <!--ContentPanel - place additional content here-->
            <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
                <TextBlock Height="407" HorizontalAlignment="Left" Margin="6,26,0,0" Name="textBlock1" Text="This sample shows two Settings screens, one where the settings changes take effect immediately and one where the user has to confirm the changes. The settings are stored in Isolated Storage.  You can navigate to the Settings screens using the menu item on the Application Bar below." VerticalAlignment="Top" Width="446" TextWrapping="Wrap" />
            </Grid>
        </Grid>
    </phone:PhoneApplicationPage>
    View Code
            public MainPage()
            {
                InitializeComponent();
    
                SupportedOrientations = SupportedPageOrientation.Portrait;
    
                //添加一个应用程序条与“设置菜单项。
                ApplicationBar = new ApplicationBar();
                ApplicationBar.IsMenuEnabled = true;
                ApplicationBar.IsVisible = true;
                ApplicationBar.Opacity = 1.0;
    
                ApplicationBarMenuItem settingsItem = new ApplicationBarMenuItem("settings");
                settingsItem.Click += new EventHandler(settings_Click);
    
                ApplicationBar.MenuItems.Add(settingsItem);
            }
    
            /// <summary>
            /// 设置按钮单击事件处理程序
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            void settings_Click(object sender, EventArgs e)
            {
                this.NavigationService.Navigate(new Uri("/SettingsWithoutConfirmation.xaml", UriKind.Relative));
            }

      SettingsWithoutConfirmation是一个不需要用户确认的存储我们可以使用数据绑定的方式进行存储,在XAML中我们使用TwoWay的模式进行绑定,使更改立即生效。在这里要注意首先要在页面引用 xmlns:local="clr-namespace:SettingsSample"命名空间,并且在xaml中要使用TwoWay的数据绑定模式使更改立即生效。

    View Code
    <phone:PhoneApplicationPage 
        x:Class="sdkSettingsCS.SettingsWithoutConfirmation"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
        xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        
        xmlns:local="clr-namespace:sdkSettingsCS"
        
        FontFamily="{StaticResource PhoneFontFamilyNormal}"
        FontSize="{StaticResource PhoneFontSizeNormal}"
        Foreground="{StaticResource PhoneForegroundBrush}"
        SupportedOrientations="Portrait" Orientation="Portrait"
        mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
        shell:SystemTray.IsVisible="True">
        
        <phone:PhoneApplicationPage.Resources>
            <local:AppSettings x:Key="appSettings"></local:AppSettings>
        </phone:PhoneApplicationPage.Resources>
        
        <Grid x:Name="LayoutRoot" Background="Transparent">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
                <TextBlock x:Name="ApplicationTitle" Text="Windows Phone SDK Sample" Style="{StaticResource PhoneTextNormalStyle}"/>
                <TextBlock x:Name="PageTitle" Text="Settings Without Confirmation" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" FontSize="34"/>
            </StackPanel>
            <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
                <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,320,0,0" Name="radioButton1" VerticalAlignment="Top" GroupName="GroupOne" IsChecked="{Binding Source={StaticResource appSettings}, Path=RadioButton1Setting, Mode=TwoWay}" />
                <RadioButton Content="Choice Two" Height="Auto" HorizontalAlignment="Left" Margin="60,380,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,440,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" />
            </Grid>
        </Grid>
    </phone:PhoneApplicationPage>
            /// <summary>
            /// 单击跳转事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void buttonAdditional_Click(object sender, RoutedEventArgs e)
            {
                this.NavigationService.Navigate(new Uri("/SettingsWithConfirmation.xaml", UriKind.Relative));
            }

      SettingsWithConfirmation是需要用户进行确认的存储:

    View Code
    <phone:PhoneApplicationPage 
        x:Class="sdkSettingsCS.SettingsWithConfirmation"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
        xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        FontFamily="{StaticResource PhoneFontFamilyNormal}"
        FontSize="{StaticResource PhoneFontSizeNormal}"
        Foreground="{StaticResource PhoneForegroundBrush}"
        SupportedOrientations="Portrait" Orientation="Portrait"
        mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
        shell:SystemTray.IsVisible="True">
    
        <Grid x:Name="LayoutRoot" Background="Transparent">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
    
            <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
                <TextBlock x:Name="ApplicationTitle" Text="Windows Phone SDK Sample" Style="{StaticResource PhoneTextNormalStyle}"/>
                <TextBlock x:Name="PageTitle" Text="Settings With Confirmation" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" FontSize="38"/>
            </StackPanel>
    
            <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
                <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" />
    
            </Grid>
        </Grid>
    </phone:PhoneApplicationPage>
    View Code
            private AppSettings settings = new AppSettings();
    
            public SettingsWithConfirmation()
            {
                InitializeComponent();
    
                //添加一个应用程序栏有一个“完成”确认按钮和一个“取消”按钮
                ApplicationBar = new ApplicationBar();
                ApplicationBar.IsMenuEnabled = true;
                ApplicationBar.IsVisible = true;
                ApplicationBar.Opacity = 1.0;
    
                ApplicationBarIconButton doneButton = new ApplicationBarIconButton(new Uri("/Images/appbar.check.rest.png", UriKind.Relative));
                doneButton.Text = "done";
                doneButton.Click += new EventHandler(doneButton_Click);
    
                ApplicationBarIconButton cancelButton = new ApplicationBarIconButton(new Uri("/Images/appbar.cancel.rest.png", UriKind.Relative));
                cancelButton.Text = "cancel";
                cancelButton.Click += new EventHandler(cancelButton_Click);
    
                ApplicationBar.Buttons.Add(doneButton);
                ApplicationBar.Buttons.Add(cancelButton);
    
                //复制当前设置到文本框中输入新值将不被保存,直到用户单击“完成”按钮。
                textBoxUsername.Text = settings.UsernameSetting;
                passwordBoxPassword.Password = settings.PasswordSetting;
            }
    
            /// <summary>
            /// “完成”单击按钮的事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            void doneButton_Click(object sender, EventArgs e)
            {
                settings.UsernameSetting = textBoxUsername.Text;
                settings.PasswordSetting = passwordBoxPassword.Password;
                NavigationService.GoBack();
            }
    
            /// <summary>
            /// "取消"按钮单击事件处理程序
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            void cancelButton_Click(object sender, EventArgs e)
            {
                NavigationService.GoBack();
            }
  • 相关阅读:
    When You Get Troubles
    CentOS 6.8升级到7+
    Tomcat服务器搭建
    Percona Server 安装
    VirtualBox中如何使虚拟机能够上网?
    CentOS
    xen安装
    SSH免密码设置
    打造绿色版的RobotFramework
    零散知识记录-Jira的安装
  • 原文地址:https://www.cnblogs.com/renhao0118/p/2810324.html
Copyright © 2011-2022 走看看