如何在鼠标点击之间共享事件处理程序? [英] How do you Share Event Handlers between mouse clicks?

查看:76
本文介绍了如何在鼠标点击之间共享事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试清理这个并且通常的私有空转转(String knobName)与turnknob配对(one.ToString());不工作。有什么建议?目标是从每次鼠标点击引用turnknob方法,只需更换knobname。而不是在所有鼠标单击事件中重复相同的代码。任何帮助将不胜感激!



就这样我觉得我也做出了贡献,如果你在你的窗口放一个日食并扔一个圆形的图像它上面的按钮,使用此代码,当您单击它时它会旋转。当你再次点击时旋转回来。



I'm trying to clean this up and the usual "private void turnknob(string knobName) paired with turnknob("one".ToString());" isn't working. Any suggestions? The objective is to reference the turnknob method from each mouse click and just replacing the knobname. Instead of repeating the same code in all the Mouse click events. Any help would be greatly appreciated!

Just so I can feel like I've contributed too, if you place an eclipse in your window and throw an image of a round button on it, with this code, it spins around when you click it. And spins back when you click again.

namespace ShareEventHandler
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
 
        private bool knobLoc = false;
 
        private void turnknob(string knobName)
        {
            if (knobLoc == false)
            {
                DoubleAnimation da = new DoubleAnimation(0, 360, new Duration(TimeSpan.FromSeconds(1)));
                RotateTransform rt = new RotateTransform();
                knobName.RenderTransform = rt;
                knobName.RenderTransformOrigin = new Point(0.5, 0.5);
                rt.BeginAnimation(RotateTransform.AngleProperty, da);
                rt.BeginAnimation(RotateTransform.AngleProperty, da);
                knobLoc = true;
            }
            else
            {
                DoubleAnimation da = new DoubleAnimation(360, 0, new Duration(TimeSpan.FromSeconds(1)));
                RotateTransform rt = new RotateTransform();
                knobName.RenderTransform = rt;
                knobName.RenderTransformOrigin = new Point(0.5, 0.5);
                rt.BeginAnimation(RotateTransform.AngleProperty, da);
                rt.BeginAnimation(RotateTransform.AngleProperty, da);
                knobLoc = false;
            }
        }
 
        private void one_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            turnknob("one".ToString());
        }
 
        private void two_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            turnknob("two".ToString());
        }
 
        private void three_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            turnknob("three".ToString());
        }
    }
}

推荐答案

据我所见,你可以使用同样的所有旋钮的事件处理程序。



尝试将以下内容添加到构造函数中

As far as I can see you can use the same event handler for all of the knobs.

Try adding the following to the constructor
one.MouseLeftButtonDown += general_MouseLeftButtonDown;
two.MouseLeftButtonDown += general_MouseLeftButtonDown;
three.MouseLeftButtonDown += general_MouseLeftButtonDown;



并添加新的事件处理程序


And add the new event handler

private void general_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
   turnknob(((Control)sender).Name);
}


也许我应该从头开始。当你点击它时,我有3个旋钮。旋钮转动并向后转动。但是必须有一种不那么冗余的写作方式。我原来的帖子是我试图清理它,但显然不起作用。



除了旋钮的名称之外,所有3个代码块都是相同的,你如何将所有三个代码放在一个方法之下并根据哪个更改名称旋钮你点击?肯定会赞赏这些例子。



Mika,我觉得你的解决方案可能在正确的道路上,但我不知道如何处理顶部



这是代码的样子:



Maybe i should start at the beginning. I have 3 knobs that turn when you click them. The knobs turn and turn back like they should. But there's got to be a less redundant way of writing this. My original post was my attempt at cleaning it up, but obviously not working.

With all 3 chunks of code being the same, aside from the name of the knob, how would you put all three under one method and change the name depending on which knob you click? Examples would definitely be appreciated.

Mika, I feel like your solution might be on the right path, but I don't know what to do with the top portion

Here's what the code looks like [edited a bit]:

private bool knobLoc = false;

private void one_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    DoubleAnimation da = new DoubleAnimation((knobLoc ? 360 : 0), (knobLoc ? 0 : 360), new Duration(TimeSpan.FromSeconds(0.5)));
    RotateTransform rt = new RotateTransform();

    if (!knobLoc) { knobLoc = !knobLoc; }

    else { knobLoc = false; }

    one.RenderTransformOrigin = new Point(0.5, 0.5);
    one.RenderTransform = rt;
    rt.BeginAnimation(RotateTransform.AngleProperty, da);
}

private void two_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    DoubleAnimation da = new DoubleAnimation((knobLoc ? 360 : 0), (knobLoc ? 0 : 360), new Duration(TimeSpan.FromSeconds(0.5)));
    RotateTransform rt = new RotateTransform();

    if (!knobLoc) { knobLoc = !knobLoc; }

    else { knobLoc = false; }

    two.RenderTransformOrigin = new Point(0.5, 0.5);
    two.RenderTransform = rt;
    rt.BeginAnimation(RotateTransform.AngleProperty, da);
}

private void three_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    DoubleAnimation da = new DoubleAnimation((knobLoc ? 360 : 0), (knobLoc ? 0 : 360), new Duration(TimeSpan.FromSeconds(0.5)));
    RotateTransform rt = new RotateTransform();

    if (!knobLoc) { knobLoc = !knobLoc; }

    else { knobLoc = false; }

    three.RenderTransformOrigin = new Point(0.5, 0.5);
    three.RenderTransform = rt;
    rt.BeginAnimation(RotateTransform.AngleProperty, da);
}


这篇关于如何在鼠标点击之间共享事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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