1. 程式人生 > 實用技巧 >DevExpress 使用模組化開發

DevExpress 使用模組化開發

DevExpress https://docs.devexpress.com/WPF/7875/wpf-controls?v=19.2
一款收費的.net 平臺前端全家桶重量級開發框架,支援.net core
桌面:WPF、WinForm
Web:Aps.net控制元件MVC等擴充套件
移動端:Xamarin.Forms 控制元件支援
基本常用的他都有
因公司需要和需求(個人一點都不喜歡它),就學習了下用它做Wpf桌面應用,基本常用的功能控制元件都有,MVVM、模組化開發等等,全家桶都是他家的,當然也可以用它和Prims一起開發


使用方式
1.安裝DevExpress,下載安裝包,然後下一步就行了,試用時間有限制,當然可以通過其他手動搞定

2.建立專案也簡單,因為安裝後vs也整合進來了,可以根據所需建立專案,建立模組化直接選擇MIF選項,建立完後直接可以執行,簡單程式碼模組程式碼基本都有


按需載入不同的模組
假如現在有很多個模組,程式啟動的時候都要註冊很多的模組可能導致程式很慢,這個時候,可以考慮按需載入模組, 程式啟動的時候我載入一個程式啟動後需要顯示的模組就行,
當我點選一個按鈕的時候,去載入這個按鈕點選後需要展示的模組。

為每個模組定義一個 唯一標識 ,然後為模組中的使用者控制元件定義唯一的 RegionsName

 1  public static class Modules
 2     {
3 public const string M_Main="M.Main"; 4 public const string M_Main2 = "M.Main2"; 5 } 6 7 8 public static class Regions 9 { 10 public const string M = "M.Main"; 11 public const string M_A1 = "M_A1"; 12 13 public const string M2 = "M2.Main"; 14
15 }
View Code

在空window檢視中載入模組的Maia

 1 <dx:ThemedWindow
 2     x:Class="DevExpressDemo.Main.Views.M1"
 3     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 4     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 5     xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
 6     Title="M1" Height="800" Width="1000"
 7    xmlns:viewModels="clr-namespace:DevExpressDemo.Main.ViewModels"
 8     xmlns:common="clr-namespace:DevExpressDemo.Common;assembly=DevExpressDemo.Common"
 9     xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
10     dxmvvm:UIRegion.Region="{x:Static common:Regions.M}"
11     >
12 
13     <dx:ThemedWindow.DataContext>
14         <viewModels:M1ViewModel/>
15     </dx:ThemedWindow.DataContext>
16 
17 </dx:ThemedWindow>
View Code

dxmvvm:UIRegion.Region="{x:Static common:Regions.M}" 指定了載入模組中的那個使用者控制元件

<dx:ThemedWindow.DataContext>
<viewModels:M1ViewModel/>
</dx:ThemedWindow.DataContext>

指定了要繫結的實體

然後在Viewmodel的建構函式中寫

 1  public class BaseVM: ViewModelBase
 2     {
 3        
 4         public void Register<T>(string region, string key, object obj)
 5         {
 6             try
 7             {
 8                 var module = ModuleManager.DefaultManager.GetModule(region, key);
 9                 if (module == null)
10                 {
11                     Manager.RegisterOrInjectOrNavigate(region, new Module(key, () => obj, typeof(T)));
12                     // Manager.InjectOrNavigate(region, key);
13                 }
14             }
15             catch (Exception ex)
16             {
17                 throw new InvalidOperationException(ex.Message);
18             }
19 
20         }
21 22 
23 
24  public class M1ViewModel:BaseVM
25     {
26 
27         public M1ViewModel()
28         {
29             Register<Modules.Views.MainView>(Regions.M, "KeyWindows", new DevExpressDemo.Modules.ViewModels.MainViewModel());
30         }
31 
32     }
View Code

這樣就註冊到了模組中,


這裡註冊的Main是一個類庫專案中的主模組,當然主模組中還可以包含許多的小模組,

主程式是Wpf專案,模組1, 模組2 是類庫專案
程式在啟動的時候,先載入主程式的window物件,接著初始化模組1,將模組1的main使用者控制元件填充到window裡面
然後main中初始化,多個小模組,呈現在模組1中

模組1中Main程式碼

 1 <UserControl 
 2     x:Class="DevExpressDemo.Modules.Views.MainView"
 3     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 4     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 5     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 7     xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" 
 8     xmlns:ViewModels="clr-namespace:DevExpressDemo.Modules.ViewModels"
 9     xmlns:region="clr-namespace:DevExpressDemo.Common;assembly=DevExpressDemo.Common"
10     mc:Ignorable="d" >
11     <UserControl.DataContext>
12         <ViewModels:MainViewModel/>
13     </UserControl.DataContext>
14     <DockPanel LastChildFill="True">
15 
16         <Grid Margin="20,5,20,10" DockPanel.Dock="Top">
17             <Grid.ColumnDefinitions>
18                 <ColumnDefinition></ColumnDefinition>
19             </Grid.ColumnDefinitions>
20             <Grid.RowDefinitions>
21                 <RowDefinition Height="Auto"></RowDefinition>
22                 <RowDefinition Height="Auto"></RowDefinition>
23                 <RowDefinition Height="Auto"></RowDefinition>
24             </Grid.RowDefinitions>
25            
26             <Border  Background="Blue" Height="100"  Grid.Row="1" Grid.Column="0">
27                 <ContentPresenter   dxmvvm:UIRegion.Region="{x:Static region:Regions.M_V1}" />
28             </Border>
29             <Border Background="Red" Height="100" Margin="0,10,0,0" Grid.Row="2" Grid.Column="0" >
30                 <ContentPresenter   dxmvvm:UIRegion.Region="{x:Static region:Regions.M_V1}" />
31             </Border>
32 
33         </Grid>
34 
35 
36     </DockPanel>
37 </UserControl>
View Code

MainViewModel

 1  public class MainViewModel : BaseVM
 2     {
 3 
 4         public MainViewModel()
 5         {
 6             Initial();
 7         }
 8 
 9         public void Initial()
10         {
11             Register<View1>(Regions.M_V1, "Key0001", Create<ViewModel1>());
12             Register<View2>(Regions.M_V2, "Key0001", Create<ViewModel2>());
13 
14         }
15     }
View Code

ViewModel1和ViewModel2繼承下bavm,

參考資料 https://docs.devexpress.com/WPF/7875/wpf-controls?v=19.2

只是做個筆記而已