处理按钮外的click事件 [英] handle click event outside of button

查看:103
本文介绍了处理按钮外的click事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过复制Apple AppStore中的App来练习c#.

I am trying to practice c# by reproduce an App that is in the Apple AppStore.

在应用程序中,有一个矩形,文本为:"Touch me".触摸它时,矩形将重新定位.

In the app, there is a rectangle with the text: "Touch me". When you touch it, the rectangle repositions itself.

执行几次此操作后,文本将更改为请勿触摸我".在这种情况下,您必须触摸矩形的外部.

After you do this a few times, the text changes to "Do not Touch me". In that case you have to Touch outside of the rectangle.

一切顺利,直到必须在矩形外部触摸为止.

It all went well, up to the point where you have to touch outside the rectangle.

这是我的事件处理程序:

Here is my event handler:

    private void Canvas_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
    {
        if (click == 0)
        {
            if (rectangle1.IsMouseOver || textBlock1.IsMouseOver)
            {
               // reposition and stuff
                if (clicks == 10)
                {
                    // Change the value of the variable click to 1 
                    click = 1;
                    textBlock1.Text = "Do Not Click me";
                    Canvas.SetLeft(textBlock1, 200);
                }
            }
        }
        else
        {
            if (rectangle1.IsMouseOver || textBlock1.IsMouseOver)
            {
                // Game Over
                this.Close();
            } else
            {
                // reposition and stuff

                click = 0;
                textBlock1.Text = "Click me";
                Canvas.SetLeft(textBlock1, 225);
            }
        }
    }

该程序可以完美运行,直到您必须在矩形外部单击为止.

The program works perfectly up to the point where you have to click outside the rectangle.

当您单击矩形时,该程序关闭,但是当您单击矩形外部时,什么也没有发生. 有没有可以处理我想要的任务的事件处理程序?

The program closes when you click on the rectangle but when you click outside it, nothing happens. Is there any event-handler that can do the task i want?

这是我的xaml

<Window x:Class="ClickMe.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="510" Width="525" ResizeMode="NoResize">
<Canvas Name="canvas" MouseLeftButtonDown="Canvas_MouseLeftButtonDown_1">
    <Rectangle Fill="#FFF4F4F5" Name="rectangle1" HorizontalAlignment="Left" Height="38" Stroke="Black" VerticalAlignment="Top" Width="509" Canvas.Left="0" Canvas.Top="63"/>
    <Label Name="label1" Content="0" Canvas.Left="57" Canvas.Top="446"/>
    <Label Content="Klicks:" Canvas.Left="10" Canvas.Top="446"/>
    <TextBlock Name="textBlock1" Canvas.Left="225" TextWrapping="Wrap" Text="Click Me" Canvas.Top="74" Margin="10,0,0,0"/>

</Canvas>

推荐答案

您实际上只需要设置Canvas的Background,因为它仅在具有渲染内容"的位置获得鼠标输入.背景甚至可以是透明的:

You really just need to set the Background of the Canvas, as it only gets mouse input where it has "rendered content". The background could even be transparent:

<Canvas Name="canvas" Background="Transparent"
        MouseLeftButtonDown="Canvas_MouseLeftButtonDown_1">
    ...
</Canvas>

这篇关于处理按钮外的click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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