原文:重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, RadioButton, CheckBox, ToggleSwitch

[源码下载]

重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, RadioButton, CheckBox, ToggleSwitch

作者:webabcd

介绍

重新想象 Windows 8 Store Apps 之按钮控件

  • Button - 按钮控件
  • HyperlinkButton - 超链按钮
  • RepeatButton - 按住后会重复执行单击操作的按钮
  • ToggleButton - 可切换状态的按钮
  • RadioButton - 单选框控件
  • CheckBox - 复选框控件
  • ToggleSwitch - 状态切换控件

示例
1、Button 的 Demo
ButtonDemo.xaml.cs

/*
* Button - 按钮控件
*/ using System;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation; namespace XamlDemo.Controls
{
public sealed partial class ButtonDemo : Page
{
public ButtonDemo()
{
this.InitializeComponent();
} protected override void OnNavigatedTo (NavigationEventArgs e)
{
/*
* Button - 按钮控件,其全部功能是通过其基类 ButtonBase 提供的
* ClickMode - 引发 Click 事件的模式:ClickMode.Release(默认值), ClickMode.Press, ClickMode.Hover
* IsPointerOver - 设备指针(鼠标或手指等)是否在按钮上
* IsPressed - 当前按钮是否处于按下的状态
* Command 和 CommandParameter 等写到 MVVM 的时候再详细写
*/ Button btn = new Button();
btn.Content = "我是按钮";
btn.ClickMode = ClickMode.Hover;
btn.Click += btn_Click;
root.Children.Add(btn);
} async void btn_Click(object sender, RoutedEventArgs e)
{
await new MessageDialog("触发了按钮的 Click 事件").ShowAsync();
}
}
}

2、HyperlinkButton 的 Demo
HyperlinkButtonDemo.xaml

<Page
x:Class="XamlDemo.Controls.HyperlinkButtonDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="120 0 0 0"> <!--
HyperlinkButton - 带超链接的按钮
NavigateUri - 按钮要导航到的 Uri
-->
<HyperlinkButton Name="btnLink" Content="webabcd blog" FontSize="36" Foreground="Blue" NavigateUri="http://webabcd.cnblogs.com" /> </StackPanel>
</Grid>
</Page>

HyperlinkButtonDemo.xaml.cs

/*
* HyperlinkButton - 超链按钮
*/ using Windows.UI.Xaml.Controls; namespace XamlDemo.Controls
{
public sealed partial class HyperlinkButtonDemo : Page
{
public HyperlinkButtonDemo()
{
this.InitializeComponent(); this.Loaded += HyperlinkButtonDemo_Loaded;
} void HyperlinkButtonDemo_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
// 为 HyperlinkButton 加上鼠标移入变手型的功能
btnLink.PointerEntered += btnLink_PointerEntered;
btnLink.PointerExited += btnLink_PointerExited;
} void btnLink_PointerEntered(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
// 鼠标移入 HyperlinkButton 变手型
Windows.UI.Core.CoreWindow.GetForCurrentThread().PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Hand, );
} void btnLink_PointerExited(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
// 鼠标移出 HyperlinkButton 变箭头
Windows.UI.Core.CoreWindow.GetForCurrentThread().PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Arrow, );
}
}
}

3、RepeatButton 的 Demo
RepeatButtonDemo.xaml

<Page
x:Class="XamlDemo.Controls.RepeatButtonDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" TextWrapping="Wrap" /> <!--
RepeatButton - 按住后会重复执行单击操作的按钮
Delay - 按住按钮后,会先执行一次单击操作,然后在此属性指定的时间后开始重复执行单击操作,单位毫秒,默认值 250
Interval - 重复执行单击操作时,这个重复时间的间隔,单位毫秒,默认值 250 注:Button 的 ClickMode 默认为 Release,而 RepeatButton 的 ClickMode 默认为 Press
-->
<RepeatButton Name="aa" Content="click me" Delay="1000" Interval="250" Click="RepeatButton_Click_1" Margin="0 10 0 0" /> </StackPanel>
</Grid>
</Page>

RepeatButtonDemo.xaml.cs

