原文:与众不同 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
[源码下载]

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

  1. 与众不同 windows phone (25) - Input(输入)之捕获 UIElement 之外的触控操作, Silverlight 方式捕获手势操作, XNA 方式捕获手势操作, 多点触控

    原文:与众不同 windows phone (25) - Input(输入)之捕获 UIElement 之外的触控操作, Silverlight 方式捕获手势操作, XNA 方式捕获手势操作, 多点触 ...

  2. 背水一战 Windows 10 (68) - 控件(控件基类): UIElement - Pointer 相关事件, Tap 相关事件, Key 相关事件, Focus 相关事件

    [源码下载] 背水一战 Windows 10 (68) - 控件(控件基类): UIElement - Pointer 相关事件, Tap 相关事件, Key 相关事件, Focus 相关事件 作者: ...

  3. 与众不同 windows phone (52) - 8.1 新增控件: AutoSuggestBox, ListView, GridView, SemanticZoom

    [源码下载] 与众不同 windows phone (52) - 8.1 新增控件: AutoSuggestBox, ListView, GridView, SemanticZoom 作者:webab ...

  4. 与众不同 windows phone (48) - 8.0 其它: C# 调用 C++

    [源码下载] 与众不同 windows phone (48) - 8.0 其它: C# 调用 C++ 作者:webabcd 介绍与众不同 windows phone 8.0 之 其它 C# 中调用 W ...

  5. 与众不同 windows phone (21) - Device(设备)之摄像头(拍摄照片, 录制视频)

    原文:与众不同 windows phone (21) - Device(设备)之摄像头(拍摄照片, 录制视频) [索引页][源码下载] 与众不同 windows phone (21) - Device ...

  6. INPUT输入子系统【转】

    转自:https://www.cnblogs.com/deng-tao/p/6094049.html 1.Linux系统支持的输入设备繁多,例如键盘.鼠标.触摸屏.手柄或者是一些输入设备像体感输入等等 ...

  7. 背水一战 Windows 10 (24) - MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过非 ButtonBase 触发命令

    [源码下载] 背水一战 Windows 10 (24) - MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过非 ButtonBase 触发命令 作者:webabcd ...

  8. input输入子系统

    一.什么是input输入子系统? 1.Linux系统支持的输入设备繁多,例如键盘.鼠标.触摸屏.手柄或者是一些输入设备像体感输入等等,Linux系统是如何管理如此之多的不同类型.不同原理.不同的输入信 ...

  9. 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件

    [源码下载] 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件 作者:w ...

随机推荐

  1. win7(64位)php5.5-Apache2.4-环境安装

    一.安装配置Apache2.4.7(httpd-2.4.7-win64-VC11.zip ) 1.解压下载的安装包:httpd-2.4.12-x64-vc11-r2.zip将其放到自己的安装目录(我的 ...

  2. 【译】在Asp.Net中操作PDF - iTextSharp - 利用列进行排版

    原文 [译]在Asp.Net中操作PDF - iTextSharp - 利用列进行排版 在使用iTextSharp通过ASP.Net生成PDF的系列文章中,前面的文章已经讲述了iTextSharp所涵 ...

  3. ACM一些题目

    Low Power 先二分答案,可以通过调整证明同一台机器选的两个芯片必然是提供能量数值相邻的两个.所以再贪心一下就可以了. 时间复杂度\(O(n \log n)\). Factors 假设\(k\) ...

  4. elasticsearch的javaAPI之query

    elasticsearch的javaAPI之query API the Search API同意运行一个搜索查询,返回一个与查询匹配的结果(hits). 它能够在跨一个或多个index上运行, 或者一 ...

  5. [ACM] hdu 1251 统计难题 (字典树)

    统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...

  6. Linux内核-系统调用

    Linux内核-系统调用 1.与内核通信 #系统调用在用户空间进程和硬件设备之间添加了一个中间层 作用:1.为用户空间提供了一种硬件的抽象接口 2.系统调用保证了系统的稳定和安全 3.出于每一个进程都 ...

  7. BZOJ 2005: [Noi2010]能量采集( 数论 + 容斥原理 )

    一个点(x, y)的能量损失为 (gcd(x, y) - 1) * 2 + 1 = gcd(x, y) *  2 - 1. 设g(i)为 gcd(x, y) = i ( 1 <= x <= ...

  8. Ajax技术——带进度条的文件上传

    1.概述 在实际的Web应该开发或网站开发过程中,经常需要实现文件上传的功能.在文件上传过程中,经常需要用户进行长时间的等待,为了让用户及时了解上传进度,可以在上传文件的同时,显示文件的上传进度条.运 ...

  9. java 一个函数EnumMap返回多个值

    java 一个函数如何返回多个值   在开发过程中,经常会有这种情况,就是一个函数需要返回多个值,这是一个问题!! 网上这个问题的解决方法: 1.使用map返回值:这个方法问题是,你并不知道如何返回值 ...

  10. SQL注入(一)普通型注入

    既然说了从头开始,先从注入开始吧,先来温习一下之前会的一些注入. PHP注入 0x01: 判断是否存在注入: '   报错 ' and 1=1   正确 ' and 1=2   错误 0x01: or ...