ndlli 发表于 2015-5-21 10:32:09

win8 app SettingPane

  我们在操作metro 的时候,可以通过右手指从屏幕右边缘水平向左划下,然后出现 The charms bar.
  在这个The charms bar 里面我们可以做很多事情,比如应用程序的设置啊、或者系统登录窗口啊。。
  下面就简单介绍下:

  Displaying the charms bar
   

  
  实现方式:
  首先新建一个用户自定义控件 SimpleSetting



   
   
      
            
            
               
               
            
            
            
               
                  
                        
                           
                        
                  
                  
                        
                        
                        
                  
                  
                  
                  
               
            
            
            
               
                  
                  
                  
                  
                  
               
               
                  
                        
                  
               
               
               
               
               
               
            
      
   
  Code-behind:


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.ApplicationSettings;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// “用户控件”项模板在 http://go.microsoft.com/fwlink/?LinkId=234236 上提供

namespace App4
{
    public sealed partial class SimpleSetting : UserControl
    {
      public SimpleSetting()
      {
            this.InitializeComponent();
      }
      private void btnBack_Click(object sender, RoutedEventArgs e)
      {
            if (this.Parent.GetType() == typeof(Popup))
            {
                ((Popup)this.Parent).IsOpen = false;
            }
            SettingsPane.Show();
      }
    }
}  
  在首页的MainPage 页面里面:
  
      
            
            
            
      
   
  
  Code-behind


public sealed partial class MainPage : Page
    {
      Rect _windowBounds;
      double _settingsWidth = 346;
      Popup _settingsPopup;
      public MainPage()
      {
            this.InitializeComponent();
            _windowBounds = Window.Current.Bounds;
            Window.Current.SizeChanged += OnWindowSizeChanged;
            SettingsPane.GetForCurrentView().CommandsRequested += BlankPage_CommandsRequested;
      }
      void OnWindowSizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
      {
            _windowBounds = Window.Current.Bounds;
      }
      void BlankPage_CommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)
      {
            SettingsCommand cmd = new SettingsCommand("sample", "登录", (x) =>
            {
                _settingsPopup = new Popup();
                _settingsPopup.Closed += OnPopupClosed;
                Window.Current.Activated += OnWindowActivated;
                _settingsPopup.IsLightDismissEnabled = true;
                _settingsPopup.Width = _settingsWidth;
                _settingsPopup.Height = _windowBounds.Height;
                SimpleSetting mypane = new SimpleSetting();
                mypane.Width = _settingsWidth;
                mypane.Height = _windowBounds.Height;
                _settingsPopup.Child = mypane;
                _settingsPopup.SetValue(Canvas.LeftProperty, _windowBounds.Width - _settingsWidth);
                _settingsPopup.SetValue(Canvas.TopProperty, 0);
                _settingsPopup.IsOpen = true;
            });
            args.Request.ApplicationCommands.Add(cmd);
      }
      private void OnWindowActivated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
      {
            if (e.WindowActivationState == Windows.UI.Core.CoreWindowActivationState.Deactivated)
            {
                _settingsPopup.IsOpen = false;
            }
      }
      void OnPopupClosed(object sender, object e)
      {
            Window.Current.Activated -= OnWindowActivated;
      }

      ///
      /// Invoked when this page is about to be displayed in a Frame.
      ///
      /// Event data that describes how this page was reached.The Parameter
      /// property is typically used to configure the page.
      protected override void OnNavigatedTo(NavigationEventArgs e)
      {
      }
      private void Button_Click_1(object sender, RoutedEventArgs e)
      {
            SettingsPane.Show();
      }
      private void HandleSizeChange(object sender, RoutedEventArgs e)
      {
            RadioButton rb = sender as RadioButton;
            _settingsWidth = Convert.ToInt32(rb.Content);
      }
    }  

  
  文章来源:http://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/hh872190.aspx
页: [1]
查看完整版本: win8 app SettingPane