图解使用Win8Api进行Metro风格的程序开发五在系统的设置窗口添加按钮

我们紧接着上篇,这篇将介绍如何使用ApplicationSettings API在系统的设置窗口添加按钮

-----------------------------------我是华丽的分割线-----------------------------------------
今天我们要用ApplicationSettings API在系统的设置窗口添加按钮

本篇将介绍如下两个方面:
a)默认页面的系统的设置窗口
b)添加了自定义按钮的系统的设置窗口

我们的创建的步骤如下:
1)为了组织文件方便,我们先建一个文件夹ApplicationSettings
2)向文件夹中添加如下四个文件:
  Default.xaml,AddSettings.xaml,
 创建方法请参照前一篇.
3)此时的解决方案结构如下:

4)向我们的DataSource添加导航所需要的信息
  修改我们的SampleDataSource.cs文件中的SampleDataSource类中的代码,
  代码如下: 

View Code
        public SampleDataSource()
        {
            #region Group1
            var group1 = new SampleDataGroup("FilePicker",
              "Use Windows.Storage.Pickers API",
              "Access and save files using the file picker",
              "Assets/FilePicker.jpg",
              "");
            group1.Items.Add(new SampleDataItem("FilePicker-PickASinglePhoto",
                    "Pick a single photo",
                    "only one file can selected,file type is jpg,jpeg,png",
                    "Assets/FilePicker.jpg",
                    "only one file can selected ",
                    "",
                    group1,
                    typeof(PickASinglePhoto)));
            group1.Items.Add(new SampleDataItem("FilePicker-PickMultipleFiles",
                    "Pick multiple files",
                    "you can pick multiple files",
                    "Assets/FilePicker.jpg",
                    "pick multiple files",
                    "",
                    group1,
                    typeof(PickMultipleFiles)));
            group1.Items.Add(new SampleDataItem("FilePicker-PickAFolder",
                    "Pick a folder",
                    "you can pick a folder",
                    "Assets/FilePicker.jpg",
                    "Pick a folder",
                    "",
                    group1,
                    typeof(PickAFolder)));
            group1.Items.Add(new SampleDataItem("FilePicker-SaveAFile",
                    "Save a file",
                    "you can save a file",
                    "Assets/FilePicker.jpg",
                    "Save a file",
                    "",
                    group1,
                    typeof(SaveAFile)));
            this.AllGroups.Add(group1);
            #endregion

            #region Group2
            var group2 = new SampleDataGroup("FileAceess",
           "Using Windows.Storage API",
           "File access",
           "Assets/FileAccess.jpg",
           "");
            group2.Items.Add(new SampleDataItem("FileAceess-CreatingAFile",
                    "Create a file",
                    "Using CreateFileAsync Create a file",
                    "Assets/FileAccess.jpg",
                    "Using CreateFileAsync",
                    "",
                    group2,
                    typeof(CreatingAFile)));

            group2.Items.Add(new SampleDataItem("FileAceess-WritingAndReadingText",
               "Write And Read A Text",
               "Using WriteTextAsync,ReadTextAsync Write And Read  Text",
               "Assets/FileAccess.jpg",
               "Using WriteTextAsync,ReadTextAsync",
               "",
               group2,
               typeof(WritingAndReadingText)));

            group2.Items.Add(new SampleDataItem("FileAceess-WritingAndReadingBytes",
              "Writing and reading bytes in a file",
              "Using WriteBufferAsync,ReadBufferAsync Write And Read bytes",
              "Assets/FileAccess.jpg",
              "Using WriteBufferAsync,ReadBufferAsync",
              "",
              group2,
              typeof(WritingAndReadingBytes)));

            group2.Items.Add(new SampleDataItem("FileAceess-WritingAndReadingUsingStream",
                "Writing and reading using a stream",
                "Using OpenAsync Writing and reading using a stream",
                "Assets/FileAccess.jpg",
                "Using OpenAsync",
                "",
                group2,
                typeof(WritingAndReadingUsingStream)));

            group2.Items.Add(new SampleDataItem("FileAceess-DisplayingFileProperties",
                "Displaying file properties",
                "Using GetBasicPropertiesAsync  Get File Properties",
                "Assets/FileAccess.jpg",
                "Using GetBasicPropertiesAsync",
                "",
                group2,
                typeof(DisplayingFileProperties)));

            group2.Items.Add(new SampleDataItem("FileAceess-PersistingAccess",
                "Persisting access to a storage item for future use",
                "Using MostRecentlyUsedList",
                "Assets/FileAccess.jpg",
                "Using MostRecentlyUsedList",
                "",
                group2,
                typeof(PersistingAccess)));

            group2.Items.Add(new SampleDataItem("FileAceess-CopyAFile",
                "Copy a file",
                "Using CopyAsync Copy a file",
                "Assets/FileAccess.jpg",
                "Using CopyAsync",
                "",
                group2,
                typeof(CopyAFile)));

            group2.Items.Add(new SampleDataItem("FileAceess-DeleteAFile",
                "Delete a file",
                "Using DeleteAsync Delete a file",
                "Assets/FileAccess.jpg",
                "Using DeleteAsync",
                "",
                group2,
                typeof(DeleteAFile)));

            this.AllGroups.Add(group2);
            #endregion

            #region Group3
            var group3 = new SampleDataGroup("AccountPictureName",
              "Use Windows.System.UserProfile API",
              "Account Picture Name",
              "Assets/AccountPictureName.jpg",
              "");
            group3.Items.Add(new SampleDataItem("AccountPictureName-GetUserDisplayName",
                    "Get User DisplayName",
                    "Use UserInformation.GetDisplayNameAsync Get User DisplayName",
                    "Assets/AccountPictureName.jpg",
                    "Use UserInformation.GetDisplayNameAsync",
                    "",
                    group3,
                    typeof(GetUserDisplayName)));
            group3.Items.Add(new SampleDataItem("AccountPictureName-GetUserFirstLastName",
                    "Get First Last Name",
                    "Use UserInformation.GetFirstNameAsync,GetLastNameAsync Get First Name",
                    "Assets/AccountPictureName.jpg",
                    "Use UserInformation.GetFirstNameAsync ",
                    "",
                    group3,
                    typeof(GetUserFirstLastName)));
            group3.Items.Add(new SampleDataItem("AccountPictureName-GetAccountPicture",
                    "Get Account Picture",
                    "Use UserInformation.GetAccountPicture Get Account Picture",
                    "Assets/AccountPictureName.jpg",
                    "Use UserInformation.GetAccountPicture",
                    "",
                    group3,
                    typeof(GetAccountPicture)));
            group3.Items.Add(new SampleDataItem("AccountPictureName-SetAccountPictureAndListen",
                    "Set AccountPicture And Listen",
                    "Use UserInformation.SetAccountPicturesAsync Set AccountPicture",
                    "Assets/AccountPictureName.jpg",
                    "Use UserInformation.SetAccountPicturesAsync",
                    "",
                    group3,
                    typeof(SetAccountPictureAndListen)));
            this.AllGroups.Add(group3);
            #endregion

            #region Group4
            var group4 = new SampleDataGroup("ApplicationSettings",
              "ApplicationSettings",
              " Use the Windows.UI.ApplicationSettings namespace and WinJS.UI.SettingsFlyout",
              "Assets/ApplicationSettings.jpg",
              "");
            group4.Items.Add(new SampleDataItem("ApplicationSettings-Default",
                    "Default behavior with no settings integration",
                    "Default behavior ",
                    "Assets/ApplicationSettings.jpg",
                    "Default behavior with no settings integration",
                    "",
                    group4,
                    typeof(Default)));
            group4.Items.Add(new SampleDataItem("ApplicationSettings-AddSettings",
                    "Add settings commands to the settings charm",
                    "Add settings",
                    "Assets/ApplicationSettings.jpg",
                    "Add settings commands to the settings charm ",
                    "",
                    group4,
                    typeof(AddSettings)));

            this.AllGroups.Add(group4);
            #endregion
        }

