MainWindow.xaml
<Window x:Class="TestTool.View.MainWindow"
......
AllowsTransparency="True"
WindowStartupLocation="CenterScreen"
WindowStyle="None"
AllowDrop="True" MouseLeftButtonDown="Window_MouseLeftButtonDown">
......
<Grid Background="Blue" Grid.ColumnSpan="2">
<TextBlock Text="Test Tool" FontWeight="Bold" FontSize="22" Foreground="#FFFFFF" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="30,0,0,0"/>
<UniformGrid Columns="3" Rows="1" HorizontalAlignment="Right" VerticalAlignment="Top" Width="100" Height="36" Margin="0,6,10,0">
<Button x:Name="btn_minimize" Content="---" FontSize="24" Foreground="White" Width="32" Height="36" Background="Transparent" BorderThickness="0" Click="btn_minimize_Click"/>
<Button x:Name="btn_maximize" Content="□" FontSize="24" Foreground="White" Width="32" Height="36" Background="Transparent" BorderThickness="0" Click="btn_maximize_Click"/>
<Button x:Name="btn_close" Content="×" FontSize="26" Foreground="White" Width="32" Height="36" Background="Transparent" BorderThickness="0" Click="btn_close_Click"/>
</UniformGrid>
</Grid>
MainWindow.xaml.cs
private void Window_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
//获取鼠标相对窗口的位置
Point position = e.GetPosition(this);
//鼠标放在窗口就移动
if (e.LeftButton == MouseButtonState.Pressed)
{
if (position.X >= 0 && position.X < this.ActualWidth && position.Y >= 0 && position.Y <= this.ActualWidth)
{
this.DragMove();
}
}
}
private void btn_minimize_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
private void btn_maximize_Click(object sender, RoutedEventArgs e)
{
if (WindowState == WindowState.Maximized)
{
this.WindowState = WindowState.Normal;
}
else
{
this.WindowStyle = WindowStyle.None;
this.ResizeMode = ResizeMode.NoResize;
this.WindowState = WindowState.Maximized;
}
}
private void btn_close_Click(object sender, RoutedEventArgs e)
{
ViewModel.ViewModelLocator.CleanupMainViewModel();
this.Close();
}
ViewModelLocator.cs
......
public static void CleanupMainViewModel()
{
Cleanup<MainViewModel>();
}
private static void Cleanup<T>() where T : ViewModelBase
{
T viewModel = ServiceLocator.Current.GetInstance<T>();
if (viewModel != null)
viewModel.Cleanup();
SimpleIoc.Default.Unregister<T>();
}