美文网首页
Image.PreviewMouseLeftUp未触发(WPF)

Image.PreviewMouseLeftUp未触发(WPF)

作者: JetLu | 来源:发表于2017-08-13 12:05 被阅读12次

    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.

    相关文章

      网友评论

          本文标题:Image.PreviewMouseLeftUp未触发(WPF)

          本文链接:https://www.haomeiwen.com/subject/euosrxtx.html