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;//定义结果
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个等级就好,时间间隔为0.6秒、1.2秒。哦,忘了怎么使“地鼠”出现了,在游戏开始后使用后台Thread来循环检测当前有没有“地鼠”,如果没有,就出现。但是由于在编写的时候遇到了一些线程上的问题,所以在解决问这些问题之后再做补充吧。

做下补充,模拟打地鼠的问题解决了,代码写在这里:

<phone:PhoneApplicationPage 
x:Class="打地鼠.MainPage"
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"
mc:Ignorable
="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily
="{StaticResource PhoneFontFamilyNormal}"
FontSize
="{StaticResource PhoneFontSizeNormal}"
Foreground
="{StaticResource PhoneForegroundBrush}"
SupportedOrientations
="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible
="True" Loaded="PhoneApplicationPage_Loaded">

<!--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="278,0,0,25"
Name
="startOrEnd" Click="startOrEnd_Click"
VerticalAlignment
="Bottom" Width="160" />
<TextBlock Height="34" HorizontalAlignment="Left"
Margin
="12,456,0,0" Name="myLog"
Text
="" VerticalAlignment="Top" Width="438" />
<Canvas Margin="0,0,0,177" Name="myCanvas">
<ToggleButton Height="120" HorizontalAlignment="Left"
Margin
="9,26,0,0" Name="toggleButton1"
VerticalAlignment
="Top" Width="120" Tap="toggleButton_Tap" IsEnabled="True">
<ToggleButton.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#FFE52F2F" Offset="0.134" />
<GradientStop Color="#FF83DB38" Offset="0.872" />
</LinearGradientBrush>
</ToggleButton.Background>
</ToggleButton>
<ToggleButton Height="120" HorizontalAlignment="Left"
Margin
="169,26,0,0" Name="toggleButton2"
VerticalAlignment
="Top" Width="120" Tap="toggleButton_Tap" IsEnabled="True">
<ToggleButton.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#FFE52F2F" Offset="0.134" />
<GradientStop Color="#FF83DB38" Offset="0.872" />
</LinearGradientBrush>
</ToggleButton.Background>
</ToggleButton>
<ToggleButton Height="120" HorizontalAlignment="Left"
Margin
="330,26,0,0" Name="toggleButton3"
VerticalAlignment
="Top" Width="120" Tap="toggleButton_Tap" IsEnabled="True">
<ToggleButton.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#FFE52F2F" Offset="0.134" />
<GradientStop Color="#FF83DB38" Offset="0.872" />
</LinearGradientBrush>
</ToggleButton.Background>
</ToggleButton>
<ToggleButton Height="120" HorizontalAlignment="Left"
Margin
="9,152,0,0" Name="toggleButton4"
VerticalAlignment
="Top" Width="120" Tap="toggleButton_Tap" IsEnabled="True">
<ToggleButton.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#FFE52F2F" Offset="0.134" />
<GradientStop Color="#FF83DB38" Offset="0.872" />
</LinearGradientBrush>
</ToggleButton.Background>
</ToggleButton>
<ToggleButton Height="120" HorizontalAlignment="Left"
Margin
="169,152,0,0" Name="toggleButton5"
VerticalAlignment
="Top" Width="120" Tap="toggleButton_Tap" IsEnabled="True">
<ToggleButton.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#FFE52F2F" Offset="0.134" />
<GradientStop Color="#FF83DB38" Offset="0.872" />
</LinearGradientBrush>
</ToggleButton.Background>
</ToggleButton>
<ToggleButton Height="120" HorizontalAlignment="Left"
Margin
="330,152,0,0" Name="toggleButton6"
VerticalAlignment
="Top" Width="120" Tap="toggleButton_Tap" IsEnabled="True">
<ToggleButton.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#FFE52F2F" Offset="0.134" />
<GradientStop Color="#FF83DB38" Offset="0.872" />
</LinearGradientBrush>
</ToggleButton.Background>
</ToggleButton>
<ToggleButton Height="120" HorizontalAlignment="Left"
Margin
="9,278,0,0" Name="toggleButton7"
VerticalAlignment
="Top" Width="120" Tap="toggleButton_Tap" IsEnabled="True">
<ToggleButton.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#FFE52F2F" Offset="0.134" />
<GradientStop Color="#FF83DB38" Offset="0.872" />
</LinearGradientBrush>
</ToggleButton.Background>
</ToggleButton>
<ToggleButton Height="120" HorizontalAlignment="Left"
Margin
="169,278,0,0" Name="toggleButton8"
VerticalAlignment
="Top" Width="120" Tap="toggleButton_Tap" IsEnabled="True">
<ToggleButton.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#FFE52F2F" Offset="0.134" />
<GradientStop Color="#FF83DB38" Offset="0.872" />
</LinearGradientBrush>
</ToggleButton.Background>
</ToggleButton>
<ToggleButton Height="120" HorizontalAlignment="Left"
Margin
="330,278,0,0" Name="toggleButton9"
VerticalAlignment
="Top" Width="120" Tap="toggleButton_Tap" IsEnabled="True">
<ToggleButton.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#FFE52F2F" Offset="0.134" />
<GradientStop Color="#FF83DB38" Offset="0.872" />
</LinearGradientBrush>
</ToggleButton.Background>
</ToggleButton>
</Canvas>
<RadioButton Content="等级1"
Height
="72" HorizontalAlignment="Left"
Margin
="6,510,0,0" Name="grade1"
VerticalAlignment
="Top" Checked="grade_Checked" />
<RadioButton Content="等级2" Height="72"
HorizontalAlignment
="Left" Margin="135,510,0,0"
Name
="grade2" VerticalAlignment="Top"
IsChecked
="True" Checked="grade_Checked" />
</Grid>
</Grid>

