C# WPF计算器界面(Calculator Design With Animations)

时间如流水,只能流去不流回!

点赞再看,养成习惯,这是您给我创作的动力!

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

欢迎扫描下方二维码关注 Dotnet9 的微信公众号,本站会及时推送最新技术文章(微信公众号“dotnet9_com”):

如有收获,请大力转发,给Dotnet9赞助和支持,谢谢大家对dotnet技术的关注和支持 。

原文地址:https://www.cnblogs.com/Dotnet9-com/p/12116983.html

时间: 2024-10-16 07:45:33

C# WPF计算器界面(Calculator Design With Animations)的相关文章

JAVA开发简易计算器界面-SWT

大家好,我是成都[LD],博客四年前就申请了,一直没打理,最近正好有时间,遂萌生了写技术博客的念头.我不得不感慨现在新技术更新很快,一不小心,就感觉自身就Out了.记得一年前,当时我也是在51CTO上了解到NoSQL和Hadoop这样的信息,当时就简单觉得很新奇,没想到一年之后发展如此迅速~~当然我这样说,并不是叫大家去追寻新技术,最根本的还是基础打牢靠,休息的时候多去了解下最新的IT动态.学习下前辈高手的一些技能~~打铁还需自身硬嘛! 我写博客的目的:一来是为了促进自身的进步,二来是为了希望与

手把手教你做安豆计算器(二)-计算器界面布局

第3节 计算器界面布局 现在起,我们就开始正式开发"计算器"应用.这一节,我们将完成计算器的界面布局,让它初具计算器的模样. 计算器界面是通过布局文件定义的.它位于项目的res\layout\activity_main.xml文件中. 这个布局文件通过java源代码MainActivity.java中的setContentView()函数,设置到界面上. @Override protected void onCreate(Bundle savedInstanceState) { sup

C# WPF聊天界面(3/3)

原文:C# WPF聊天界面(3/3) 微信公众号:Dotnet9,网站:Dotnet9,问题或建议:请网站留言, 如果对您有所帮助:欢迎赞赏. C# WPF聊天界面(3/3) 阅读导航 本文背景 代码实现 本文参考 1.本文背景 系列文章最后一篇,一个完整的聊天界面.当然只看效果,具体的项目需要将左侧好友列表.中间会话列表.右侧联系人简况做成MVVM绑定的形式,做成模板才是一个完整的项目,本系列只是对界面的一个设计参考. 前面两篇文章: C# WPF联系人列表(1/3) C# WPF简况(2/3

计算器界面——GridLayout

1 <?xml version="1.0" encoding="utf-8"?> 2 <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 and

Android计算器界面

1 <?xml version="1.0" encoding="utf-8"?> 2 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 an

从零开始学android&lt;使用嵌套布局实现计算器界面.十七.&gt;

所谓的嵌套布局就是在一个文件中嵌套多个布局文件 <span style="font-size:18px;"> <LinearLayout android:layout_width="match_parent" android:layout_height="fill_parent" android:orientation="vertical" > <FrameLayout android:layou

【源码分享】WPF漂亮界面框架实现原理分析及源码分享

1 源码下载 2 OSGi.NET插件应用架构概述 3 漂亮界面框架原理概述 4 漂亮界面框架实现  4.1 主程序  4.2 主程序与插件的通讯   4.2.1 主程序获取插件注册的服务   4.2.2 插件获取主程序注册的服务   4.2.3 服务接口  4.3 权限管理插件的登录窗体  4.4 界面框架插件   4.4.1 导航服务   4.4.2 界面框架扩展实现  4.5 插件   4.5.1 插件引用了第三方程序集   4.5.2 一个程序集如何让所有插件都直接使用   4.5.3

WPF实现界面动态布局

以前总觉得动态布局是个很麻烦的问题,是个很需要功力的问题.但是貌似在.NET中,在WPF中却不是那么的麻烦.下面介绍我现在实现的一个动态布局的实例. 因为有需求,所以困难得克服!而我们的需求表名,不同的用户需要的界面元素是不一样的,我们总不能每次都去修改代码吧!所以,需要完成动态布局. 这里主要完成这样一个功能: 1.动态画线 2.动态new控件 3.线和控件都是可拖拽并随意放置位置的 4.线和控件是可删除的 5.控件是可绑定属性和事件的 要完成这样的功能,我们首先得定义三个鼠标事件,即:左键d

Android计算器界面布局

Android计算器界面图: 所定义的XML布局文件,主要用到的是TableLayout: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_heigh