LoginWindow.xaml
<Window x:Class="TestTool.View.LoginWindow"
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:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:hc="https://handyorg.github.io/handycontrol"
xmlns:helpers="clr-namespace:TestTool.Helpers"
xmlns:local="clr-namespace:TestTool.View"
mc:Ignorable="d"
x:Name="loginWin"
WindowStartupLocation="CenterScreen"
Style="{DynamicResource LoginWindowStyle}"
DataContext="{Binding Source={StaticResource Locator}, Path= Login}" ResizeMode="NoResize" AllowDrop="True" ShowInTaskbar="True">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes/Generic.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="LoginWindowStyle" TargetType="{x:Type Window}">
<Setter Property="AllowsTransparency" Value="True"/>
<Setter Property="WindowStyle" Value="None"/>
<Setter Property="AllowDrop" Value="True"/>
<Setter Property="Height" Value="400"/>
<Setter Property="Width" Value="600"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Border BorderThickness="0" CornerRadius="8" Background="White">
<ContentPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ToggleButtonLogin" BasedOn="{StaticResource ToggleButtonBaseStyle}" TargetType="ToggleButton">
<Setter Property="Background" Value="#396DF2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Border x:Name="Chrome" CornerRadius="2" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<hc:CircleProgressBar x:Name="Loading" Width="20" Height="20" Foreground="{TemplateBinding Foreground}"
IsIndeterminate="{TemplateBinding IsChecked}" Visibility="Collapsed"
ArcThickness="2" Style="{StaticResource ProgressBarSuccessCircle}" Background="Transparent"/>
<hc:SimplePanel>
<ContentPresenter Name="UnCheckedElement" Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
<ContentPresenter Name="CheckedElement" Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Visibility="Collapsed" Content="{Binding Path=(hc:StatusSwitchElement.CheckedElement),RelativeSource={RelativeSource TemplatedParent}}" />
</hc:SimplePanel>
</StackPanel>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Opacity" Value=".9" />
<Setter Property="Cursor" Value="Hand"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Opacity" Value=".6" />
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Visibility" Value="Visible" TargetName="Loading" />
<Setter Property="Visibility" Value="Visible" TargetName="CheckedElement" />
<Setter Property="Background" Value="#A1BCFF"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="hc:StatusSwitchElement.HideUncheckedElement" Value="True" />
<Condition Property="IsChecked" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Visibility" Value="Collapsed" TargetName="UnCheckedElement" />
<Setter Property="Background" Value="#A1BCFF"/>
</MultiTrigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value="0.4" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Window.Resources>
<i:Interaction.Behaviors>
<helpers:WindowBehavior Close="{Binding ToClose}"/>
</i:Interaction.Behaviors>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Border Background="#3A7DF3" CornerRadius="8,8,0,0">
<Grid>
<TextBlock Text="MMI Test Tool" FontWeight="Bold" FontSize="22" Foreground="#99FFFFFF" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<TextBlock Text="{Binding Version}" FontSize="12" Foreground="#99FFFFFF" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="0,0,20,12"/>
</Grid>
</Border>
<StackPanel Grid.Row="1" Orientation="Vertical" HorizontalAlignment="Center" Width="500" Margin="30">
<TextBlock Text="{DynamicResource Login_Tips}" FontSize="18" Foreground="#F53F3F" VerticalAlignment="Center" Margin="10"/>
<ComboBox Margin="10" Height="40" SelectionChanged="ComboBox_SelectionChanged">
<ComboBoxItem Content="aaaaaa"/>
<ComboBoxItem Content="bbbbbb"/>
</ComboBox>
<ToggleButton x:Name="loginBtn" Style="{StaticResource ToggleButtonLogin}"
IsEnabled="False"
IsChecked="{Binding IsLoggingIn,Mode=TwoWay}"
Command="{Binding LoginCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}"
Content="{DynamicResource Login_Login}"
FontSize="18" Width="160" Height="50" Margin="50"/>
</StackPanel>
<Grid VerticalAlignment="Top" HorizontalAlignment="Right">
<Button Content="×" FontSize="26"
Foreground="#6A6A77" Width="46" Height="46" Background="Transparent" BorderThickness="0" Click="Button_Click"/>
</Grid>
</Grid>
</Window>
LoginWindow.xaml.cs
public partial class LoginWindow : Window
{
public LoginWindow()
{
InitializeComponent();
LanguageService.SetLanguageType(AppConfigManager.GetValue("Language"));
InitializeComponent();
this.Register<MainWindow>("MainWindow");
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void ComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
loginBtn.IsEnabled = true;
}
}
LoginViewModel.cs
public class LoginViewModel : ViewModelBase
{
private bool _toClose;
/// <summary>
///
/// </summary>
public bool ToClose
{
get { return _toClose; }
set { Set(() => ToClose, ref _toClose, value); }
}
private bool _isLoggingIn;
public bool IsLoggingIn
{
get { return _isLoggingIn; }
set { Set(() => IsLoggingIn, ref _isLoggingIn, value); }
}
private string _version;
public string Version
{
get { return _version; }
set { Set(() => Version, ref _version, value); }
}
public RelayCommand<object> LoginCommand { get; set; }
public LoginViewModel()
{
IsLoggingIn = false;
LoginCommand = new RelayCommand<object>(LoginAction, true);
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
Version = string.Format("V{0}.{1}.{2:D2}.{3:D3}", version.Major, version.Minor, version.Build, version.Revision);
}
private void LoginAction(object o)
{
IsLoggingIn = true;
Task.Run(async () =>
{
await Task.Delay(500).ContinueWith(p =>
{
System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
//打开新窗口
MainViewModel mainViewModel = new MainViewModel();
WindowManager.Show("MainWindow", mainViewModel);
ToClose = true;
IsLoggingIn = false;
//退出登录窗口
if (o != null)
{
var win = o as System.Windows.Window;
win.Close();
}
}));
});
});
}
}
WindowBehavior.cs
public class WindowBehavior : Behavior<Window>
{
/// <summary>
/// 关闭窗口
/// </summary>
public bool Close
{
get { return (bool)GetValue(CloseProperty); }
set { SetValue(CloseProperty, value); }
}
public static readonly DependencyProperty CloseProperty =
DependencyProperty.Register("Close", typeof(bool), typeof(WindowBehavior), new PropertyMetadata(false, OnCloseChanged));
private static void OnCloseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var window = ((WindowBehavior)d).AssociatedObject;
var newValue = (bool)e.NewValue;
if (newValue)
{
window.Close();
}
}
}
WindowExt.cs
public static class WindowExt
{
public static void Register(this Window win, string key)
{
WindowManager.Regiter(key, win.GetType());
}
public static void Register(this Window win, string key, Type t)
{
WindowManager.Regiter(key, t);
}
public static void Register<T>(this Window win, string key)
{
WindowManager.Regiter<T>(key);
}
}
WindowManager.cs
public static class WindowManager
{
private static Hashtable _RegisterWindow = new Hashtable();
private static readonly object _locker = new object();
public static void Regiter<T>(string key)
{
lock (_locker)
{
if (!_RegisterWindow.ContainsKey(key))
_RegisterWindow.Add(key, typeof(T));
}
}
public static void Regiter(string key, Type t)
{
lock (_locker)
{
if (!_RegisterWindow.ContainsKey(key))
_RegisterWindow.Add(key, t);
}
}
public static void Remove(string key)
{
lock (_locker)
{
if (_RegisterWindow.ContainsKey(key))
_RegisterWindow.Remove(key);
}
}
public static void ShowDialog(string key, object VM)
{
Type wintype = null;
lock (_locker)
{
if (!_RegisterWindow.ContainsKey(key))
{
//throw (new Exception("This key is not registered!"));
return;
}
wintype = (Type)_RegisterWindow[key];
}
if (wintype == null)
return;
var win = (Window)Activator.CreateInstance(wintype);
win.DataContext = VM;
win.ShowDialog();
}
public static void Show(string key, object VM)
{
Type wintype = null;
lock (_locker)
{
if (!_RegisterWindow.ContainsKey(key))
{
//throw (new Exception("This key is not registered!"));
return;
}
wintype = (Type)_RegisterWindow[key];
}
if (wintype == null)
return;
var win = (Window)Activator.CreateInstance(wintype);
win.DataContext = VM;
win.Show();
}
}