欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > 内容正文

Button类控件

来源: 开发者 投稿于  被查看 47491 次 评论:60

Button类控件


Windows Phone中提供了五种按钮控件,分别为:Button、HyperlinkButton、RadioButton、ToggleButton、RepeatButton。这些控件显示名称的属性都是Content。
 
Button
类似于WinForm中的Button,一样有Click、KeyDown等事件,但是对于MouseEnter等鼠标操作性事件来说在手机上不好控制,所以使用起来还是谨慎些。而与WinForm的控件把不同的是,在C#代码中更改前景色和背景色等的时候Windows Phone必须使用Brush来填充,不能直接指定颜色。
 
HyperlinkButton
表示显示超链接的按钮控件。它一般用来执行页面导航,所以一个重要的属性就是NavigateUri,获取或设置单击 HyperlinkButton 时要导航到的 URI。因此HyperlinkButton的一个重要应用就是页面的导航。另外一个重要属性就是HyperlinkButton.TargetName,获取或设置网页应在其中打开的目标窗口或框架的名称,或 Silverlight 应用程序中要导航到的对象的名称。如果导航到当前 Silverlight 应用程序的外部位置,则 TargetName 属性与标准 HTML TARGET 特性相对应。如果不指定TargetName则使用的路径只能为相对路径。
使用方式:
_blank、_media 或 _search:将链接文档加载到新的空白窗口中。
_parent、_self、_top 或 "":将相应页面加载到在其中单击该链接的窗口(活动窗口)中。它的常用事件就是Click,但是一般不用设置,只要写好NavigateURI就行了。
 
 
RadioButton
这个和WinForm中的控件的用法大致一样,它的作用就是标识一类值中选中一个值,它的一个常用的属性就是GroupName,只要设置了这个属性就可以很好的将一个页面中的某一些类型的值分配到一起。它的一些事件处理就像WinForm中的处理一样,当选项更改时发生,当点击控件时发生等等。但是它添加了一种状态Indeterminate,称作不确定状态,使用IsThreeState 属性获取或设置指示控件是支持两种状态还是三种状态的值。获取或设置指示控件是支持两种状态还是三种状态的值。事件有Checked、Unchecked和Indeterminate,分别是当选中时、,没选中时以及当 ToggleButton 的状态切换到不确定状态时发生。
 
RepeatButton
表示从按下按钮到释放按钮的时间内重复引发其 Click 事件的控件,是一个比较新的控件,在触摸屏界面上很有用,它主要是使用户按住按钮之后不松开,便会执行一个事件,它的Click事件就是在用户按住按钮不松开的前提下使用的,这相当于Button的变种。它有两个重要的属性,
Delay,获取或设置 RepeatButton 被按下后在开始重复单击操作之前等待的时间(以毫秒为单位)。也就是说如果在延迟时间内松开按钮,那么Click事件就被执行一次,其默认值是500毫秒,也就是0.5秒,但要注意进制转换,1000毫秒是1秒;
Interval,获取或设置重复开始后单击操作的重复时间间隔(以毫秒为单位)。默认是33毫秒,也就是用户按住按钮不放,每33毫秒执行一次Click操作。
如果上述两个属性害怕混淆的话,就这样理解,Delay是控制用户点击按钮的,Interval是控制Click执行的。
 
ToggleButton
可切换状态的控件的基类,例如是CheckBox 和 RadioButton的基类。三个主要事件Checked、UnChecked和Indeterminate。
下面这是一些主要的属性及事件的应用的练习。在wp7项目中添加一个新页面,名为ButtonControls,布局及截图为:
<!--LayoutRoot 是包含所有页面内容的根网格-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel 包含应用程序的名称和页标题-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="页面名称" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - 在此处放置其他内容-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Button Content="Button" Height="72" HorizontalAlignment="Left"
                    Margin="33,26,0,0" Name="button1"
                    VerticalAlignment="Top" Width="208" Click="button1_Click" />
            <HyperlinkButton Content="HyperlinkButton" Height="30"
                             HorizontalAlignment="Left"
                             Margin="33,379,0,0" Name="hyperlinkButton1"
                             VerticalAlignment="Top" Width="200"
                             Click="hyperlinkButton1_Click"/>
            <RepeatButton Height="72" HorizontalAlignment="Left"
                          Margin="33,88,0,0" Name="repeatButton1"
                          VerticalAlignment="Top" Width="335"
                          Content="Repeat" Click="repeatButton1_Click" />
            <ToggleButton Height="83" HorizontalAlignment="Left"
                          Margin="33,244,0,0" Name="toggleButton1"
                          VerticalAlignment="Top" Width="208"
                          IsChecked="True" IsThreeState="True"
                          Click="toggleButton1_Click" Checked="toggleButton1_Checked"
                          Unchecked="toggleButton1_Unchecked" Indeterminate="toggleButton1_Indeterminate"
                          />
            <RadioButton Content="RadioButton" HorizontalAlignment="Left" Margin="33,166,0,369"
                         Name="radioButton1" Width="208" IsThreeState="True"
                         Click="radioButton1_Click" Checked="radioButton1_Checked"
                         Unchecked="radioButton1_Unchecked" Indeterminate="radioButton1_Indeterminate"
                         />
        </Grid>

  \

 

