WPF软件开发
1、WPF介绍
官网:https://learn.microsoft.com/zh-cn/dotnet/desktop/wpf/app-development/?view=netframeworkdesktop-4.8
1.1、WPF定义
WPF是Windows Presentation Foundation的缩写,是一种基于XAML(Extensible Application Markup Language)的UI框架。它是由微软公司开发的,旨在提供一种更灵活、更强大的方式来开发Windows应用程序的用户界面。相比于传统的WinForms,WPF具有以下优点:
XAML语言更加简洁易懂,易于维护和修改。
支持矢量图形和动画效果,可以创建更加逼真的视觉效果。
支持数据绑定和依赖属性,可以实现更加灵活的数据交互。
支持多线程编程,可以提高应用程序的性能和响应速度。
提供了丰富的控件和布局选项,可以轻松实现复杂的界面设计。
1.2、WPF和winform的区别
WPF和WinForm是两种不同的Windows应用程序开发框架,它们在界面设计、性能、可扩展性等方面存在一些区别。
首先,WPF是一种基于XAML的UI框架,它使用XML格式来定义用户界面元素和布局。相比之下,WinForm使用C#代码来创建用户界面,需要手动编写HTML标记和CSS样式。这使得WPF更加灵活和易于维护,因为它允许开发人员使用一种更加直观和易于理解的方式来描述用户界面。
其次,WPF支持矢量图形和动画效果,可以创建更加逼真的视觉效果。而WinForm只能使用位图和GIF等静态图像来显示内容,无法实现复杂的动画效果。此外,WPF还提供了更多的控件和布局选项,可以轻松实现复杂的界面设计。
另外,WPF支持数据绑定和依赖属性,可以实现更加灵活的数据交互。例如,开发人员可以使用数据绑定功能将一个控件的值与另一个控件的属性进行关联,从而实现数据的自动更新。而在WinForm中,需要手动编写代码来实现这种数据交互。
最后,WPF支持多线程编程,可以提高应用程序的性能和响应速度。它允许开发人员在不同的线程中执行耗时的操作,如文件读取或网络请求等,以避免阻塞主线程导致应用程序无响应。而在WinForm中,所有的操作都必须在主线程中执行。
2、使用JetBrains Rider软件创建WPF项目
我习惯用JetBrains公司的产品,之前写java用idea习惯了,索性下载一个rider来开发C#。
Rider是一款强大的代码编辑器,可以帮助开发人员快速创建WPF项目并编写代码。下面是如何使用Rider创建WPF项目的步骤:
2.1、创建WPF项目
1.打开JetBrains Rider软件,选择“Create New Project”创建一个新项目。
在“Project Templates”中选择“WPF App (.NET Framework)”,然后点击“Next”。
2.输入项目名称和位置,然后点击“Finish”完成项目创建。
在解决方案资源管理器中双击“MainWindow.xaml”文件打开主窗口的设计界面。
2.2、编写xaml代码,创建按钮
打开MainWindow.xaml文件,编写代码如下,不知道标签名字可以去官网查看。
<Window x:Class="WPFTest.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:WPFTest"
mc:Ignorable="d"
Title="File Conversion" Height="500" Width="800" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen">
<StackPanel Background="#F2F4FB">
<WrapPanel Background="Beige" Margin="0,10,0,10">
<Button x:Name="fileConverBtn" Click="FileConverBtn_OnClick" Background="#ADD8E6" Content="文档转换" Style="{StaticResource MyButtonStyle1}"></Button>
<Button x:Name="fileTransBtn" Click="FileTransBtn_OnClick" Background="Azure" Content="文档翻译" Style="{StaticResource MyButtonStyle1}"></Button>
<Button x:Name="LoginBtn" Click="LoginPopup_OnClick" Background="Azure" Content="登录认证" Style="{StaticResource MyButtonStyle1}"></Button>
</WrapPanel>
</StackPanel>
</Window>
效果如图
2.3、美化按钮
这些样式都可以在官网搜到。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="MyButtonStyle1" TargetType="Button">
<Setter Property="FontFamily" Value="宋体" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Width" Value="100"></Setter>
<Setter Property="Height" Value="30"></Setter>
<Setter Property="Margin" Value="5,5,10,5"></Setter>
<Setter Property="FontSize" Value="16"></Setter>
<Setter Property="Cursor" Value="Arrow"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Cursor" Value="Hand"/>
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
效果如下:
3、按钮控制显示\隐藏内容
3.1、创建两个面板
我们要达到的效果是:点击fileConverBtn按钮显示PdfConverPanel面板,点击fileTransBtn按钮显示PdfConverPanel面板
<Window x:Class="WPFTest.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:WPFTest"
mc:Ignorable="d"
Title="File Conversion" Height="500" Width="800" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="btnStyle.xaml"/>
<ResourceDictionary Source="FileSpace.xaml"/>
<ResourceDictionary Source="ComboBoxMouse1.xaml"/>
<ResourceDictionary Source="BtnMouse1.xaml"/>
<ResourceDictionary Source="StackPanel1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<StackPanel Background="#F2F4FB">
<WrapPanel Background="Beige" Margin="0,10,0,10">
<Button x:Name="fileConverBtn" Click="FileConverBtn_OnClick" Background="#ADD8E6" Content="文档转换" Style="{StaticResource MyButtonStyle1}"></Button>
<Button x:Name="fileTransBtn" Click="FileTransBtn_OnClick" Background="Azure" Content="文档翻译" Style="{StaticResource MyButtonStyle1}"></Button>
<Button x:Name="LoginBtn" Click="LoginPopup_OnClick" Background="Azure" Content="登录认证" Style="{StaticResource MyButtonStyle1}"></Button>
</WrapPanel>
<WrapPanel Background="Aquamarine" x:Name="PdfConverPanel" Margin="10">
<ComboBox x:Name="pdfConvertComboBox" Style="{StaticResource ComboxMouse1}">
<ComboBoxItem Content="pdf转docx" IsSelected="True"/>
<ComboBoxItem Content="pdf转doc" />
<ComboBoxItem Content="pdf转html" />
</ComboBox>
</WrapPanel>
<StackPanel x:Name="TransPanel" Visibility="Collapsed">
</StackPanel>
</StackPanel>
</Window>
3.2、给按钮添加事件
/// <summary>
/// 文档转换按钮 ,点击之后,面板切换到文档转换面板
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FileConverBtn_OnClick(object sender, RoutedEventArgs e)
{
// 显示PdfConverPanel
PdfConverPanel.Visibility = Visibility.Visible;
// 隐藏TransPanel
TransPanel.Visibility = Visibility.Collapsed;
//设置点击时按钮的颜色
fileConverBtn.Background = new SolidColorBrush(Color.FromRgb(173, 216, 230));
fileTransBtn.Background = new SolidColorBrush(Color.FromRgb(0xE9, 0xF4, 0xFF));
}
/// <summary>#ADD8E6
/// 文档翻译按钮 ,点击之后,面板切换到文档翻译面板
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FileTransBtn_OnClick(object sender, RoutedEventArgs e)
{
// 隐藏PdfConverPanel
PdfConverPanel.Visibility = Visibility.Collapsed;
// 显示TransPanel
TransPanel.Visibility = Visibility.Visible;
//设置点击时按钮的颜色
fileConverBtn.Background = new SolidColorBrush(Color.FromRgb(0xE9, 0xF4, 0xFF));
fileTransBtn.Background = new SolidColorBrush(Color.FromRgb(173, 216, 230));
}
4、总结
感谢您的阅读,希望本篇博客对您有所帮助。WPF是一种基于XAML的UI框架,它提供了更加灵活、更强大的方式来开发Windows应用程序的用户界面。相比于传统的WinForms,WPF具有更高的灵活性、更好的用户体验、更丰富的控件和布局选项以及更好的性能表现。
在创建WPF项目时,可以使用JetBrains Rider软件来简化流程。通过可视化设计器,可以轻松地创建WPF项目并添加窗口、按钮等控件。同时,也可以使用代码编辑器编写XAML代码来实现更加复杂的界面设计。
在使用WPF的过程中,需要注意一些细节问题,如如何将窗口居中显示、如何禁止最大化等。这些小技巧可以帮助我们更好地利用WPF的特性,实现更加美观、高效的用户界面。
最后,本篇博客介绍了如何添加按钮以及如何美化按钮。按钮是WPF应用程序中最常用的控件之一,它可以用于触发各种操作和事件。通过对按钮进行美化和添加事件处理程序,可以实现更加友好、易于使用的用户体验。
如有帮助,还请动动手指点个赞!