WPF 登录窗口demo示例

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();
        }
    }

  • 21
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: WPF动画效果 demo是一个展示WPF动画效果的演示程序。WPF是Windows Presentation Foundation的缩写,是微软开发的一种基于向量图形的桌面应用程序开发平台。WPF具有强大的图形渲染能力和动画效果的支持,可以帮助开发人员快速开发出富有交互性的应用程序。 WPF动画效果demo展示了WPF桌面应用程序开发中最常用的动画效果:平移、旋转、缩放和淡入淡出。这些动画效果可以通过WPF内置的动画类或自定义动画类实现。在demo中,每种动画效果都配有一个按钮,点击按钮即可观看相应的动画效果。 平移动画效果是通过设置元素的位置属性来实现的。demo中演示了一个按钮在平移时如何改变位置属性,并使用WPF提供的动画类来实现流畅的运动效果。 旋转动画效果是通过设置元素的旋转角度属性来实现的。demo中演示了如何通过使用动画类来控制元素的旋转角度,并使元素在旋转时保持流畅性。 缩放动画效果是通过设置元素的缩放属性来实现的。demo中演示了如何通过使用动画类来控制元素的缩放属性,实现元素在缩放时的平滑过渡效果。 淡入淡出动画效果是通过设置元素的不透明度属性来实现的。demo中演示了如何使用动画类来控制元素的不透明度属性,实现元素在淡入淡出时的平滑过渡效果。 总体来说,WPF动画效果demo是一个非常有用的资源,可以帮助开发人员更好地理解和掌握WPF动画效果的实现方法,并为开发交互性强的桌面应用程序提供有力支持。 ### 回答2: WPF动画效果demo是一种展示WPF技术所提供的动画效果的实例应用。WPF动画效果demo的目的是为了展示WPF技术中所包含的丰富的动画效果,以让开发者在实际应用中可以使用这些动画效果来制作更加生动、优美的应用界面。 WPF动画效果demo可以运用各种形式的动画效果,如平移、旋转、缩放、透明度等,通过动画效果的实时反馈来吸引用户的注意力,为用户提供更加丰富的视觉体验。在动画效果的展示中,可根据实际需求,自由控制动画的速率、节奏、方向等参数,以达到不同的效果表现。 此外,WPF动画效果demo还可以通过应用多种动画效果的组合来实现更为复杂的视觉效果,增强应用的互动性和吸引力。 总之,WPF动画效果demoWPF技术中的一个重要应用示例,通过动态展示各种动画效果,让开发者们可以利用WPF技术来实现更为生动、灵活的应用界面,提高应用的用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值