工具代码记录
版权声明:本文为博主初学经验,未经博主允许不得转载。
一、前言
记录在学习与制作WPF过程中遇到的解决方案。
分页控件的制作,邮件发送,日志代码,excel导入导出等代码的实现过程;
二、配置
系统环境:
win10
开发工具:
Visual Studio 2017
开发语言:
C#.WPF (MVVM框架)
三、功能
1. 分页控件的制作
1.1 前端xaml代码
<UserControl x:Class="SCB.RPS.Client.Controls.UcPager" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:SCB.RPS.Client.Controls" mc:Ignorable="d" > <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition Width="20" /> <ColumnDefinition /> <ColumnDefinition Width="20" /> <ColumnDefinition /> <ColumnDefinition Width="20" /> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <StackPanel Grid.Column="0" Orientation="Horizontal"> <Label Content="每页显示"/> <ComboBox Text="{Binding PagerSize}" Foreground="Orange" VerticalContentAlignment="Center"> <ComboBoxItem Content="10"/> <ComboBoxItem Content="20"/> <ComboBoxItem Content="50"/> </ComboBox> <Label Content="项"/> </StackPanel> <StackPanel Grid.Column="2" Orientation="Horizontal"> <Label Content="当前显示数目:"/> <Label Content="{Binding PagerRecord}" Foreground="Orange"/> </StackPanel> <StackPanel Grid.Column="4" Orientation="Horizontal"> <Label Content="查询总量:"/> <Label Content="{Binding PagerQuantity}" Foreground="Orange"/> <Label Content="页,共"/> <Label Content="{Binding TotalRecord}" Foreground="Orange"/> <Label Content="项"/> </StackPanel> <StackPanel Grid.Column="6" Orientation="Horizontal" VerticalAlignment="Center"> <Button Content="首页" Width="40" Height="20" Foreground="White" Template="{StaticResource DefaultButton}" Command="{Binding PagerFirst}"/> <Button Content="上页" Width="60" Height="20" Foreground="White" Template="{StaticResource DefaultButton}" Command="{Binding PagerBefore}" /> <Label Content="{Binding PagerIndex}" Foreground="Orange" ToolTip="当前所在页码"/> <Button Content="下页" Width="60" Foreground="White" Template="{StaticResource DefaultButton}" Command="{Binding PagerAfter}" /> <Button Content="末页" Width="40" Height="20" Foreground="White" Template="{StaticResource DefaultButton}" Command="{Binding PagerEnd}" /> </StackPanel> </Grid> </UserControl>1.2 style代码
<ControlTemplate TargetType="{x:Type Button}" x:Key="DefaultButton"> <Border BorderBrush="{TemplateBinding Control.BorderBrush}" BorderThickness="0" Name="btn_modify_bg"> <Border.Background> <LinearGradientBrush EndPoint="0,1" StartPoint="0,0"> <GradientStop Color="#5CACEE" Offset="1.0" /> </LinearGradientBrush> </Border.Background> <ContentPresenter Content="{TemplateBinding ContentControl.Content}" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Border> <ControlTemplate.Triggers> <Trigger Property="UIElement.IsMouseOver" Value="True"> <Setter Property="Border.Background" TargetName="btn_modify_bg"> <Setter.Value> <LinearGradientBrush EndPoint="0,1" StartPoint="0,0"> <GradientStop Color="#1C86EE" Offset="0.0" /> </LinearGradientBrush> </Setter.Value> </Setter> </Trigger> <Trigger Property="ButtonBase.IsPressed" Value="True"> <Setter Property="UIElement.Effect"> <Setter.Value> <DropShadowEffect BlurRadius="10" Color="#1C86EE" Direction="0" Opacity="0.6" RenderingBias="Performance" ShadowDepth="0" /> </Setter.Value> </Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate>1.3 ViewModel业务代码
/// 分页控件 页面模型 public class PagerViewModel : BindableBase { //定义一个委托 public delegate void PageChangedHandle(object sender); //上一页下一页的触发事件 public event PageChangedHandle PageChanged; // 默认显示的页容量 public static int DefaultSize = 20; public PagerViewModel() { PagerFirst = new RelayCommand(PageFirst); PagerBefore = new RelayCommand(PageBefore); PagerAfter = new RelayCommand(PageAfter); PagerEnd = new RelayCommand(PageEnd); } private int _pagerSize = DefaultSize; //每页容量 private int _pagerIndex = 1; //第几页 private int _pagerQuantity; //多少页 private int _pagerRecord; //当前页大小 private int _totalRecord; //总量多少 // 每页容量 public int PagerSize { get => _pagerSize; set { _pagerSize = value; RaisePropertyChanged(); } } // 第几页 public int PagerIndex { get => _pagerIndex; set { _pagerIndex = value; RaisePropertyChanged(); } } // 总共有多少页 public int PagerQuantity { get => _pagerQuantity; set { _pagerQuantity = value; RaisePropertyChanged(); } } // 当前页有多少条数据 public int PagerRecord { get => _pagerRecord; set { _pagerRecord = value; RaisePropertyChanged(); } } // 查询记录的总量 public int TotalRecord { get => _totalRecord; set { _totalRecord = value; RaisePropertyChanged(); } } // 首页 public RelayCommand PagerFirst { get; set; } // 上一页 public RelayCommand PagerBefore { get; set; } // 下一页 public RelayCommand PagerAfter { get; set; } // 末页 public RelayCommand PagerEnd { get; set; } // 首页事件 private void PageFirst() { PagerIndex = 1; PageChanged?.Invoke(1); } // 上一页事件 private void PageBefore() { PagerIndex--; if (PagerIndex < 1) { PagerIndex = 1; MessageBoxHelper.ShowTips("已经是首页"); return; } PageChanged?.Invoke(PagerIndex); } // 下一页事件 private void PageAfter() { PagerIndex++; if (PagerIndex > PagerQuantity) { PagerIndex = PagerQuantity; MessageBoxHelper.ShowTips("已经是末页"); return; } PageChanged?.Invoke(PagerIndex); } // 末页事件 private void PageEnd() { PagerIndex = PagerQuantity; PageChanged?.Invoke(PagerQuantity); } // 重置分页数据 public void ResetPage() { PagerIndex = 1; PagerSize = DefaultSize; PagerRecord = 0; PagerQuantity = 0; TotalRecord = 0; } }MessageBoxHelper.ShowTip 是我封装的弹框提示信息类,等价于MessageBox.Show,只是便于以后统一修改样式或者更改提示信息时的业务处理;
自定义控件不涉及业务逻辑代码,在业务场景使用的时候,需要返回当前页容量、查询总量和查询页数;
PagerView.PagerRecord = result.Count; PagerView.TotalRecord = result.FirstOrDefault()?.TotalRecord ?? 0; PagerView.PagerQuantity=PagerView.TotalRecord / PagerView.PagerSize + 1;PageChanged是控件的委托方法,在调用该控件时,需绑定委托的事件;
//代码放置在类初始化事件中PagerView.PageChanged += SearchPageData; //委托给分页控件的查询方法可以增加比如缓存效果,点击下一页时保存当前页面数据到内存中,重新刷新再清理内存的数据;
private Dictionary<int, List<BatchProductShiftModel>> _tempPage =
new Dictionary<int, List<BatchProductShiftModel>>(); //列表内容
private int _tempIndex = 1; //记录当前页
// 分页查询 [加了缓存效果,保存查过的页码数据] // 缓存后,不会根据选择的页面大小进行调整 // 缓存已处理的数据,点击下一页时,查询总量会产生变化,因为根据条件查询,状态变了 public void SearchPageData(object str) { //记录当前页面数据 _tempPage[_tempIndex] = LstReceiveOrder.ToList(); //为下次点击分页操作做准备 在内存中记录当前页码 _tempIndex = PagerView.PagerIndex;//判断该页码是否已经在缓存中 if (_tempPage.ContainsKey(PagerView.PagerIndex)) { LstReceiveOrder.Clear(); //清理后加载数据 LstReceiveOrder.AddRange(_tempPage[PagerView.PagerIndex]); //汇总当前页数量 PagerView.PagerRecord = LstReceiveOrder.Count; //清理下面明细页的列表内容 OrderVolumes.Clear(); SelectItemOrder = string.Empty; } else SearchProductShiftData(false); }LstReceiveOrder 是查询的列表数据;
SearchProductShifData 是查询数据的方法;具体代码不贴了;
OrderVolumes 是明细页,可以去掉该代码;
2.中文转拼音代码
using System.Linq; using System.Collections.Generic; using System.Text.RegularExpressions; using Microsoft.International.Converters.PinYinConverter; namespace Common { public class PinyinResult { public List<string> FirstPingYin { get; set; } public List<string> FullPingYin { get; set; } } /// <summary> ///汉字转为拼音 /// </summary> public static class PinYinHelper { /// <summary> /// 汉字转为拼音 /// </summary> /// <param name="str">需要转换的汉字</param> public static PinyinResult ToPinYin(string str) { var chs = str.ToCharArray(); var totalPingYins = new Dictionary<int, List<string>>(); for (int i = 0; i < chs.Length; i++) { var pinyins = new List<string>(); var ch = chs[i]; //是否是有效的汉字 if (ChineseChar.IsValidChar(ch)) { var cc = new ChineseChar(ch); pinyins=cc.Pinyins.Where(p=>!string.IsNullOrWhiteSpace(p)).ToList(); } else pinyins.Add(ch.ToString()); //去除声调,转小写 pinyins=pinyins.ConvertAll(p=>Regex.Replace(p, @"\d", "").ToLower()); //去重 pinyins = pinyins.Where(p => !string.IsNullOrWhiteSpace(p)) .Distinct().ToList(); if (pinyins.Any()) totalPingYins[i] = pinyins; } var result = new PinyinResult(); foreach (var pinyins in totalPingYins) { var items = pinyins.Value; if (result.FullPingYin == null||result.FullPingYin.Count <= 0) { result.FullPingYin = items; result.FirstPingYin = items.ConvertAll(p => p.Substring(0, 1)) .Distinct().ToList(); } else { //全拼循环匹配 var newTotalPingYins = new List<string>(); foreach (var totalPingYin in result.FullPingYin) { newTotalPingYins.AddRange(items.Select(item =>totalPingYin+item)); } newTotalPingYins = newTotalPingYins.Distinct().ToList(); result.FullPingYin = newTotalPingYins; //首字母循环匹配 var newFirstPingYins = new List<string>(); foreach (var firstPingYin in result.FirstPingYin) { newFirstPingYins.AddRange(items.Select(item => firstPingYin + item.Substring(0, 1))); } newFirstPingYins = newFirstPingYins.Distinct().ToList(); result.FirstPingYin = newFirstPingYins; } } return result; } } }
3.日志代码
((.持续更新中.))
4.邮件发送代码
((.持续更新中.))
5.Excel导入导出
((.持续更新中.))
.[!周日不更新!].
6.下篇预告
干货贴代码,包含需求文案、设计思路、简要数据库结构、简要流程图和明细代码,动图细化每步操作,入门级引导文章;
项目功能包括:登录、首页、数据维护 和 全文搜索等增删查改的常用操作;
原文地址:https://www.cnblogs.com/yjwlogs/p/8467251.html