閱讀導航:app
- 1、先看效果
- 2、本文背景
- 3、代碼實現
- 4、文章參考
- 5、代碼下載
1、先看效果
2、本文背景
YouTube Design com WPF 大神處習得,簡單的計算器佈局界面 + 簡單動畫,使用的開源 C# WPF控件庫 MaterialDesignInXAML ,本站曾有介紹:開源C# WPF控件庫《MaterialDesignInXAML》。佈局
3、代碼實現
3.1 添加Nuget庫
站長使用.Net Core 3.1建立的WPF工程,建立「Calculator」解決方案後,須要添加兩個Nuget庫:MaterialDesignThemes和MaterialDesignColors,上圖的效果是使用該控件庫實現的,很是強大。學習
3.2 工程結構
不須要截圖,只修改了兩個文件,App.xaml添加MD控件樣式,MainWindow主窗口實現效果。動畫
3.3 App.xaml引入MD控件樣式
<Application x:Class="Calculator.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Calculator" StartupUri="MainWindow.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" /> <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" /> </ResourceDictionary.MergedDictionaries> <!--PRIMARY--> <SolidColorBrush x:Key="PrimaryHueLightBrush" Color="#349fda"/> <SolidColorBrush x:Key="PrimaryHueLightForegroundBrush" Color="#FF777777"/> <SolidColorBrush x:Key="PrimaryHueMidBrush" Color="#FF222222"/> <SolidColorBrush x:Key="PrimaryHueMidForegroundBrush" Color="#DDDDDD"/> <SolidColorBrush x:Key="PrimaryHueDarkBrush" Color="#FF000000"/> <SolidColorBrush x:Key="PrimaryHueDarkForegroundBrush" Color="#FFFFFF"/> <!--ACCENT--> <SolidColorBrush x:Key="SecondaryAccentBrush" Color="#FFD14C25"/> <SolidColorBrush x:Key="SecondaryAccentForegroundBrush" Color="#FFFFFF"/> </ResourceDictionary> </Application.Resources> </Application>
3.4 主窗體
MainWindow.xaml,總體佈局,看上圖加上下面的界面代碼,本文基本就是佈局 + 簡單動畫,全在這個界面,直接看代碼吧,不細說了:ui
<Window x:Class="Calculator.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:Calculator" mc:Ignorable="d" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" Height="550" Width="300" AllowsTransparency="True" WindowStyle="None" ResizeMode="NoResize" MouseDown="Window_MouseDown" WindowStartupLocation="CenterScreen"> <Window.Resources> <Storyboard x:Key="PowerOff"> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="grid"> <DiscreteObjectKeyFrame KeyTime="0:0:0.2" Value="{x:Static Visibility.Visible}"/> <DiscreteObjectKeyFrame KeyTime="0:0:1.5" Value="{x:Static Visibility.Visible}"/> </ObjectAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid"> <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0"/> <EasingDoubleKeyFrame KeyTime="0:0:1.5" Value="1"/> </DoubleAnimationUsingKeyFrames> </Storyboard> <Storyboard x:Key="PowerOn"> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="grid"> <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/> <DiscreteObjectKeyFrame KeyTime="0:0:1.3" Value="{x:Static Visibility.Visible}"/> <DiscreteObjectKeyFrame KeyTime="0:0:1.5" Value="{x:Static Visibility.Collapsed}"/> </ObjectAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid"> <EasingDoubleKeyFrame KeyTime="0" Value="1"/> <EasingDoubleKeyFrame KeyTime="0:0:1.3" Value="0"/> <EasingDoubleKeyFrame KeyTime="0:0:1.5" Value="0"/> </DoubleAnimationUsingKeyFrames> </Storyboard> </Window.Resources> <Window.Triggers> <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="ButtonPowerOff"> <BeginStoryboard Storyboard="{StaticResource PowerOff}"/> </EventTrigger> <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="ButtonPowerOn"> <BeginStoryboard Storyboard="{StaticResource PowerOn}"/> </EventTrigger> </Window.Triggers> <Border Background="#E5000000" CornerRadius="10"> <Grid> <StackPanel> <Grid Height="210"> <StackPanel> <Button HorizontalAlignment="Left" Margin="10" Style="{StaticResource MaterialDesignFlatButton}" Foreground="{StaticResource PrimaryHueMidForegroundBrush}"> <materialDesign:PackIcon Kind="Menu" Foreground="{StaticResource PrimaryHueLightForegroundBrush}"/> </Button> <TextBlock FontSize="15" FontFamily="Oswald" Text="30 + 20 = 50" TextAlignment="Right" Foreground="{StaticResource PrimaryHueLightForegroundBrush}" Margin="20 0 20 10"/> </StackPanel> <StackPanel VerticalAlignment="Bottom"> <TextBlock FontSize="20" FontFamily="Oswald" Text="30 + 47 + 32 -" TextAlignment="Right" Foreground="{StaticResource PrimaryHueLightForegroundBrush}" Margin="20 0"/> <TextBlock FontSize="50" FontFamily="Oswald" Text="13" TextAlignment="Right" Foreground="#FF5885A4" Margin="20 0"> <TextBlock.Effect> <DropShadowEffect BlurRadius="10" ShadowDepth="1" Color="#FF5885A4"/> </TextBlock.Effect> </TextBlock> </StackPanel> </Grid> <Rectangle Height="1" Fill="Gray" Margin="10 0"/> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="1*"/> <RowDefinition Height="1*"/> <RowDefinition Height="1*"/> <RowDefinition Height="1*"/> <RowDefinition Height="1*"/> </Grid.RowDefinitions> <Button Grid.Column="0" Grid.Row="0" Margin="5" Style="{StaticResource MaterialDesignFloatingActionButton}" BorderThickness="1" BorderBrush="{StaticResource PrimaryHueMidBrush}"> <materialDesign:PackIcon Kind="LetterC" Width="30" Height="30"/> </Button> <Button Grid.Column="1" Grid.Row="0" Margin="5" Style="{StaticResource MaterialDesignFloatingActionButton}" BorderThickness="1" BorderBrush="{StaticResource PrimaryHueMidBrush}"> <materialDesign:PackIcon Kind="PlusMinus" Width="30" Height="30"/> </Button> <Button Grid.Column="2" Grid.Row="0" Margin="5" Style="{StaticResource MaterialDesignFloatingActionButton}" BorderThickness="1" BorderBrush="{StaticResource PrimaryHueMidBrush}"> <materialDesign:PackIcon Kind="Percent" Width="30" Height="30"/> </Button> <Button Grid.Column="3" Grid.Row="0" Margin="5" Style="{StaticResource MaterialDesignFloatingActionButton}" BorderThickness="1" BorderBrush="{StaticResource PrimaryHueMidBrush}"> <materialDesign:PackIcon Kind="Division" Width="30" Height="30"/> </Button> <Button Grid.Column="0" Grid.Row="1" Margin="5" Style="{StaticResource MaterialDesignFloatingActionDarkButton}" BorderThickness="1" BorderBrush="{StaticResource PrimaryHueMidBrush}"> <materialDesign:PackIcon Kind="Number7" Width="30" Height="30"/> </Button> <Button Grid.Column="1" Grid.Row="1" Margin="5" Style="{StaticResource MaterialDesignFloatingActionDarkButton}" BorderThickness="1" BorderBrush="{StaticResource PrimaryHueMidBrush}"> <materialDesign:PackIcon Kind="Number8" Width="30" Height="30"/> </Button> <Button Grid.Column="2" Grid.Row="1" Margin="5" Style="{StaticResource MaterialDesignFloatingActionDarkButton}" BorderThickness="1" BorderBrush="{StaticResource PrimaryHueMidBrush}"> <materialDesign:PackIcon Kind="Number9" Width="30" Height="30"/> </Button> <Button Grid.Column="3" Grid.Row="1" Margin="5" Style="{StaticResource MaterialDesignFloatingActionDarkButton}" BorderThickness="1" BorderBrush="{StaticResource PrimaryHueMidBrush}"> <materialDesign:PackIcon Kind="Multiplication" Width="30" Height="30"/> </Button> <Button Grid.Column="0" Grid.Row="2" Margin="5" Style="{StaticResource MaterialDesignFloatingActionDarkButton}" BorderThickness="1" BorderBrush="{StaticResource PrimaryHueMidBrush}"> <materialDesign:PackIcon Kind="Number4" Width="30" Height="30"/> </Button> <Button Grid.Column="1" Grid.Row="2" Margin="5" Style="{StaticResource MaterialDesignFloatingActionDarkButton}" BorderThickness="1" BorderBrush="{StaticResource PrimaryHueMidBrush}"> <materialDesign:PackIcon Kind="Number5" Width="30" Height="30"/> </Button> <Button Grid.Column="2" Grid.Row="2" Margin="5" Style="{StaticResource MaterialDesignFloatingActionDarkButton}" BorderThickness="1" BorderBrush="{StaticResource PrimaryHueMidBrush}"> <materialDesign:PackIcon Kind="Number6" Width="30" Height="30"/> </Button> <Button Grid.Column="3" Grid.Row="2" Margin="5" Style="{StaticResource MaterialDesignFloatingActionDarkButton}" BorderThickness="1" BorderBrush="{StaticResource PrimaryHueMidBrush}"> <materialDesign:PackIcon Kind="Minus" Width="30" Height="30"/> </Button> <Button Grid.Column="0" Grid.Row="3" Margin="5" Style="{StaticResource MaterialDesignFloatingActionDarkButton}" BorderThickness="1" BorderBrush="{StaticResource PrimaryHueMidBrush}"> <materialDesign:PackIcon Kind="Number1" Width="30" Height="30"/> </Button> <Button Grid.Column="1" Grid.Row="3" Margin="5" Style="{StaticResource MaterialDesignFloatingActionDarkButton}" BorderThickness="1" BorderBrush="{StaticResource PrimaryHueMidBrush}"> <materialDesign:PackIcon Kind="Number2" Width="30" Height="30"/> </Button> <Button Grid.Column="2" Grid.Row="3" Margin="5" Style="{StaticResource MaterialDesignFloatingActionDarkButton}" BorderThickness="1" BorderBrush="{StaticResource PrimaryHueMidBrush}"> <materialDesign:PackIcon Kind="Number3" Width="30" Height="30"/> </Button> <Button Grid.Column="3" Grid.Row="3" Margin="5" Style="{StaticResource MaterialDesignFloatingActionDarkButton}" BorderThickness="1" BorderBrush="{StaticResource PrimaryHueMidBrush}"> <materialDesign:PackIcon Kind="Plus" Width="30" Height="30"/> </Button> <Button x:Name="ButtonPowerOff" Grid.Column="0" Grid.Row="4" Margin="5" Style="{StaticResource MaterialDesignFloatingActionDarkButton}" BorderThickness="1" BorderBrush="{StaticResource PrimaryHueMidBrush}"> <materialDesign:PackIcon Kind="Power" Width="30" Height="30"/> </Button> <Button Grid.Column="1" Grid.Row="4" Margin="5" Style="{StaticResource MaterialDesignFloatingActionDarkButton}" BorderThickness="1" BorderBrush="{StaticResource PrimaryHueMidBrush}"> <materialDesign:PackIcon Kind="Number0" Width="30" Height="30"/> </Button> <Button Grid.Column="2" Grid.Row="4" Margin="5" Content="." Style="{StaticResource MaterialDesignFloatingActionDarkButton}" BorderThickness="1" BorderBrush="{StaticResource PrimaryHueMidBrush}"> </Button> <Button Grid.Column="3" Grid.Row="4" Margin="5" Style="{StaticResource MaterialDesignFloatingActionAccentButton}" BorderThickness="1" BorderBrush="{StaticResource PrimaryHueMidBrush}"> <materialDesign:PackIcon Kind="Equal" Width="30" Height="30"/> </Button> </Grid> </StackPanel> <Border x:Name="grid" CornerRadius="10" Background="Black" Visibility="Collapsed" Opacity="0"> <Button x:Name="ButtonPowerOn" Grid.Column="0" Grid.Row="1" Margin="5" Width="150" Height="150" Style="{StaticResource MaterialDesignFloatingActionDarkButton}" BorderThickness="1" BorderBrush="{StaticResource PrimaryHueMidBrush}"> <materialDesign:PackIcon Kind="Power" Width="80" Height="80"/> </Button> </Border> </Grid> </Border> </Window>
後臺簡單的事件spa
private void Window_MouseDown(object sender, MouseButtonEventArgs e) { DragMove(); }
4、文章參考
可直接打開大神視頻學習,他的YouTube上還有不少代碼視頻哦,參考:
參考視頻: Design com WPF: https://www.youtube.com/watch?v=G76O79eLcKM.net
5、代碼下載
文章中代碼已經所有貼出,添加Nuget包,複製文中代碼就能夠運行了。
除非註明,文章均由 Dotnet9 整理髮布,歡迎轉載。
轉載請註明本文地址:https://dotnet9.com/6777.html
歡迎掃描下方二維碼關注 Dotnet9 的微信公衆號,本站會及時推送最新技術文章(微信公衆號「dotnet9_com」):
若有收穫,請大力轉發,給Dotnet9贊助和支持,謝謝你們對dotnet技術的關注和支持 。