与众不同 windows phone (24) - Input(输入)之软键盘类型, XNA 方式启动软键盘, UIElement 的 Touch 相关事件, 触摸涂鸦

[索引页]
[源码下载]


与众不同 windows phone (24) - Input(输入)之软键盘类型, XNA 方式启动软键盘, UIElement 的 Touch 相关事件, 触摸涂鸦



作者:webabcd


介绍
与众不同 windows phone 7.5 (sdk 7.1) 之输入

  • 指定软键盘的类型
  • XNA 方式启动软键盘,并获取用户输入的信息
  • UIElement 的 Touch 相关事件
  • 涂鸦板



示例
1、演示如何指定软键盘的类型
InputScopeDemo.xaml

<phone:PhoneApplicationPage 
    x:Class="Demo.Input.Keyboard.InputScopeDemo"
    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">
        <StackPanel Orientation="Vertical">

            <!--SIP 包含数字和小数点,按住句号键可以显示“.,-”-->
            <TextBox InputScope="Number" />

            <!--SIP 按“123”键切换到电话号码键盘,按住句号键可以显示“.,-”-->
            <TextBox InputScope="NameOrPhoneNumber" />

            <!--SIP 默认显示数字和符号键盘-->
            <TextBox InputScope="CurrencyChinese" />

            <!--SIP 显示电话拨号键盘-->
            <TextBox>
                <TextBox.InputScope>
                    <InputScope>
                        <InputScopeName NameValue="TelephoneNumber" />
                    </InputScope>
                </TextBox.InputScope>
            </TextBox>

            <!--后台将此 TextBox 的 InputScope 设置为“EmailUserName”-->
            <!--SIP 包括 @ 和 .com 键,按住 .com 键可以显示“.org .com .edu .net”-->
            <TextBox Name="textBox" KeyDown="textBox_KeyDown" />
            
        </StackPanel>
    </Grid>

</phone:PhoneApplicationPage>

InputScopeDemo.xaml.cs

/*
 * 演示如何指定 SIP(Soft Input Panel)的输入范围
 * 本 Demo 只演示常用 SIP 布局,更多的布局请参见:http://msdn.microsoft.com/en-us/library/hh393998(v=vs.92)
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using System.Windows.Navigation;

namespace Demo.Input.Keyboard
{
    public partial class InputScopeDemo : PhoneApplicationPage
    {
        public InputScopeDemo()
        {
            InitializeComponent();
        }

      
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            /*
             * 通过指定 TextBox 的 InputScope 来决定关联 SIP 的布局方式
             * 
             * System.Windows.Input.InputScopeNameValue 枚举有多个值,每个值所对应的 SIP 布局请参见:http://msdn.microsoft.com/en-us/library/hh393998(v=vs.92)
             */

            InputScope scope = new InputScope();
            InputScopeName name = new InputScopeName();

            name.NameValue = InputScopeNameValue.EmailUserName;
            scope.Names.Add(name);

            textBox.InputScope = scope;
        }

        private void textBox_KeyDown(object sender, KeyEventArgs e)
        {
            // 判断用户是否按下了 SIP 上的回车键
            if (e.Key == Key.Enter)
            {
                MessageBox.Show("用户按下了回车键");

                // 转移焦点,虚拟键盘会自动隐藏
                this.Focus();
            }
        }
    }
}


2、演示如何以 XNA 方式启动软键盘,并获取用户输入的信息
XNAKeyboard.xaml

<phone:PhoneApplicationPage 
    x:Class="Demo.Input.Keyboard.XNAKeyboard"
    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">
        
        <Button Content="显示软键盘" Click="Button_Click" />
        
    </Grid>

</phone:PhoneApplicationPage>

XNAKeyboard.xaml.cs

/*
 * 演示如何以 XNA 的方式启动软键盘,并获取用户输入的信息
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework;

namespace Demo.Input.Keyboard
{
    public partial class XNAKeyboard : PhoneApplicationPage
    {
        public XNAKeyboard()
        {
            InitializeComponent();
        }

        private void LaunchSIP()
        {
            string title = "请输入文本";
            string description = "用于演示 XNA 启动软键盘";
            string defaultText = "hello webabcd";

            /*
             * Guide.BeginShowKeyboardInput(PlayerIndex player, string title, string description, string defaultText, AsyncCallback callback, Object state) - 显示软键盘
             *     player - 在 windows phone 下只能是 PlayerIndex.One
             *     title - SIP 对话框上显示的标题
             *     description - SIP 对话框上显示的描述
             *     defaultText - SIP 对话框中的文本框的默认文本
             *     callback - 回调方法
             *     state - 上下文
             */

            Guide.BeginShowKeyboardInput(PlayerIndex.One, title, description, defaultText, GetText, null);
        }

        private void GetText(IAsyncResult result)
        {
            /*
             * 获取用户输入的文本信息
             */
            string resultString = Guide.EndShowKeyboardInput(result);

            this.Dispatcher.BeginInvoke(delegate
            {
                // 显示用户输入的信息
                MessageBox.Show("用户输入的文本:" + resultString);
            });
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            LaunchSIP();
        }  
    }
}