5)我们的导航这样就做好了,效果图:

  点击 ApplicationSettings

6)默认页面的系统的设置窗口
   修改Default.xaml的xaml:

View Code
    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid x:Name="Input" Grid.Row="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <TextBlock x:Name="InputTextBlock1"  TextWrapping="Wrap" Grid.Row="0" Style="{StaticResource SubheaderTextStyle}" HorizontalAlignment="Left" >
                <Run Text="系统的设置窗口默认是隐藏的,你可以使用手指轻放在屏幕的右边缘,或使用鼠标放在屏幕的右边缘或使用win+i调出系统的设置窗口" />
                <LineBreak/>
                <LineBreak/>
                <Run Text="打开的设置面板,选择 权限 来查看本程序所要用到的权限" />
            </TextBlock>
        </Grid>
    </Grid>

  效果图:

7)添加了自定义按钮的系统的设置窗口

  我们使用Windows.UI.ApplicationSettings 命名空间SettingsPane

  修改AddSettings.xaml的xaml:

View Code
    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid x:Name="Input" Grid.Row="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <StackPanel>
                <TextBlock x:Name="InputTextBlock1" Grid.Row="0" TextWrapping="Wrap" Style="{StaticResource SubheaderTextStyle}" HorizontalAlignment="Left">
                <Run Text="Add two settings commands, General and Help, to the settings charm." />
                <LineBreak/>
                <Run/>
                <LineBreak/>
                <Run Text="Invoke the settings charm to view the results." />
                </TextBlock>
                <StackPanel Orientation="Horizontal" Margin="0,10,0,0">
                    <Button x:Name="addSettingsScenarioAdd" Grid.Row="1" Content="Add settings commands" Margin="0,0,10,0" Click="addSettingsScenarioAdd_Click"/>
                </StackPanel>
            </StackPanel>
        </Grid>
        <Grid x:Name="Output" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="1">
            <TextBlock x:Name="OutputTextBlock" Style="{StaticResource SubheaderTextStyle}" TextWrapping="Wrap"/>
        </Grid>
    </Grid>

  修改后台代码:

