你相信 “物竞天择,适者生存” 这样的学说吗?但是我们今天却在提倡 “尊老爱幼,救死扶伤” ,帮助并救护弱势群体;第二次世界大战期间,希特勒认为自己是优等民族,劣势民族应该被消灭,这样的思想带来的战争之痛,至今让人难以忘怀。我们的文化里面有这样一句话 “天无绝人之路” ,在西方世界是 “神爱世人”。 这个世代所创造的生产力大过先前的任何世代,但是这个世代的人过的仿佛比任何一个世代的人都忙碌;能否今天已经感到无路可走,或是说今天已经在经济上迫在眉睫的时候,心里不被忧虑或是烦乱抓住呢?当思想今天我们生活在这个这么恰到好处的自然界时,我相信,我们比麻雀贵重的多,深被造物主所爱。
这次博客的标题是数据库,自定义弹窗,表单验证;我们的目标是一个实用的权限控制框架,所以我觉得自己更像是一个组装产品的人,把其他人分享的成果拿来一点一点组装;好了,先看下截图:
接下来就们就针对这几个方面逐个来介绍下:
数据库:
今天我们在操作数据库的时候,已经很难接受手写sql语句的做法了,很多的时候都会考虑选用ORM框架,即可以自由的使用linq表达式,在特殊的场合又可以使用sql语句;原本打算使用dapper,但是在使用linq表达式的时候,发现网上可参考的资料不多,思索一番,决定使用先前用过的chloe。目前数据库使用的是sqlite数据库,当然大家要是需要改换其他的数据库,自行改换就是了,在项目中已经引入了针对SqlServer,MySql,Oracle的chloe所支持的组件,并且数据操作类也留下了其他数据库的扩展入口,看下截图与代码:
1 using Chloe; 2 using Chloe.SQLite; 3 using System; 4 using System.Collections.Generic; 5 using System.Configuration; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 10 namespace HQ.Plugin.SysManagerPlugin.Common 11 { 12 public class DbHelper 13 { 14 15 private static readonly string dbType = ConfigurationManager.AppSettings["DbType"].ToLower(); 16 private static readonly string sqliteconn = ConfigurationManager.ConnectionStrings["SQLiteConnectionString"].ConnectionString; 17 private static IDbContext sqliteDbContext; 18 private static IDbContext SqliteDbContext 19 { 20 get 21 { 22 if (sqliteDbContext == null) 23 { 24 sqliteDbContext = new SQLiteContext(new SQLiteConnectionFactory(sqliteconn)); 25 } 26 return sqliteDbContext; 27 } 28 set 29 { 30 sqliteDbContext = value; 31 } 32 } 33 private static IDbContext dbContext; 34 public static IDbContext DbContext 35 { 36 get 37 { 38 switch (dbType) 39 { 40 case "sqlite": 41 dbContext = SqliteDbContext; 42 break; 43 } 44 return dbContext; 45 } 46 set 47 { 48 dbContext = value; 49 } 50 } 51 52 } 53 } 数据操作类
自定义弹窗:
自定义弹窗是通过在Window窗体界面中加入ContentControl控件,然后在ContentControl控件中通过加载用户控件来实现的,效果是这样的:
贴下Window窗体的界面布局代码:
1 <Window x:Class="HQ.Plugin.SysManagerPlugin.View.Dialog.CustomDialog" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:HQ.Plugin.SysManagerPlugin.View.Dialog" 7 mc:Ignorable="d" 8 WindowStartupLocation="CenterScreen" ResizeMode="NoResize" BorderBrush="{DynamicResource IndexColor}" BorderThickness="1" WindowStyle="None" 9 10 > 11 <Grid> 12 <Grid> 13 <Grid.RowDefinitions> 14 <RowDefinition Height="30"></RowDefinition> 15 <RowDefinition Height="*"></RowDefinition> 16 <RowDefinition Height="Auto"></RowDefinition> 17 </Grid.RowDefinitions> 18 <DockPanel Grid.Row="0" Name="TitleBar" Cursor="Hand"> 19 <DockPanel.Background> 20 <LinearGradientBrush StartPoint="0,0" EndPoint="1,1"> 21 <GradientStop Color="White" Offset="0"/> 22 <GradientStop Color="#ADD8E6" Offset="1"/> 23 </LinearGradientBrush> 24 </DockPanel.Background> 25 <TextBlock DockPanel.Dock="Left" Name="DialogTitle" Foreground="Gray" Margin="5 7 5 0"/> 26 <Button DockPanel.Dock="Right" Content="✕" IsCancel="True" HorizontalAlignment="Right" BorderBrush="Transparent" BorderThickness="0" Width="30" Background="Transparent" Foreground="Gray"/> 27 </DockPanel> 28 <Border Grid.Row="1" Margin="5 5 5 0" BorderThickness="0" BorderBrush="{DynamicResource IndexColor}"> 29 <ContentControl x:Name="contentContainer" /> 30 </Border> 31 <Border Grid.Row="2" BorderThickness="0 1 0 0" BorderBrush="{DynamicResource IndexColor}" Height="40" Margin="5 0 5 5"> 32 <WrapPanel Name="dpBottom" HorizontalAlignment="Right" VerticalAlignment="Center"> 33 <Button Content="确定" Name="btnOK" /> 34 <Button Content="取消" Name="btnCancel" IsCancel="True" /> 35 </WrapPanel> 36 </Border> 37 </Grid> 38 </Grid> 39 </Window> View Code
这是使用弹出层的代码:
1 private void AddEvent() 2 { 3 var userControl = new View.RoleDialog.Add(); 4 RoleAdd = new RoleAddViewModel(); 5 userControl.DataContext = RoleAdd; 6 CustomDialog dialog = new CustomDialog(userControl, "添加", LoginUserHelper.MainWindow, userControl.Height, userControl.Width); 7 dialog.ShowDialog(AddRoles); 8 } View Code
表单验证:
因为源码会分享出来,大家自由查看,所以这里就只是做下大致的介绍,对源码感兴趣的朋友,欢迎加入
QQ群:720369133
源码会在群里给大家分享,也恳请大家提出宝贵意见!