问题描述
我正在尝试清理这个并且通常的私有空转转(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());
}
}
}
推荐答案
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);
}
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);
}
这篇关于如何在鼠标点击之间共享事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!