3、演示如何响应 UIElement 的 Touch 相关事件
UIElementTouchEvent.xaml

<phone:PhoneApplicationPage 
    x:Class="Demo.Input.Touch.UIElementTouchEvent"
    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">
        <StackPanel Orientation="Vertical">

            <Rectangle Width="100" Height="100" Fill="Red" 
                       Tap="Rectangle_Tap"
                       DoubleTap="Rectangle_DoubleTap"
                       Hold="Rectangle_Hold" />

            <TextBlock Name="lblMsg" TextWrapping="Wrap" Margin="0 15 0 0" Text="触摸红色方块以演示 Tap 事件,DoubleTap 事件,Hold 事件" />
            
        </StackPanel>
    </Grid>

</phone:PhoneApplicationPage>

UIElementTouchEvent.xaml.cs

/*
 * 演示 UIElement 支持的 Touch 相关的事件
 * 
 * UIElement - UI 元素
 *     Tap - 单击事件(事件参数:GestureEventArgs)
 *     DoubleTap - 双击事件(事件参数:GestureEventArgs)
 *     Hold - 较长时间触摸某一 UIElement 时所触发的事件(事件参数:GestureEventArgs)
 *     
 * GestureEventArgs
 *     GetPosition(UIElement relativeTo) - 获取触摸点相对于指定 UIElement 的坐标
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace Demo.Input.Touch
{
    public partial class UIElementTouchEvent : PhoneApplicationPage
    {
        public UIElementTouchEvent()
        {
            InitializeComponent();
        }

        private void Rectangle_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            lblMsg.Text = "Rectangle 的 Tap 事件被触发,触摸点相对于 LayoutRoot 的坐标为:" + e.GetPosition(LayoutRoot).ToString();
        }

        private void Rectangle_DoubleTap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            lblMsg.Text = "Rectangle 的 DoubleTap 事件被触发,触摸点相对于 LayoutRoot 的坐标为:" + e.GetPosition(LayoutRoot).ToString();
        }

        private void Rectangle_Hold(object sender, System.Windows.Input.GestureEventArgs e)
        {
            lblMsg.Text = "Rectangle 的 Hold 事件被触发,触摸点相对于 LayoutRoot 的坐标为:" + e.GetPosition(LayoutRoot).ToString();
        }
    }
}


4、演示如何开发涂鸦板程序
InkPresenterDemo.xaml

<phone:PhoneApplicationPage 
    x:Class="Demo.Input.Touch.InkPresenterDemo"
    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">
        
        <InkPresenter x:Name="inkPresenter" Cursor="Stylus" Background="Blue" 
                      MouseMove="inkPresenter_MouseMove" MouseLeftButtonDown="inkPresenter_MouseLeftButtonDown">

            <TextBlock Text="请涂鸦" />
            
        </InkPresenter>

    </Grid>

</phone:PhoneApplicationPage>

InkPresenterDemo.xaml.cs

/*
 * 演示如何把手机当做一个涂鸦板
 * 
 * 本 Demo 只是做一个简单的示例,详细说明在之前的 Silverlight 系列文章中已经写过,请参考 http://www.cnblogs.com/webabcd/archive/2008/11/17/1334768.html
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using System.Windows.Ink;

namespace Demo.Input.Touch
{
    public partial class InkPresenterDemo : PhoneApplicationPage
    {
        // 涂鸦笔画对象
        private Stroke _newStroke;

        public InkPresenterDemo()
        {
            InitializeComponent();
        }

        private void inkPresenter_MouseMove(object sender, MouseEventArgs e)
        {
            _newStroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(inkPresenter));
        }

        private void inkPresenter_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            AddStroke();
        }

        private void AddStroke()
        {
            _newStroke = new Stroke();
            _newStroke.DrawingAttributes.Width = 3d;
            _newStroke.DrawingAttributes.Height = 3d;
            _newStroke.DrawingAttributes.Color = Colors.Green;
            _newStroke.DrawingAttributes.OutlineColor = Colors.Red;

            inkPresenter.Strokes.Add(_newStroke);
        }
    }
}

 


OK
[源码下载]

posted @ 2012-08-23 08:27  webabcd  阅读(3359)  评论(7编辑  收藏  举报