WPF許可權控制——【3】資料庫、自定義彈窗、表單驗證
你相信「物競天擇,適者生存」這樣的學說嗎?但是我們今天卻在提倡「尊老愛幼,救死扶傷」,幫助並救護弱勢群體;第二次世界大戰期間,希特勒認為自己是優等民族,劣勢民族應該被消滅,這樣的思想帶來的戰爭之痛,至今讓人難以忘懷。我們的文化裡面有這樣一句話「天無絕人之路」,在西方世界是「神愛世人」。這個世代所創造的生產力大過先前的任何世代,但是這個世代的人過的彷彿比任何一個世代的人都忙碌;能否今天已經感到無路可走,或是說今天已經在經濟上迫在眉睫的時候,心裡不被憂慮或是煩亂抓住呢?當思想今天我們生活在這個這麼恰到好處的自然界時,我相信,我們比麻雀貴重的多,深被造物主所愛。
這次部落格的標題是資料庫,自定義彈窗,表單驗證;我們的目標是一個實用的許可權控制框架,所以我覺得自己更像是一個組裝產品的人,把其他人分享的成果拿來一點一點組裝;好了,先看下截圖:
接下來就們就針對這幾個方面逐個來介紹下:
資料庫:
今天我們在操作資料庫的時候,已經很難接受手寫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="//schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="//schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:d="//schemas.microsoft.com/expression/blend/2008"
5 xmlns:mc="//schemas.openxmlformats.org/markup-compatibility/2006"
6 xmlns:local="clr-namespace:HQ.Plugin.SysManagerPlugin.View.Dialog"
7 mc:Ignorable="d"
8 Title="CustomDialog" 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" Style="{StaticResource ButtonBaseStyle}" />
34 <Button Content="取消" Name="btnCancel" IsCancel="True" Style="{StaticResource ButtonBaseStyle}" />
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
源碼會在群里給大家分享,也懇請大家提出寶貴意見!
系列目錄: