Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.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

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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.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
C# 在包含两个图像的画布上更改UIElement.CaptureMouse()的调用顺序时,我得到了不同的结果_C#_Wpf - Fatal编程技术网

C# 在包含两个图像的画布上更改UIElement.CaptureMouse()的调用顺序时,我得到了不同的结果

C# 在包含两个图像的画布上更改UIElement.CaptureMouse()的调用顺序时,我得到了不同的结果,c#,wpf,C#,Wpf,我现在正试图创建一个简单的图像查看器,它带有平移和缩放功能,用于研究WPF 我编写的XAML代码如下: <Window x:Class="PanningImageTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmln

我现在正试图创建一个简单的图像查看器,它带有平移和缩放功能,用于研究WPF

我编写的XAML代码如下:

<Window x:Class="PanningImageTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:PanningImageTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Border Name="panBorder" ClipToBounds="True">
            <Canvas Name="panImage" MouseLeftButtonDown="Image_MouseLeftButtonDown"
                MouseMove="Image_MouseMove"
                MouseLeftButtonUp="Image_MouseLeftButtonUp">
                <!-- Two Image is intended, as in real situation,
                     they will use different sources -->
                <Image Source="Wallpaper.jpg" Stretch="None"/>
                <Image Source="Wallpaper.jpg" Stretch="None"/>
            </Canvas>
        </Border>
    </Grid>
</Window>
如果我在
\u start=>之前调用
this.panImage.CaptureMouse()
。。。等等…
行,只要我点击图像上的某个地方,所有的图像就会传送到鼠标的位置

像这样:

但是,如果我在
行之后调用相同的函数,如下面所示,它只会按预期工作:

    _start = e.GetPosition(this.panBorder);
    _origin.X = this.panImage.RenderTransform.Value.OffsetX;
    _origin.Y = this.panImage.RenderTransform.Value.OffsetY;

    this.panImage.CaptureMouse();
像这样:

如果我在
Canvas
中只使用一个
Image
标记,那么它在这两种情况下都能很好地工作。 我尝试更改.NET版本,将事件移动到
Border
而不是
Canvas
,但一切都无法解释这些结果


我不知道为什么会这样。有人能解释一下吗?

MouseMove
事件已经开始触发,即使
MouseLeftButtonDown
方法尚未完成

因此,在最后一行调用
CaptureMouse()方法
,并使用以下检查作为保护,以防止在尚未获得\u origin的值时拖动图像

if (!this.panImage.IsMouseCaptured)
{
    return;
}

在开始计算偏移之前,需要设置起始位置和坐标。否则你会得到错误的补偿。所以在你做这件事之前抓到老鼠是没有意义的

请注意,
MouseMove
事件将在您移动鼠标时立即引发(基本上一直如此),但在捕获它之前,事件处理程序将通过设置其
RenderTransform
属性返回,而不移动图像


因此,您应该设置开始位置以及X和Y坐标,然后根据这些坐标移动图像。而不是相反。

这是一个很好的解释。但我有一个问题。如果我在XAML中去掉画布,只留下一个图像,比如,并在图像上处理鼠标事件,比如,即使我在计算_origin的值之前先在图像上调用CaptureMouse,它也会工作。这只是一笔财富吗?或者某种机制?区别在于:当您单击图像并尝试将鼠标捕获到底层画布时,画布的MouseMove事件被触发。相反,当您单击图像并尝试捕获图像时,图像的MouseMove事件不会触发。看起来这是由CaptureMouse API引起的。这是一个非常有趣的症状,我建议您单独问一个问题,为什么在有问题的代码中会触发MouseMove。
if (!this.panImage.IsMouseCaptured)
{
    return;
}