海南都市網_海南最權威的都市企業資訊門戶網站之一
您當前的位置 : 海南都市網  >  微商
C# WPF計算器界面(Calculator Design With Animations)
2020-03-30 19:30:15 來源:互聯網 閱讀:-

時間如流水,只能流去不流回!

點贊再看,養成習慣,這是您給我創作的動力!

本文 Dotnet9 https://dotnet9.com 已收錄,站長樂于分享dotnet相關技術,比如Winform、WPF、ASP.NET Core、Xamarin.Forms等,亦有C++桌面相關的Qt Quick和Qt Widgets等,只分享自己熟悉的、自己會的。

閱讀導航:

  • 一、先看效果
  • 二、本文背景
  • 三、代碼實現
  • 四、文章參考
  • 五、代碼下載

一、先看效果

二、本文背景

YouTube Design com WPF 大神處習得,簡單的計算器布局界面 + 簡單動畫,使用的開源 C# WPF控件庫 MaterialDesignInXAML ,本站曾有介紹:開源C# WPF控件庫《MaterialDesignInXAML》。

三、代碼實現

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,整體布局,看上圖加上下面的界面代碼,本文基本就是布局 + 簡單動畫,全在這個界面,直接看代碼吧,不細說了:

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

后臺簡單的事件

private void Window_MouseDown(object sender, MouseButtonEventArgs e){    DragMove();}

四、文章參考

可直接打開大神視頻學習,他的YouTube上還有很多代碼視頻哦,參考:
參考視頻: Design com WPF: https://www.youtube.com/watch?v=G76O79eLcKM

五、代碼下載

文章中代碼已經全部貼出,添加Nuget包,復制文中代碼就可以運行了。

除非注明,文章均由 Dotnet9 整理發布,歡迎轉載。

轉載請注明本文地址:https://dotnet9.com/6777.html


推薦閱讀:2019年性價比最高的筆記本

頻道推薦
  • 《鶴唳華亭》更新時間提前至17點,預計下月初迎來大結局!
    《鶴唳華亭》更新時間提前至17點,預計下月初

    羅晉、李一桐等主演的古裝大劇《鶴唳華亭》正在優酷熱播,劇情已經接近尾聲。目前,該劇豆瓣7.4,口碑不錯。今天,優酷官宣《鶴唳華亭》更新時間變更:自今晚起,追劇時...

    2020-01-02
  • 開年第一瓜!連他也劈腿了?
    開年第一瓜!連他也劈腿了?

    今天一早,賀軍翔這個許久不見的名字登上熱搜,頓時引發了不少網友的圍觀。這難道就是新年的第一瓜?據臺媒報道,已婚人夫賀軍翔對妻女極為保護,很少分享自己的家庭生活,...

    2020-01-02
  • 《大明風華》中,朱棣為什么要五次親征漠北?
    《大明風華》中,朱棣為什么要五次親征漠北?

    湯唯、朱亞文主演的電視劇《大明風華》持續熱播,相較此前上映的兩部古裝戲,熱度有下降趨勢。《大明風華》卻憑借著精良制作,演員的出色發揮,收視率和播放成績開始逐漸提...

    2020-01-02
  • 跨年夜臺下更精彩:肖戰狂奔,李現成迷弟,陳偉霆忙加微信
    跨年夜臺下更精彩:肖戰狂奔,李現成迷弟,陳偉

    昨晚的跨年演唱會,可以說是熱鬧非凡。五大衛視都同期舉辦跨年演唱會,數不勝數的明星可以說讓觀眾們也是看的眼花繚亂。明星們在臺上賣力的演出,帶給我們觀眾很多精彩的表...

    2020-01-02
  • 《奇葩說》五強已現,黃詹儲席邱各有短板,bbking爭奪將撲在哪?
    《奇葩說》五強已現,黃詹儲席邱各有短板,bb

    節目播到這里,其實,五強勢頭已然顯現。如無意外,bbking一定會在他們之中產生。01,黃執中:撲在面兒說目前場上留存的最強辯手是黃執中,你可以不信,但《奇葩說...

    2020-01-02
秒速赛车规律个人心得 有玩吉林快3的人吗 双节彩票停售 上海时时彩开奖号 本期深圳风采几点开奖 广东时时彩11选五计划软件下载 dg官网旗舰店 澳洲幸运5是国家开奖吗加 神童码报彩图今日 pt电子游pt电子游艺|Welcome 十一运夺金开奖结果360 ds真人平台 pc蛋蛋预测工具 青海11选5今天开奖结果 天津快乐十分最快开奖 ag视讯哪个是官网 浙江体彩6+1