View Code
    public sealed partial class AddSettings : Page
    {
        private bool isEventRegistered;
        public AddSettings()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            // Added to make sure the event handler for CommandsRequested in cleaned up before other scenarios
            if (this.isEventRegistered)
            {
                SettingsPane.GetForCurrentView().CommandsRequested -= onCommandsRequested;
                this.isEventRegistered = false;
            }
        }

        void addSettingsScenarioAdd_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            if (b != null)
            {
                OutputTextBlock.Text = "You selected the " + b.Content + " button";
                if (!this.isEventRegistered)
                {
                    SettingsPane.GetForCurrentView().CommandsRequested += onCommandsRequested;
                    this.isEventRegistered = true;
                }
            }
        }

        void onSettingsCommand(IUICommand command)
        {
            SettingsCommand settingsCommand = (SettingsCommand)command;
            OutputTextBlock.Text = "You selected the " + settingsCommand.Label + " settings command";
        }

        void onCommandsRequested(SettingsPane settingsPane, SettingsPaneCommandsRequestedEventArgs eventArgs)
        {
            UICommandInvokedHandler handler = new UICommandInvokedHandler(onSettingsCommand);

            SettingsCommand generalCommand = new SettingsCommand("generalSettings", "General", handler);
            eventArgs.Request.ApplicationCommands.Add(generalCommand);

            SettingsCommand helpCommand = new SettingsCommand("helpPage", "Help", handler);
            eventArgs.Request.ApplicationCommands.Add(helpCommand);
        }
    }

  效果图如下:

  当我们不点击"Add settings commands"时:

  点击后出现了 General 和 Help:

  点击 General

未完待续,敬请期待...

转载请注明出处:http://www.cnblogs.com/refactor/

原文地址:https://www.cnblogs.com/refactor/p/2543898.html