Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF/Caliburn Micro中的鼠标处理程序 我已经考虑到了一个简单的情况,但似乎无法弄清楚该怎么做。我对WPF和Caliburn微框架非常陌生。在我看来,我有一个画布,在这个画布上我有一个图像。我想编写一些鼠标事件处理程序(MouseLeftButtonDown、MouseLeftButtonUp和MouseMove),当光标位于图像内部时调用它们。我已经成功地在ViewModel中创建了事件处理程序,但是我似乎不知道如何在处理程序中获取当前光标位置。MouseEventArgs.GetPosition需要一个IInputElement作为参数…不知道如何获取它_Wpf_Mouseevent_Caliburn.micro - Fatal编程技术网

WPF/Caliburn Micro中的鼠标处理程序 我已经考虑到了一个简单的情况,但似乎无法弄清楚该怎么做。我对WPF和Caliburn微框架非常陌生。在我看来,我有一个画布,在这个画布上我有一个图像。我想编写一些鼠标事件处理程序(MouseLeftButtonDown、MouseLeftButtonUp和MouseMove),当光标位于图像内部时调用它们。我已经成功地在ViewModel中创建了事件处理程序,但是我似乎不知道如何在处理程序中获取当前光标位置。MouseEventArgs.GetPosition需要一个IInputElement作为参数…不知道如何获取它

