我坚持从另一个点击事件中获取一个值 [英] I'm stuck with getting a value back from another click event

查看:94
本文介绍了我坚持从另一个点击事件中获取一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经开始构建记忆卡游戏,但我从第二次点击的按钮中恢复了一个值,但我首先要写的是我计划的
游戏:


单击按钮后,图像将在按钮的同一位置显示5秒钟,在那5秒钟您必须单击另一个按钮,如果您没有点击事件将结束,如果您选择匹配的图片 - 他们都永远保持开放,但如果你打开另一张图片,他们都会再出现3秒。



我已经尝试解决这个问题,我不只是跑到这里,我会坐两天探索选项来做到这一点。



到目前为止我做了什么:我做了一个dictonary,其中"Button"是键,"String"是图片的名称,我打算做的是:当点击2张图片时 - 第一次点击将获得按钮的名称并返回一个字符串值
,它将与该按钮的字符串值进行比较,并且如果他们匹配,他们都保持开放。



因为我无法发布截图,直到msdn将验证我的帐户,这是xaml代码:



< Page 
x:Class =" MemoryGame.MainPage"
xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x =" http://schemas.microsoft.com/ winfx / 2006 / xaml"
xmlns:local =" using:MemoryGame"
xmlns:d =" http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc =" http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable =" d">

< Page.Resources>
< Style TargetType ="图片" x:Key =" HideableImages">
< Setter Property =" Visibility" Value =" Collapsed" />
< ; Setter Property =" Horizo​​ntalAlignment" Value =" Stretch" />
< Setter Property =" VerticalAlignment" Value =" Stretch" />
< Setter Property =" Stretch"值= QUOT;填写" />
< / Style>

< Style x:Key =" QuesionMarkButton"的TargetType = QUOT;按钮和QUOT;>
< Setter Property =" Horizo​​ntalAlignment"值= QUOT;拉伸" />
< Setter Property =" VerticalAlignment"值= QUOT;拉伸" />
< Setter Property =" BorderBrush"值= QUOT;黑色" />
< Setter Property =" FontFamily"值="Segoe MDL2资产"/>
< Setter Property =" Content"值= QUOT;&安培;#xE897;" />
< Setter Property =" FontSize"值= QUOT; 72" />
< / Style>
< /Page.Resources>

< Grid Background =" {ThemeResource ApplicationPageBackgroundThemeBrush}">
< Grid.ColumnDefinitions>
< ColumnDefinition Width =" 1 *" />
< ColumnDefinition Width =" 1 *" />
< ColumnDefinition Width =" 1 *" />
< ColumnDefinition Width =" 1 *" />
< /Grid.ColumnDefinitions>
< Grid.RowDefinitions>
< RowDefinition Height =" 1 *" />
< RowDefinition Height =" 2 *" />
< RowDefinition Height =" 2 *" />
< RowDefinition Height =" 2 *" />
< RowDefinition Height =" 2 *" />
< /Grid.RowDefinitions>

< TextBlock Name =" Title"字号= QUOT; 36英寸; Text ="Ido levi的记忆游戏" Grid.ColumnSpan = QUOT; 4英寸; />

