Image.PreviewMouseLeftUp未触发(WPF)

launcher时遇到的一个问题。

以下结构的xaml:

<Window x:Class="test.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:test"
        mc:Ignorable="d"
        Background="Transparent"
        WindowStyle="None"
        AllowsTransparency="True"
        PreviewMouseLeftButtonDown="Window_PreviewMouseLeftButtonDown"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Border Background="#80000000" CornerRadius="4">
            <Grid>
                <Image Source="https://cdn.lufei.so/image/avatar.png"
                    PreviewMouseLeftButtonUp="Image_PreviewMouseLeftButtonUp" 
                ></Image>
            </Grid>
            
        </Border>
    </Grid>
</Window>

对应的cs:

using System.Windows.Input;

namespace test {
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
        }

        private void Window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
            DragMove();
            Trace.WriteLine(e.OriginalSource);
        }

        private void Image_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
            MessageBox.Show("image mouse up");
        }
    }
}

那么永远不会弹出Message box

查找了一些资料和自己的测试。

问题出在DragMove

下面这段截在 stackoverflow

DragMove is a synchronous call; it blocks until the user is done moving the window. This means once DragMove returns, the left button is up. Add your code immediately after your DragMove() call and you should be fine.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容