WPF/Caliburn Micro中的鼠标处理程序 我已经考虑到了一个简单的情况,但似乎无法弄清楚该怎么做。我对WPF和Caliburn微框架非常陌生。在我看来,我有一个画布,在这个画布上我有一个图像。我想编写一些鼠标事件处理程序(MouseLeftButtonDown、MouseLeftButtonUp和MouseMove),当光标位于图像内部时调用它们。我已经成功地在ViewModel中创建了事件处理程序,但是我似乎不知道如何在处理程序中获取当前光标位置。MouseEventArgs.GetPosition需要一个IInputElement作为参数…不知道如何获取它,wpf,mouseevent,caliburn.micro,Wpf,Mouseevent,Caliburn.micro,以下是我的XAML(视图): 一旦我这样做了,我需要将鼠标坐标转换为图像上的坐标…但首先要做的是 谢谢 我相信我已经解决了 在我的XAML中,我将事件处理程序附加到Image对象上的事件。因此,事件处理程序中的发送方可以转换为Image类型。然后可以将发送方作为参数传递给GetPosition 此外,我还能够将发送的坐标转换为实际图像坐标。知道原始图像的像素大小,这只是一些简单的缩放数学 以下是我的一个鼠标处理程序的代码: public void MouseDown_Image(object s

以下是我的XAML(视图):

一旦我这样做了,我需要将鼠标坐标转换为图像上的坐标…但首先要做的是


谢谢

我相信我已经解决了

在我的XAML中,我将事件处理程序附加到Image对象上的事件。因此,事件处理程序中的发送方可以转换为Image类型。然后可以将发送方作为参数传递给GetPosition

此外,我还能够将发送的坐标转换为实际图像坐标。知道原始图像的像素大小,这只是一些简单的缩放数学

以下是我的一个鼠标处理程序的代码:

public void MouseDown_Image(object sender, MouseEventArgs e)
   {

      // Get the x and y coordinates of the mouse pointer.
       System.Windows.Point position = e.GetPosition((Image)sender);
       int pX = (int)position.X;
       int pY = (int)position.Y;

     // DataImage is the actual Image object that is being shown in the view
       int X_ImageCoordinates = (int)(DataImage.PixelWidth * (position.X/img.ActualWidth));
       int Y_ImageCoordinates = (int)(DataImage.PixelHeight * (position.Y / img.ActualHeight));                
    }
原来很简单!应该如此

请告诉我是否有更好的解决方案,或者是否出于某种原因我犯了错误


谢谢

为了处理图像的坐标,可能需要创建一个
SpecialValues
字典条目-通过这种方式,您可以在中心位置添加功能,甚至只在图像或其他缩放输入控件中的X/Y位置:

e、 g.在CM引导程序中配置:

            MessageBinder.SpecialValues.Add("$scaledmousex", (ctx) =>
            {
                var img = ctx.Source as Image;
                var input = ctx.Source as IInputElement;
                var e = ctx.EventArgs as MouseEventArgs;

                // If there is an image control, get the scaled position
                if (img != null && e != null)
                {
                    Point position = e.GetPosition(img);
                    return (int)(img.Source.Width * (position.X / img.ActualWidth));
                }

                // If there is another type of of IInputControl get the non-scaled position - or do some processing to get a scaled position, whatever needs to happen
                if (e != null && input != null)
                    return e.GetPosition(input).X;

                // Return 0 if no processing could be done
                return 0;
            });
此字典用于解析操作消息绑定中的输入参数,并将运行上述匿名委托,返回您指定的值(您还需要添加
$scaledmousey

然后在绑定中使用此值:

cal:Message.Attach=“[Event MouseMove]=[Action MouseMove\u Image($scaledmousex,$scaledmousey)]”

这样,如果您决定更改源代码类型,您仍然可以在不进行太多/任何重构的情况下获得该位置,并且您的代码通过一条中央处理路径


这也确保了视图/视图模型是解耦的——MVVM模式目标的一部分

您刚刚使MVVM模型无效。MVVM模型的全部要点是将视图与模型分开。通过将事件传递给模型,您破坏了它。如果您确实需要处理鼠标事件,请在视图中执行。非常感谢。看起来我还有很多关于MVVM、CM和WPF的知识需要学习。这不是MVVM的内容。从不在ViewModel中创建事件处理程序。如果要处理鼠标事件,请在视图中执行。如果要将鼠标事件转换为特定操作,请创建一个转换器,将事件转换为该操作。Charleh-感谢您的响应。我从收到的其他响应中看到,我通过在操作中传递eventArgs破坏了MVVM。你的建议似乎解决了这个问题。我很快意识到,我需要备份和研究更多关于WPF和MVVM的内容。再次感谢您的友好和详细的回复。有时似乎唯一的方法是从VM中引用已知类型的视图,但大多数情况下都有一种机制可以避免这种情况,而且通常CM内置了这种机制。我总是问自己“这段代码如何确保我们不耦合这些组件”,通常通过它来帮助我找到一个替代方案——通常是一个更干净、更简单的实现和更好的分离!祝你好运!但是,我有一个问题:-)您上面提供的代码是否在CM引导程序类中?当我这样做时,我会得到一个错误:“Caliburn.Micro.MessageBinder.SpecialValues是一个字段,但被用作类型”。我缺少什么?应该进入你的
引导程序。Configure
method-
SpecialValues
MessageBinder
类(iirc它是
字典
)上的一个静态字段,因此代码只需向字典添加一个键和一个回调
public void MouseDown_Image(object sender, MouseEventArgs e)
   {

      // Get the x and y coordinates of the mouse pointer.
       System.Windows.Point position = e.GetPosition((Image)sender);
       int pX = (int)position.X;
       int pY = (int)position.Y;

     // DataImage is the actual Image object that is being shown in the view
       int X_ImageCoordinates = (int)(DataImage.PixelWidth * (position.X/img.ActualWidth));
       int Y_ImageCoordinates = (int)(DataImage.PixelHeight * (position.Y / img.ActualHeight));                
    }
            MessageBinder.SpecialValues.Add("$scaledmousex", (ctx) =>
            {
                var img = ctx.Source as Image;
                var input = ctx.Source as IInputElement;
                var e = ctx.EventArgs as MouseEventArgs;

                // If there is an image control, get the scaled position
                if (img != null && e != null)
                {
                    Point position = e.GetPosition(img);
                    return (int)(img.Source.Width * (position.X / img.ActualWidth));
                }

                // If there is another type of of IInputControl get the non-scaled position - or do some processing to get a scaled position, whatever needs to happen
                if (e != null && input != null)
                    return e.GetPosition(input).X;

                // Return 0 if no processing could be done
                return 0;
            });