/*
* RepeatButton - 按住后会重复执行单击操作的按钮
*/ using Windows.UI.Xaml.Controls; namespace XamlDemo.Controls
{
public sealed partial class RepeatButtonDemo : Page
{
public RepeatButtonDemo()
{
this.InitializeComponent();
} private void RepeatButton_Click_1(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
lblMsg.Text += "a";
}
}
}

4、ToggleButton 的 Demo
ToggleButtonDemo.xaml.cs

/*
* ToggleButton - 可切换状态的按钮
*/ using System;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives; namespace XamlDemo.Controls
{
public sealed partial class ToggleButtonDemo : Page
{
public ToggleButtonDemo()
{
this.InitializeComponent(); this.Loaded += ToggleButtonDemo_Loaded;
} void ToggleButtonDemo_Loaded(object sender, RoutedEventArgs e)
{
/*
* ToggleButton - 可切换状态的 Button
* IsThreeState - 是否支持 3 状态
* IsChecked - 按钮的选中状态: false, true, null
* Checked - 按钮变为选中状态后所触发的事件
* Unchecked - 按钮变为未选中状态后所触发的事件
* Indeterminate - 按钮变为不确定状态后所触发的事件
*/ ToggleButton btn = new ToggleButton();
btn.Content = "可切换状态的按钮";
btn.Checked += btn_Checked;
btn.Unchecked += btn_Unchecked; root.Children.Add(btn);
} async void btn_Unchecked(object sender, RoutedEventArgs e)
{
await new MessageDialog("按钮为未选中 状态").ShowAsync();
} async void btn_Checked(object sender, RoutedEventArgs e)
{
await new MessageDialog("按钮为选中状 态").ShowAsync();
}
}
}

5、RadioButton 的 Demo
RadioButtonDemo.xaml

<Page
x:Class="XamlDemo.Controls.RadioButtonDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Name="root" Margin="120 0 0 0"> <!--
RadioButton - 单选框控件(继承自 ToggleButton)
GroupName - 单选框的组名,同一组单选框只能有一个为选中状态
-->
<RadioButton GroupName="groupName" IsChecked="True" Content="RadioButton1" />
<RadioButton GroupName="groupName" Content="RadioButton2" /> </StackPanel>
</Grid>
</Page>

6、CheckBox 的 Demo
CheckBoxDemo.xaml

<Page
x:Class="XamlDemo.Controls.CheckBoxDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Name="root" Margin="120 0 0 0"> <!--
CheckBox - 复选框控件(继承自 ToggleButton)
-->
<CheckBox IsChecked="True" Content="CheckBox1" /> <CheckBox IsChecked="False" Content="CheckBox2" /> </StackPanel>
</Grid>
</Page>

7、ToggleSwitch 的 Demo
ToggleSwitchDemo.xaml

<Page
x:Class="XamlDemo.Controls.ToggleSwitchDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<Grid.Resources>
<!--
自定义关闭状态的显示内容的数据模板
-->
<Style x:Key="MyToggleSwitchStyle" TargetType="ToggleSwitch">
<Setter Property="OffContentTemplate">
<Setter.Value>
<DataTemplate>
<Grid Background="Red">
<TextBlock Text="{Binding}" />
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources> <StackPanel Name="root" Margin="120 0 0 0"> <!--
ToggleSwitch - 状态切换控件
Header - 标题内容
OffContent - 关闭状态的显示内容
OnContent - 打开状态的显示内容
IsOn - 是否是 On 的状态
Toggled - 状态改变后所触发的事件
-->
<ToggleSwitch OffContent="off" Header="wifi" IsOn="True" Style="{StaticResource MyToggleSwitchStyle}">
<!--
自定义打开状态的显示内容
-->
<ToggleSwitch.OnContent>
<StackPanel Background="Blue">
<TextBlock Text="on" TextAlignment="Right" />
</StackPanel>
</ToggleSwitch.OnContent>
</ToggleSwitch>
</StackPanel>
</Grid>
</Page>

OK
[源码下载]