在后台代码中这样书写:
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 Controls
{
    public partial class ButtonControls : PhoneApplicationPage
    {
        public ButtonControls()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("您点击了Button");
        }

        int i = 0;
        private void repeatButton1_Click(object sender, RoutedEventArgs e)
        {
            this.repeatButton1.Content = "";
           
            this.repeatButton1.Content="RepeatButton"+(i++).ToString ();
        }

        private void radioButton1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("您点击了RadioButton");
        }

        private void radioButton1_Checked(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("RadioButton的状态:Checked");
        }

        private void radioButton1_Unchecked(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("RadioButton的状态:UnChecked");
        }

        private void radioButton1_Indeterminate(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("RadioButton的状态:Indeterminate");
        }

        private void toggleButton1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("您点击了ToggleButton");
        }

        private void toggleButton1_Checked(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("ToggleButton的状态:Checked");
        }

        private void toggleButton1_Unchecked(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("ToggleButton的状态:UnChecked");
        }

        private void toggleButton1_Indeterminate(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("ToggleButton的状态:Indeterminate");
        }

        private void hyperlinkButton1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("您点击了HyperlinkButton");
        }
    }
}

当用户点击按钮时给出一些提示信息。
 
为了更好的练习这几个控件,我们来编写两个小游戏,一个是小时候在文曲星上玩过的小游戏,叫做猜数字,为了练习上边的控件,编的稍微不伦不类一点。
思路:按住RepeatButton,随机生成一个随机数,而随机数的位数由RadioButton来选择,随机数使用不可编辑的PassWordBox来显示,当然,要有很多异常在代码中处理,使用TextBox来使用户输入数字,使用TextBlock来显示一些提示信息,比如数字猜的大了还是小了等,使用Button来提交自己的结果,这仅仅是为了练习,我们不去设置猜数字的次数上限。在使用过程中每一次长按RepeatButton就相当于是游戏重新启动。为了显示更多的log信息,使用ScrollViewer将显示信息的TextBlock包起来,至于这个控件是什么作用,以后会说的。但是有一点,千万不能手动规定TextBlock的Height属性,其属性在设计器中定位Auto即可。
界面设计:

 \


 

.xaml代码
<!--LayoutRoot 是包含所有页面内容的根网格-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel 包含应用程序的名称和页标题-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="猜数字" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - 在此处放置其他内容-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Button Content="猜一下" Height="72"
                    HorizontalAlignment="Left" Margin="263,146,0,0"
                    Name="submit" VerticalAlignment="Top" Width="160"
                    Click="submit_Click"/>
            <TextBox Height="72" HorizontalAlignment="Left"
                     Margin="0,146,0,0" Name="myTextBox" Text=""
                     VerticalAlignment="Top" Width="202"
                     >
                <TextBox.InputScope>
                    <InputScope>
                        <InputScopeName NameValue="Number"/>
                    </InputScope>
                </TextBox.InputScope>
            </TextBox>
            <PasswordBox Height="72" HorizontalAlignment="Left"
                         Margin="196,68,0,0" Name="myPasswordBox"
                         VerticalAlignment="Top" Width="254"
                         IsEnabled="False"
                         />
            <RadioButton Content="3位数" Height="72"
                         HorizontalAlignment="Left"
                         Margin="12,6,0,0" Name="threeNumbers"
                         VerticalAlignment="Top" Checked="radio_Checked"
                         />
            <RadioButton Content="4位数" Height="72"
                         HorizontalAlignment="Left" Margin="165,6,0,0"
                         Name="fourNumbers" VerticalAlignment="Top"
                         Click="radio_Checked"
                         />
            <RadioButton Content="5位数" Height="72"
                         HorizontalAlignment="Left" Margin="324,6,0,0"
                         Name="fiveNumbers"
                         VerticalAlignment="Top"
                         Click="radio_Checked"/>
            <RepeatButton Content="长按生成数字" Height="72" HorizontalAlignment="Left"
                          Margin="0,68,0,0" Name="produce"
                          VerticalAlignment="Top" Width="202" IsEnabled="False"
                          Click="produce_Click"/>
            <ScrollViewer Height="353" Width="430" Margin="12,224,0,0"
                          VerticalScrollBarVisibility="Visible">
                <TextBlock  HorizontalAlignment="Left"
                           Margin="0,0,0,0" Name="myLog"
                           Text="" VerticalAlignment="Top"
                           Width="426" TextWrapping="Wrap" />
            </ScrollViewer>
        </Grid>