< Button Name =" R1C1" BorderThickness =" 0,0,3,3" Style =" {StaticResource QuesionMarkButton}" Grid.Row = QUOT 1 QUOT; Grid.Column = QUOT; 0"点击= QUOT; R1C1_Click" PointerPressed = QUOT; R1C1pressed" />
< Button Name =" R1C2"了borderThickness = QUOT; 3,0,3,3" Style =" {StaticResource QuesionMarkButton}" Grid.Row = QUOT 1 QUOT; Grid.Column = QUOT 1 QUOT;点击= QUOT; R1C2_Click_1" PointerPressed = QUOT; R1C2_PointerPressed" />
< Button Name =" R1C3"了borderThickness = QUOT; 3,0,3,3" Style =" {StaticResource QuesionMarkButton}" Grid.Row = QUOT 1 QUOT; Grid.Column = QUOT; 2英寸/>
< Button Name =" R1C4"了borderThickness = QUOT; 3,0,0,3" Style =" {StaticResource QuesionMarkButton}" Grid.Row = QUOT 1 QUOT; Grid.Column = QUOT; 3英寸/>
< Button Name =" R2C1"了borderThickness = QUOT; 0,3,3,3" Style =" {StaticResource QuesionMarkButton}" Grid.Row = QUOT; 2英寸Grid.Column = QUOT; 0" />
< Button Name =" R2C2"了borderThickness = QUOT; 3,3,3,3" Style =" {StaticResource QuesionMarkButton}" Grid.Row = QUOT; 2英寸Grid.Column = QUOT 1 QUOT; />
< Button Name =" R2C3"了borderThickness = QUOT; 3,3,3,3" Style =" {StaticResource QuesionMarkButton}" Grid.Row = QUOT; 2英寸Grid.Column = QUOT; 2英寸/>
< Button Name =" R2C4"了borderThickness = QUOT; 3,3,0,3" Style =" {StaticResource QuesionMarkButton}" Grid.Row = QUOT; 2英寸Grid.Column = QUOT; 3英寸/>
< Button Name =" R3C1"了borderThickness = QUOT; 0,3,3,3" Style =" {StaticResource QuesionMarkButton}" Grid.Row = QUOT; 3英寸Grid.Column = QUOT; 0" />
< Button Name =" R3C2"了borderThickness = QUOT; 3,3,3,3" Style =" {StaticResource QuesionMarkButton}" Grid.Row = QUOT; 3英寸Grid.Column = QUOT 1 QUOT; />
< Button Name =" R3C3"了borderThickness = QUOT; 3,3,3,3" Style =" {StaticResource QuesionMarkButton}" Grid.Row = QUOT; 3英寸Grid.Column = QUOT; 2英寸/>
< Button Name =" R3C4"了borderThickness = QUOT; 3,3,0,3" Style =" {StaticResource QuesionMarkButton}" Grid.Row = QUOT; 3英寸Grid.Column = QUOT; 3英寸/>
< Button Name =" R4C1"了borderThickness = QUOT; 0,3,3,0" Style =" {StaticResource QuesionMarkButton}" Grid.Row = QUOT; 4英寸Grid.Column = QUOT; 0" />
< Button Name =" R4C2"了borderThickness = QUOT; 3,3,3,0" Style =" {StaticResource QuesionMarkButton}" Grid.Row = QUOT; 4英寸Grid.Column = QUOT 1 QUOT; />
< Button Name =" R4C3"了borderThickness = QUOT; 3,3,3,0" Style =" {StaticResource QuesionMarkButton}" Grid.Row = QUOT; 4英寸Grid.Column = QUOT; 2英寸/>
< Button Name =" R4C4"了borderThickness = QUOT; 3,3,0,0" Style =" {StaticResource QuesionMarkButton}" Grid.Row = QUOT; 4英寸Grid.Column = QUOT; 3英寸/>

< Image Name =" R1C1img" Grid.Row = QUOT 1 QUOT; Grid.Column = QUOT; 0" Style =" {StaticResource HideableImages}"源= QUOT;资产/ OriginalPepe.jpg" />
< Image Name =" R1C2img" Grid.Row = QUOT 1 QUOT; Grid.Column = QUOT 1 QUOT; Style =" {StaticResource HideableImages}"源= QUOT;资产/ PixelPepe.jpg" />
< Image Name =" R1C3img" Grid.Row = QUOT 1 QUOT; Grid.Column = QUOT; 2英寸Style =" {StaticResource HideableImages}"源= QUOT;资产/ IronManPepe.jpg" />
< Image Name =" R1C4img" Grid.Row = QUOT 1 QUOT; Grid.Column = QUOT; 3英寸Style =" {StaticResource HideableImages}"源= QUOT;资产/ CoolPepe.jpg" />