</phone:PhoneApplicationPage>

在这里至于颜色背景什么的就随意啦,反正这只是一个模拟。我们要使用自定义线程来控制UI线程中的显示信息:

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.Threading;
using System.Windows.Controls.Primitives;
using System.Windows.Threading;


namespace 打地鼠
{
public partial class MainPage : PhoneApplicationPage
{
delegate void MyDelegate(int i);
MyDelegate myDelegate;
delegate void MyDelegate2();
MyDelegate2 myDelegate2;

private int totalTapRequired;//需要打击的“地鼠”的数目
private int actualTap;//实际打击的“地鼠”的数目
private double grade = 1.2;//等级默认为1
Random myRandom;//创建一个随机数实例
private string currentToggle;//表示当前袋鼠
//List<Thread> myList;
Thread myThread;
private bool flagOfStartGame = false;//游戏开始的标识
private bool isHaveOne = false;//标识是否有“地鼠”了


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

//开始和结束按钮
private void startOrEnd_Click(object sender, RoutedEventArgs e)
{
if (this.startOrEnd.Content.ToString() == "开始游戏")//开始游戏
{
//myList.Clear();//先清空列表
this.myLog.Text = "";//清空提示信息
this.startOrEnd.Content = "结束游戏";
//初始数据置0
totalTapRequired = 0;
actualTap = 0;
myCanvas.Visibility = Visibility.Visible;//使画布可见,游戏开始
flagOfStartGame = true;//标识开始游戏
}
else if (this.startOrEnd.Content.ToString() == "结束游戏")//结束游戏
{
this.startOrEnd.Content = "开始游戏";
this.myCanvas.Visibility = Visibility.Collapsed;//使画布隐藏
this.myLog.Text = String.Format("您一共打到了{0}只地鼠,成绩是:{1}", actualTap, actualTap * 20);//显示信息
flagOfStartGame = false;//标识关闭游戏

//这种方法行不通
//foreach (Thread t in myList)
//{
// if (t.IsAlive)
// {
// t.Abort();
// }
//}

//if (myThread.IsAlive)
//{
// myThread.Abort();//终止线程
//}
}
}

//页面初始化事件
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
myCanvas.Visibility = Visibility.Collapsed;//页面初始时,使画布不可见,说明游戏未开始
myRandom = new Random();//实例化
myThread = new Thread(new ThreadStart(MonitorOfToggleButton));
myThread.IsBackground = true;//设置为后台运行的线程
myThread.Start();//线程启动
}

//等级的选择
private void grade_Checked(object sender, RoutedEventArgs e)
{
//设计等级
switch(((RadioButton)sender).Content.ToString())
{
case "等级1":
grade = 0.6;
break;
case "等级2":
grade = 1.2;
break ;
}
}

//监视土地,出现“地鼠”
private void MonitorOfToggleButton()
{
while (true)
{
while (flagOfStartGame)
{
if (!isHaveOne)
{
int j = myRandom.Next(1, 10);//取到下线,取不到上限
myDelegate = ChangeUIChecked;//委托
this.Dispatcher.BeginInvoke(myDelegate, j);
totalTapRequired++;//"地"鼠的总数
isHaveOne = true;

}
else
{
myDelegate2 = ChangeUIUnChecked;//委托
this.Dispatcher.BeginInvoke(myDelegate2);
isHaveOne = false;//表示没有“地鼠”
}
Thread.Sleep((int)(grade * 1000));//休眠,模仿规定时间间隔
}

}
}

private void ChangeUIChecked(int i)
{
((ToggleButton)(myCanvas.FindName("toggleButton" + i))).IsChecked = true;//地鼠出现了
currentToggle = ((ToggleButton)(myCanvas.FindName("toggleButton" + i))).Name;//存储当前“地鼠”
}

private void ChangeUIUnChecked()
{
if (((ToggleButton)(myCanvas.FindName(currentToggle))).IsChecked == true)
{
((ToggleButton)(myCanvas.FindName(currentToggle))).IsChecked = false;
}
}

//用户点击事件
private void toggleButton_Tap(object sender, GestureEventArgs e)
{
//此事件发生在用户点击之后,而用户点击之后,toggleButton的值就发生了变化,因此,如果此项是未选中的,则使它仍然未选中,否则,使其选中
if (((ToggleButton)sender).IsChecked == false)
{
((ToggleButton)sender).IsChecked = false;
actualTap++;
//flag = false;//表示又没有“地鼠”了
isHaveOne = false;
//myDispatcherTimer.Stop();//关闭计时
}
else if (((ToggleButton)sender).IsChecked == true)
{
((ToggleButton)sender).IsChecked = false;//返回原先的值
}
}
}
}

在这里我保留了一些自己的调试过程中的信息,也许会对有兴趣的朋友一定的灵感。执行起来就是这样:

posted on 2012-04-07 08:31  WaitingSky  阅读(1470)  评论(1编辑  收藏  举报