寫launcher
時(shí)遇到的一個(gè)問題。
以下結(jié)構(gòu)的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>
對(duì)應(yīng)的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");
}
}
}
那么永遠(yuǎn)不會(huì)彈出Message box
抛腕。
查找了一些資料和自己的測(cè)試芋绸。
問題出在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.