< Image Name =" R2C1img" Grid.Row = QUOT; 2英寸Grid.Column = QUOT; 0" Style =" {StaticResource HideableImages}"源= QUOT;资产/ UrselfPepe.png" />
< Image Name =" R2C2img" Grid.Row = QUOT; 2英寸Grid.Column = QUOT 1 QUOT; Style =" {StaticResource HideableImages}"源= QUOT;资产/ WinkPepe.jpg" />
< Image Name =" R2C3img" Grid.Row = QUOT; 2英寸Grid.Column = QUOT; 2英寸Style =" {StaticResource HideableImages}"源= QUOT;资产/ OriginalPepe.jpg" />
< Image Name =" R2C4img" Grid.Row = QUOT; 2英寸Grid.Column = QUOT; 3英寸Style =" {StaticResource HideableImages}"源= QUOT;资产/ HappyPepe.jpg" />

< Image Name =" R3C1img" Grid.Row = QUOT; 3英寸Grid.Column = QUOT; 0" Style =" {StaticResource HideableImages}"源= QUOT;资产/ PixelPepe.jpg" />
< Image Name =" R3C2img" Grid.Row = QUOT; 3英寸Grid.Column = QUOT 1 QUOT; Style =" {StaticResource HideableImages}"源= QUOT;资产/ MonsterPepe.jpg" />
< Image Name =" R3C3img" Grid.Row = QUOT; 3英寸Grid.Column = QUOT; 2英寸Style =" {StaticResource HideableImages}"源= QUOT;资产/ CoolPepe.jpg" />
< Image Name =" R3C4img" Grid.Row = QUOT; 3英寸Grid.Column = QUOT; 3英寸Style =" {StaticResource HideableImages}"源= QUOT;资产/ IronManPepe.jpg" />

< Image Name =" R4C1img" Grid.Row = QUOT; 4英寸Grid.Column = QUOT; 0" Style =" {StaticResource HideableImages}"源= QUOT;资产/ WinkPepe.jpg" />
< Image Name =" R4C2img" Grid.Row = QUOT; 4英寸Grid.Column = QUOT 1 QUOT; Style =" {StaticResource HideableImages}"源= QUOT;资产/ UrselfPepe.png" />
< Image Name =" R4C3img" Grid.Row = QUOT; 4英寸Grid.Column = QUOT; 2英寸Style =" {StaticResource HideableImages}"源= QUOT;资产/ MonsterPepe.jpg" />
< Image Name =" R4C4img" Grid.Row = QUOT; 4英寸Grid.Column = QUOT; 3英寸Style =" {StaticResource HideableImages}"源= QUOT;资产/ HappyPepe.jpg" />





< / Grid>
< / Page>



这是一个4x4按钮,我现在要做的是:



按下1按钮后,该按钮将激活5秒钟,在这5秒内,他必须抓住另一个单击的按钮,如果没有,则禁用该事件。



所以我的问题是:



- 如何抓住按钮点击事件?



- 如何将计时器设置为5秒钟,在5秒钟内,我可以点击另一个按钮(我尝试使用异步await by:  await System.Threading.Tasks.Task.Delay(5000);,它会停止5秒钟,但是在那5秒钟我无法按下另一个
按钮

解决方案

1)将事件处理程序附加到所有按钮,就像您对第一个按钮所做的那样,并使用全局变量来记住按下的内容。


2)正在寻找Threadpool计时器。



https://msdn.microsoft。 COM /库/窗/应用/ windows.system.threading.threadpooltimer.aspx F = 255安培; MSPPError = -2147217396&安培; CS-保存琅= 1和CS-LANG = CSHARP#代码片断-1


I've started to build an Memory Card game, but I got stuck with retrving a value from the second clicked button, but first I'll write what I planed the game to be like:

After you click a button, an Image will apper at the same place of the button for 5 seconds, at those 5 seconds you have to click another button, if you don't the click event will end, if you pick a matching picture - they both stay open forever, but if you open another picture they both will appear for extra 3 seconds.

I already tried to solve that, I'm not just running to here, I'm sitting 2 days exploring options to do that.

