寫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.