重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, RadioButton, CheckBox, ToggleSwitch的更多相关文章

  1. 重新想象 Windows 8 Store Apps 系列文章索引

    [源码下载][重新想象 Windows 8.1 Store Apps 系列文章] 重新想象 Windows 8 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...

  2. 重新想象 Windows 8 Store Apps (17) - 控件基础: Measure, Arrange, GeneralTransform, VisualTree

    原文:重新想象 Windows 8 Store Apps (17) - 控件基础: Measure, Arrange, GeneralTransform, VisualTree [源码下载] 重新想象 ...

  3. 重新想象 Windows 8 Store Apps (15) - 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState, VisualStateManager

    原文:重新想象 Windows 8 Store Apps (15) - 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState ...

  4. 重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试

    原文:重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试 [源码下载] 重新想象 Windows 8 Store ...

  5. 重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding

    原文:重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding [源码下 ...

  6. 重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom

    原文:重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom [源码下载] 重新想象 Windows 8 Store Apps (13) - 控件之 Sem ...

  7. 重新想象 Windows 8 Store Apps (12) - 控件之 GridView 特性: 拖动项, 项尺寸可变, 分组显示

    原文:重新想象 Windows 8 Store Apps (12) - 控件之 GridView 特性: 拖动项, 项尺寸可变, 分组显示 [源码下载] 重新想象 Windows 8 Store Ap ...

  8. 重新想象 Windows 8 Store Apps (11) - 控件之 ListView 和 GridView

    原文:重新想象 Windows 8 Store Apps (11) - 控件之 ListView 和 GridView [源码下载] 重新想象 Windows 8 Store Apps (11) - ...

  9. 重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom

    原文:重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom [源码下载] ...

随机推荐

  1. css中的hover ,关于li与a标签的问题

    <head> <style> ul li a:hover{ background-color: red; } </style></head><ul ...

  2. 如何使用git

    本文不是谈论git具体命令的技术文章. 原文地址:http://blog.csdn.net/ffb/article/details/11206067 我之前发了一条关于git中如何处理中文文件名的微博 ...

  3. iOS Dev (60) 怎样实现 UITextView 中的 placeHolder

    iOS Dev (60) 怎样实现 UITextView 中的 placeHolder 作者:阿锐 地址:http://blog.csdn.net/prevention - 跟着你的 UITextVi ...

  4. Delphi数据类型转换(有几个字符串函数没见过,比如StringToWideChar和WideCharToString)

    DateTimeToFileDate                  函数                     将DELPHI的日期格式转换为DOS的日期格式         DateTimeT ...

  5. 无法引入import com.sun.management.OperatingSystemMXBean

    现象:在JDK的安装包的jre\lib\rt.jar包里确实有这个类com.sun.management.OperatingSystemMXBean,但是就是不能import  com.sun.man ...

  6. Xcode免证书真机调试,解决cannot read entitlement data问题

    本文是根据某个帖子写的(帖子链接在最后放出),但是在配置的过程中,遇到了一个纠结的问题,这个问题折腾了我N久,一直没搞明白到底是什么原因,问题如下: 按照原帖上写的每一步去做了,但是在最后编译的时候出 ...

  7. groovy : 正則表達式

    groovy 正則表達式 企图模仿Perl 的语法,结果是我试用后.发现没法提取匹配的字符串. 还是直接引用 java.util.regex  负责对字符序列进行正則表達式匹配 先转载水木清华上的样例 ...

  8. MVC多语言应用

    MVC多语言应用 最近发现资源文件是个好东西, 用的好了可以给开发人员借阅不少的时间. 例如做一个多语言的网站, 资源文件就有不小的用处. 这里以MVC4模版项目的登录页为例, 简单说一下过程: 1. ...

  9. Tiny Mapper是一个.net平台开源的对象映射组件

    NET平台开源项目速览(14)最快的对象映射组件Tiny Mapper   阅读目录 1.Tiny Mapper基本介绍 2.Tiny Mapper 基本使用 3.Tiny Mapper 指定配置使用 ...

  10. 【iOS】苹果,百度Map定位使用与总结

    iOS中使用较多的3款地图,google地图.百度地图.苹果自带地图(高德).当中苹果自带地图在中国使用的是高德的数据.苹果在iOS 6之后放弃了使用谷歌地图,而改用自家的地图.在国内使用的较多的就是 ...