What have I done so far: I've made a dictonary which "Button" is the key and "String" is the name of the picture, what I've planned to do is: When 2 pictures are clicked - the first click would get the Button's name and return a string value which will be compared to the string value of that button, and if they match, they both stay open.

Because I can't post a screenshot untill msdn will verify my account, that's the xaml code:

<Page
    x:Class="MemoryGame.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MemoryGame"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Page.Resources>
        <Style TargetType="Image" x:Key="HideableImages">
            <Setter Property="Visibility" Value="Collapsed" />
            <Setter Property="HorizontalAlignment" Value="Stretch" />
            <Setter Property="VerticalAlignment" Value="Stretch" />
            <Setter Property="Stretch" Value="Fill" />
        </Style>

        <Style x:Key="QuesionMarkButton" TargetType="Button">
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="VerticalAlignment" Value="Stretch"/>
            <Setter Property="BorderBrush" Value="Black"/>
            <Setter Property="FontFamily" Value="Segoe MDL2 Assets"/>
            <Setter Property="Content" Value="&#xE897;"/>
            <Setter Property="FontSize" Value="72"/>
        </Style>
    </Page.Resources>

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="2*"/>
        </Grid.RowDefinitions>

        <TextBlock Name="Title" FontSize="36" Text="Memory game by Ido levi" Grid.ColumnSpan="4"/>

        <Button Name="R1C1" BorderThickness ="0,0,3,3" Style="{StaticResource QuesionMarkButton}" Grid.Row="1" Grid.Column="0" Click="R1C1_Click"    PointerPressed="R1C1pressed" />
        <Button Name="R1C2" BorderThickness="3,0,3,3" Style="{StaticResource QuesionMarkButton}" Grid.Row="1" Grid.Column="1" Click="R1C2_Click_1" PointerPressed="R1C2_PointerPressed"/>
        <Button Name="R1C3" BorderThickness="3,0,3,3" Style="{StaticResource QuesionMarkButton}" Grid.Row="1" Grid.Column="2"  />
        <Button Name="R1C4" BorderThickness="3,0,0,3" Style="{StaticResource QuesionMarkButton}" Grid.Row="1" Grid.Column="3" />
        <Button Name="R2C1" BorderThickness="0,3,3,3" Style="{StaticResource QuesionMarkButton}" Grid.Row="2" Grid.Column="0" />
        <Button Name="R2C2" BorderThickness="3,3,3,3" Style="{StaticResource QuesionMarkButton}" Grid.Row="2" Grid.Column="1" />
        <Button Name="R2C3" BorderThickness="3,3,3,3" Style="{StaticResource QuesionMarkButton}" Grid.Row="2" Grid.Column="2" />
        <Button Name="R2C4" BorderThickness="3,3,0,3" Style="{StaticResource QuesionMarkButton}" Grid.Row="2" Grid.Column="3" />
        <Button Name="R3C1" BorderThickness="0,3,3,3" Style="{StaticResource QuesionMarkButton}" Grid.Row="3" Grid.Column="0" />
        <Button Name="R3C2" BorderThickness="3,3,3,3" Style="{StaticResource QuesionMarkButton}" Grid.Row="3" Grid.Column="1" />
        <Button Name="R3C3" BorderThickness="3,3,3,3" Style="{StaticResource QuesionMarkButton}" Grid.Row="3" Grid.Column="2" />
        <Button Name="R3C4" BorderThickness="3,3,0,3" Style="{StaticResource QuesionMarkButton}" Grid.Row="3" Grid.Column="3" />
        <Button Name="R4C1" BorderThickness="0,3,3,0" Style="{StaticResource QuesionMarkButton}" Grid.Row="4" Grid.Column="0" />
        <Button Name="R4C2" BorderThickness="3,3,3,0" Style="{StaticResource QuesionMarkButton}" Grid.Row="4" Grid.Column="1" />
        <Button Name="R4C3" BorderThickness="3,3,3,0" Style="{StaticResource QuesionMarkButton}" Grid.Row="4" Grid.Column="2" />
        <Button Name="R4C4" BorderThickness="3,3,0,0" Style="{StaticResource QuesionMarkButton}" Grid.Row="4" Grid.Column="3" />

        <Image Name="R1C1img"  Grid.Row="1" Grid.Column="0" Style="{StaticResource HideableImages}" Source="Assets/OriginalPepe.jpg"/>
        <Image Name="R1C2img"  Grid.Row="1" Grid.Column="1" Style="{StaticResource HideableImages}" Source="Assets/PixelPepe.jpg"/>
        <Image Name="R1C3img"  Grid.Row="1" Grid.Column="2" Style="{StaticResource HideableImages}" Source="Assets/IronManPepe.jpg"/>
        <Image Name="R1C4img"  Grid.Row="1" Grid.Column="3" Style="{StaticResource HideableImages}" Source="Assets/CoolPepe.jpg"/>
     
        <Image Name="R2C1img"  Grid.Row="2" Grid.Column="0" Style="{StaticResource HideableImages}" Source="Assets/UrselfPepe.png"/>
        <Image Name="R2C2img"  Grid.Row="2" Grid.Column="1" Style="{StaticResource HideableImages}" Source="Assets/WinkPepe.jpg"/>
        <Image Name="R2C3img"  Grid.Row="2" Grid.Column="2" Style="{StaticResource HideableImages}" Source="Assets/OriginalPepe.jpg"/>
        <Image Name="R2C4img"  Grid.Row="2" Grid.Column="3" Style="{StaticResource HideableImages}" Source="Assets/HappyPepe.jpg"/>
        
        <Image Name="R3C1img"  Grid.Row="3" Grid.Column="0" Style="{StaticResource HideableImages}" Source="Assets/PixelPepe.jpg"/>
        <Image Name="R3C2img"  Grid.Row="3" Grid.Column="1" Style="{StaticResource HideableImages}" Source="Assets/MonsterPepe.jpg"/>
        <Image Name="R3C3img"  Grid.Row="3" Grid.Column="2" Style="{StaticResource HideableImages}" Source="Assets/CoolPepe.jpg"/>
        <Image Name="R3C4img"  Grid.Row="3" Grid.Column="3" Style="{StaticResource HideableImages}" Source="Assets/IronManPepe.jpg"/>
        
        <Image Name="R4C1img"  Grid.Row="4" Grid.Column="0" Style="{StaticResource HideableImages}" Source="Assets/WinkPepe.jpg"/>
        <Image Name="R4C2img"  Grid.Row="4" Grid.Column="1" Style="{StaticResource HideableImages}" Source="Assets/UrselfPepe.png"/>
        <Image Name="R4C3img"  Grid.Row="4" Grid.Column="2" Style="{StaticResource HideableImages}" Source="Assets/MonsterPepe.jpg"/>
        <Image Name="R4C4img"  Grid.Row="4" Grid.Column="3" Style="{StaticResource HideableImages}" Source="Assets/HappyPepe.jpg"/>





    </Grid>
</Page>

That's a 4x4 buttons, what I'm trying to do now is:

After pressing 1 button, that button would be active for 5 seconds and whithin those 5 seconds, he have to catch another clicked button and if not, disable the event.

So my questions is:

- How can I catch a button clicked event?

- How to set the timer for 5 seconds and whithin those 5 seconds, I would be able to click another button (I tried using async await by: await System.Threading.Tasks.Task.Delay(5000);, it does stop for 5 seconds but at those 5 seconds I couldnt press another buttons

解决方案

1) Attach event handlers to all buttons, like you did for the first one, and use global variables to remember what was pressed.

2) Threadpool timers are what you are looking for.

https://msdn.microsoft.com/library/windows/apps/windows.system.threading.threadpooltimer.aspx?f=255&MSPPError=-2147217396&cs-save-lang=1&cs-lang=csharp#code-snippet-1


这篇关于我坚持从另一个点击事件中获取一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