.cs代码
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 猜数字
{
    public partial class MainPage : PhoneApplicationPage
    {
        private int myNumberByte;//定义随机数位数
 private int answer;//定义结果 www.2cto.com
 private int myAnswer;//定义用户输入的结果
    private int iterator;//定义迭代器
     private bool flag = false;//标识,表示是否能进行判断
     Random myRandom;//随机数实例

        // 构造函数
     public MainPage()
        {
            InitializeComponent();
        }

        //用户选择随机数位数
     private void radio_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton myRadioButton = (RadioButton)sender;//获取点击的radioButton
       switch (myRadioButton.Content.ToString())//判断选择的是哪一个radioButton
       {
                case "3位数":
                    myNumberByte = 3;
                    break;

                case "4位数":
                    myNumberByte = 4;
                    break;

                case "5位数":
                    myNumberByte = 5;
                    break;

            }
            produce.IsEnabled = true;//使生成按钮可以使用
 }

        //生成随机数
     private void produce_Click(object sender, RoutedEventArgs e)
        {
            switch (myNumberByte)
            {
                case 3:
                    answer= myRandom.Next(100,1000);//因为Next方法可取下界,取不到上界
             break;
                case 4:
                    answer=  myRandom.Next(1000,10000);//因为Next方法可取下界,取不到上界
             break;
                case 5:
                    answer=  myRandom.Next(10000,100000);//因为Next方法可取下界,取不到上界
             break;
            }
            myPasswordBox.Password = answer.ToString();//以密码符的形式显示正确值
       flag = true;//可以进行判断
       iterator = 0;//重置为0
       this.myLog.Text = "";//重新开始游戏,提示信息为空
 }

        //页面加载事件
     private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            myPasswordBox.PasswordChar = '*';//设置密码显示符
       produce.Interval = 100;//设置repeatButton的单击操作的重复时间为0.1秒
       myRandom = new Random();//实例化Random类的实例
 }

        //提交
     private void submit_Click(object sender, RoutedEventArgs e)
        {
            if (flag)
            {
                if (int.TryParse(myTextBox.Text.Trim(), out myAnswer))
                {
                    iterator++;//迭代器即输入次数加1
         if (myAnswer == answer)
                    {
                        myLog.Text += String.Format("这是您的第 {0} 次输入..\n您输入的数是:{1}...\n恭喜,您成功了!\n正确答案是:{2}\n游戏结束。", iterator, myAnswer.ToString(), answer.ToString());
                    }
                    else if (myAnswer > answer)
                    {
                        myLog.Text += String.Format("这是您的第 {0} 次输入..\n您输入的数是:{1}...\n不好意思,您输入的数字大了....\n试试比这个小的数字吧。\n", iterator, myAnswer.ToString());
                    }
                    else if (myAnswer < answer)
                    {
                        myLog.Text += String.Format("这是您的第 {0} 次输入..\n您输入的数是:{1}...\n不好意思,您输入的数字小了....\n试试比这个大的数字吧。\n", iterator, myAnswer.ToString());
                    }
                }
            }
        }
    }
}

 
另一个小游戏就是去模拟打地鼠,使用ToggleButton的UnChecked状态来模拟一块一块的土地,当“地鼠”出现的时候,将ToggleButton的状态设置为Checked,当用户Tap的时候,相当于是去“打地鼠”,将ToggleButton设置为UnChecked状态。这样认为的规定打中一只为20分,最后统计总分。
每一个“地鼠”出现之后,每过一定的时间,如果用户没有点击,那就自动“缩回土地里”,使用RadioButton来让用户选择游戏等级,分2个等级就好,时间间隔为1秒、2秒。然后使用System.Windows.Threading命名空间下的DispatcherTimer来实现计时功能。哦,忘了怎么使“地鼠”出现了,在游戏开始后使用后台Thread来循环检测当前有没有“地鼠”,如果没有,就出现。但是由于在编写的时候遇到了一些线程上的问题,所以在解决问这些问题之后再做补充吧。

 

 

摘自  WaitingSky
 
WaitingSky
  

相关文章

    暂无相关文章
相关频道:

用户评论