.NET Core 3 WPF MVVM框架 Prism系列之導航系統

来源:https://www.cnblogs.com/ryzen/archive/2020/04/16/12703914.html
-Advertisement-
Play Games

本文將介紹如何在.NET Core3環境下使用MVVM框架Prism基於區域Region的導航系統 在講解Prism導航系統之前,我們先來看看一個例子,我在之前的demo項目創建一個登錄界面: 我們看到這裡是不是一開始想象到使用WPF帶有的導航系統,通過Frame和Page進行頁面跳轉,然後通過導航 ...


本文將介紹如何在.NET Core3環境下使用MVVM框架Prism基於區域Region的導航系統

在講解Prism導航系統之前,我們先來看看一個例子,我在之前的demo項目創建一個登錄界面:

我們看到這裡是不是一開始想象到使用WPF帶有的導航系統,通過Frame和Page進行頁面跳轉,然後通過導航日誌的GoBack和GoForward實現後退和前進,其實這是通過使用Prism的導航框架實現的,下麵我們來看看如何在Prism的MVVM模式下實現該功能

一.區域導航

我們在上一篇介紹了Prism的區域管理,而Prism的導航系統也是基於區域的,首先我們來看看如何在區域導航

1.註冊區域

LoginWindow.xaml:

<Window x:Class="PrismMetroSample.Shell.Views.Login.LoginWindow"
        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:PrismMetroSample.Shell.Views.Login"
        xmlns:region="clr-namespace:PrismMetroSample.Infrastructure.Constants;assembly=PrismMetroSample.Infrastructure"
        mc:Ignorable="d"
        xmlns:prism="http://prismlibrary.com/"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
         Height="600" Width="400" prism:ViewModelLocator.AutoWireViewModel="True" ResizeMode="NoResize" WindowStartupLocation="CenterScreen"
        Icon="pack://application:,,,/PrismMetroSample.Infrastructure;Component/Assets/Photos/Home, homepage, menu.png" >
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <i:InvokeCommandAction Command="{Binding LoginLoadingCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <Grid>
        <ContentControl prism:RegionManager.RegionName="{x:Static region:RegionNames.LoginContentRegion}" Margin="5"/>
    </Grid>
</Window>

2.註冊導航

App.cs:

  protected override void RegisterTypes(IContainerRegistry containerRegistry)
  {
        containerRegistry.Register<IMedicineSerivce, MedicineSerivce>();
        containerRegistry.Register<IPatientService, PatientService>();
        containerRegistry.Register<IUserService, UserService>();

        //註冊全局命令
        containerRegistry.RegisterSingleton<IApplicationCommands, ApplicationCommands>();
        containerRegistry.RegisterInstance<IFlyoutService>(Container.Resolve<FlyoutService>());

        //註冊導航
        containerRegistry.RegisterForNavigation<LoginMainContent>();
        containerRegistry.RegisterForNavigation<CreateAccount>();
  }

3.區域導航

LoginWindowViewModel.cs:

public class LoginWindowViewModel:BindableBase
{

    private readonly IRegionManager _regionManager;
    private readonly IUserService _userService;
    private DelegateCommand _loginLoadingCommand;
    public DelegateCommand LoginLoadingCommand =>
            _loginLoadingCommand ?? (_loginLoadingCommand = new DelegateCommand(ExecuteLoginLoadingCommand));

    void ExecuteLoginLoadingCommand()
    {
        //在LoginContentRegion區域導航到LoginMainContent
        _regionManager.RequestNavigate(RegionNames.LoginContentRegion, "LoginMainContent");

         Global.AllUsers = _userService.GetAllUsers();
    }

    public LoginWindowViewModel(IRegionManager regionManager, IUserService userService)
    {
          _regionManager = regionManager;
          _userService = userService;            
    }

}

LoginMainContentViewModel.cs:

public class LoginMainContentViewModel : BindableBase
{
    private readonly IRegionManager _regionManager;

    private DelegateCommand _createAccountCommand;
    public DelegateCommand CreateAccountCommand =>
            _createAccountCommand ?? (_createAccountCommand = new DelegateCommand(ExecuteCreateAccountCommand));

    //導航到CreateAccount
    void ExecuteCreateAccountCommand()
    {
         Navigate("CreateAccount");
    }

    private void Navigate(string navigatePath)
    {
         if (navigatePath != null)
              _regionManager.RequestNavigate(RegionNames.LoginContentRegion, navigatePath);
        
    }


    public LoginMainContentViewModel(IRegionManager regionManager)
    {
         _regionManager = regionManager;
    }

 }

效果如下:

這裡我們可以看到我們調用RegionMannager的RequestNavigate方法,其實這樣看不能很好的說明是基於區域的做法,如果將換成下麵的寫法可能更好理解一點:

   //在LoginContentRegion區域導航到LoginMainContent
  _regionManager.RequestNavigate(RegionNames.LoginContentRegion, "LoginMainContent");

換成

 //在LoginContentRegion區域導航到LoginMainContent
 IRegion region = _regionManager.Regions[RegionNames.LoginContentRegion];
 region.RequestNavigate("LoginMainContent");

其實RegionMannager的RequestNavigate源碼也是大概實現也是大概如此,就是去調Region的RequestNavigate的方法,而Region的導航是實現了一個INavigateAsync介面:

public interface INavigateAsync
{
   void RequestNavigate(Uri target, Action<NavigationResult> navigationCallback);
   void RequestNavigate(Uri target, Action<NavigationResult> navigationCallback, NavigationParameters navigationParameters);
    
}   

我們可以看到有RequestNavigate方法三個形參:

  • target:表示將要導航的頁面Uri
  • navigationCallback:導航後的回調方法
  • navigationParameters:導航傳遞參數(下麵會詳解)

那麼我們將上述加上回調方法:

 //在LoginContentRegion區域導航到LoginMainContent
 IRegion region = _regionManager.Regions[RegionNames.LoginContentRegion];
 region.RequestNavigate("LoginMainContent", NavigationCompelted);

 private void NavigationCompelted(NavigationResult result)
 {
     if (result.Result==true)
     {
         MessageBox.Show("導航到LoginMainContent頁面成功");
     }
     else
     {
         MessageBox.Show("導航到LoginMainContent頁面失敗");
     }
 }

效果如下:

二.View和ViewModel參與導航過程

1.INavigationAware

我們經常在兩個頁面之間導航需要處理一些邏輯,例如,LoginMainContent頁面導航到CreateAccount頁面時候,LoginMainContent退出頁面的時刻要保存頁面數據,導航到CreateAccount頁面的時刻處理邏輯(例如獲取從LoginMainContent頁面的信息),Prism的導航系統通過一個INavigationAware介面:

    public interface INavigationAware : Object
    {
        Void OnNavigatedTo(NavigationContext navigationContext);

        Boolean IsNavigationTarget(NavigationContext navigationContext);

        Void OnNavigatedFrom(NavigationContext navigationContext);
    }
  • OnNavigatedFrom:導航之前觸發,一般用於保存該頁面的數據
  • OnNavigatedTo:導航後目的頁面觸發,一般用於初始化或者接受上頁面的傳遞參數
  • IsNavigationTarget:True則重用該View實例,Flase則每一次導航到該頁面都會實例化一次

我們用代碼來演示這三個方法:

LoginMainContentViewModel.cs:

public class LoginMainContentViewModel : BindableBase, INavigationAware
{
     private readonly IRegionManager _regionManager;

     private DelegateCommand _createAccountCommand;
     public DelegateCommand CreateAccountCommand =>
            _createAccountCommand ?? (_createAccountCommand = new DelegateCommand(ExecuteCreateAccountCommand));

     void ExecuteCreateAccountCommand()
     {
         Navigate("CreateAccount");
     }

     private void Navigate(string navigatePath)
     {
         if (navigatePath != null)
            _regionManager.RequestNavigate(RegionNames.LoginContentRegion, navigatePath);
     }

     public LoginMainContentViewModel(IRegionManager regionManager)
     {
          _regionManager = regionManager;
     }

     public bool IsNavigationTarget(NavigationContext navigationContext)
     {            
          return true;
     }

     public void OnNavigatedFrom(NavigationContext navigationContext)
     {
          MessageBox.Show("退出了LoginMainContent");
     }

     public void OnNavigatedTo(NavigationContext navigationContext)
     {
          MessageBox.Show("從CreateAccount導航到LoginMainContent");
     }
 }

CreateAccountViewModel.cs:

public class CreateAccountViewModel : BindableBase,INavigationAware
{
     private DelegateCommand _loginMainContentCommand;
     public DelegateCommand LoginMainContentCommand =>
            _loginMainContentCommand ?? (_loginMainContentCommand = new DelegateCommand(ExecuteLoginMainContentCommand));

     void ExecuteLoginMainContentCommand()
     {
         Navigate("LoginMainContent");
     }

     public CreateAccountViewModel(IRegionManager regionManager)
     {
         _regionManager = regionManager;
     }

     private void Navigate(string navigatePath)
     {
        if (navigatePath != null)
            _regionManager.RequestNavigate(RegionNames.LoginContentRegion, navigatePath);
     }

     public bool IsNavigationTarget(NavigationContext navigationContext)
     {
         return true;
     }

     public void OnNavigatedFrom(NavigationContext navigationContext)
     {
         MessageBox.Show("退出了CreateAccount");
     }

     public void OnNavigatedTo(NavigationContext navigationContext)
     {
         MessageBox.Show("從LoginMainContent導航到CreateAccount");
     }

 }

效果如下:

修改IsNavigationTarget為false:

public class LoginMainContentViewModel : BindableBase, INavigationAware
{
     public bool IsNavigationTarget(NavigationContext navigationContext)
     {            
          return false;
     }
}

public class CreateAccountViewModel : BindableBase,INavigationAware
{
     public bool IsNavigationTarget(NavigationContext navigationContext)
     {
         return false;
     }
 }

效果如下:

我們會發現LoginMainContent和CreateAccount頁面的數據不見了,這是因為第二次導航到頁面的時候當IsNavigationTarget為false時,View將會重新實例化,導致ViewModel也重新載入,因此所有數據都清空了

2.IRegionMemberLifetime

同時,Prism還可以通過IRegionMemberLifetime介面的KeepAlive布爾屬性控制區域的視圖的生命周期,我們在上一篇關於區域管理器說到,當視圖添加到區域時候,像ContentControl這種單獨顯示一個活動視圖,可以通過RegionActivateDeactivate方法激活和失效視圖,像ItemsControl這種可以同時顯示多個活動視圖的,可以通過RegionAddRemove方法控制增加活動視圖和失效視圖,而當視圖的KeepAlivefalseRegionActivate另外一個視圖時,則該視圖的實例則會去除出區域,為什麼我們不在區域管理器講解該介面呢?因為當導航的時候,同樣的是在觸發了RegionActivateDeactivate,當有IRegionMemberLifetime介面時則會觸發RegionAddRemove方法,這裡可以去看下Prism的RegionMemberLifetimeBehavior源碼

我們將LoginMainContentViewModel實現IRegionMemberLifetime介面,並且把KeepAlive設置為false,同樣的將IsNavigationTarget設置為true

LoginMainContentViewModel.cs:

public class LoginMainContentViewModel : BindableBase, INavigationAware,IRegionMemberLifetime
{

     public bool KeepAlive => false;
     
     private readonly IRegionManager _regionManager;

     private DelegateCommand _createAccountCommand;
     public DelegateCommand CreateAccountCommand =>
            _createAccountCommand ?? (_createAccountCommand = new DelegateCommand(ExecuteCreateAccountCommand));

     void ExecuteCreateAccountCommand()
     {
         Navigate("CreateAccount");
     }

     private void Navigate(string navigatePath)
     {
         if (navigatePath != null)
            _regionManager.RequestNavigate(RegionNames.LoginContentRegion, navigatePath);
     }

     public LoginMainContentViewModel(IRegionManager regionManager)
     {
          _regionManager = regionManager;
     }

     public bool IsNavigationTarget(NavigationContext navigationContext)
     {            
          return true;
     }

     public void OnNavigatedFrom(NavigationContext navigationContext)
     {
          MessageBox.Show("退出了LoginMainContent");
     }

     public void OnNavigatedTo(NavigationContext navigationContext)
     {
          MessageBox.Show("從CreateAccount導航到LoginMainContent");
     }
 }

效果如下:

我們會發現跟沒實現IRegionMemberLifetime介面和IsNavigationTarget設置為false情況一樣,當KeepAlivefalse時,通過斷點知道,重新導航回LoginMainContent頁面時不會觸發IsNavigationTarget方法,因此可以

知道判斷順序是:KeepAlive -->IsNavigationTarget

3.IConfirmNavigationRequest

Prism的導航系統還支持再導航前允許是否需要導航的交互需求,這裡我們在CreateAccount註冊完用戶後尋問是否需要導航回LoginMainContent頁面,代碼如下:

CreateAccountViewModel.cs:

public class CreateAccountViewModel : BindableBase, INavigationAware,IConfirmNavigationRequest
{
     private DelegateCommand _loginMainContentCommand;
     public DelegateCommand LoginMainContentCommand =>
            _loginMainContentCommand ?? (_loginMainContentCommand = new DelegateCommand(ExecuteLoginMainContentCommand));
    
     private DelegateCommand<object> _verityCommand;
     public DelegateCommand<object> VerityCommand =>
            _verityCommand ?? (_verityCommand = new DelegateCommand<object>(ExecuteVerityCommand));

     void ExecuteLoginMainContentCommand()
     {
         Navigate("LoginMainContent");
     }

     public CreateAccountViewModel(IRegionManager regionManager)
     {
         _regionManager = regionManager;
     }

     private void Navigate(string navigatePath)
     {
        if (navigatePath != null)
            _regionManager.RequestNavigate(RegionNames.LoginContentRegion, navigatePath);
     }

     public bool IsNavigationTarget(NavigationContext navigationContext)
     {
         return true;
     }

     public void OnNavigatedFrom(NavigationContext navigationContext)
     {
         MessageBox.Show("退出了CreateAccount");
     }

     public void OnNavigatedTo(NavigationContext navigationContext)
     {
         MessageBox.Show("從LoginMainContent導航到CreateAccount");
     }
    
     //註冊賬號
     void ExecuteVerityCommand(object parameter)
     {
         if (!VerityRegister(parameter))
         {
                return;
         }
         MessageBox.Show("註冊成功!");
         LoginMainContentCommand.Execute();
     }
    
     //導航前詢問
     public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
     {
         var result = false;
         if (MessageBox.Show("是否需要導航到LoginMainContent頁面?", "Naviagte?",MessageBoxButton.YesNo) ==MessageBoxResult.Yes)
         {
             result = true;
         }
         continuationCallback(result);
      }
 }

效果如下:

三.導航期間傳遞參數

Prism提供NavigationParameters類以幫助指定和檢索導航參數,在導航期間,可以通過訪問以下方法來傳遞導航參數:

  • INavigationAware介面的IsNavigationTargetOnNavigatedFromOnNavigatedTo方法中IsNavigationTargetOnNavigatedFromOnNavigatedTo中形參NavigationContext對象的NavigationParameters屬性
  • IConfirmNavigationRequest介面的ConfirmNavigationRequest形參NavigationContext對象的NavigationParameters屬性
  • 區域導航的INavigateAsync介面的RequestNavigate方法賦值給其形參navigationParameters
  • 導航日誌IRegionNavigationJournal介面CurrentEntry屬性的NavigationParameters類型的Parameters屬性(下麵會介紹導航日誌)

這裡我們CreateAccount頁面註冊完用戶後詢問是否需要用當前註冊用戶來作為登錄LoginId,來演示傳遞導航參數,代碼如下:

CreateAccountViewModel.cs(修改代碼部分):

private string _registeredLoginId;
public string RegisteredLoginId
{
    get { return _registeredLoginId; }
    set { SetProperty(ref _registeredLoginId, value); }
}

public bool IsUseRequest { get; set; }

void ExecuteVerityCommand(object parameter)
{
    if (!VerityRegister(parameter))
    {
        return;
    }
    this.IsUseRequest = true;
    MessageBox.Show("註冊成功!");
    LoginMainContentCommand.Execute();
}   

public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
{
    if (!string.IsNullOrEmpty(RegisteredLoginId) && this.IsUseRequest)
    {
         if (MessageBox.Show("是否需要用當前註冊的用戶登錄?", "Naviagte?", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
         {
              navigationContext.Parameters.Add("loginId", RegisteredLoginId);
         }
    }
    continuationCallback(true);
}

LoginMainContentViewModel.cs(修改代碼部分):

public void OnNavigatedTo(NavigationContext navigationContext)
{
     MessageBox.Show("從CreateAccount導航到LoginMainContent");
            
     var loginId= navigationContext.Parameters["loginId"] as string;
     if (loginId!=null)
     {
         this.CurrentUser = new User() { LoginId=loginId};
     }
            
 }

效果如下:

四.導航日誌

Prism導航系統同樣的和WPF導航系統一樣,都支持導航日誌,Prism是通過IRegionNavigationJournal介面來提供區域導航日誌功能,

    public interface IRegionNavigationJournal
    {
        bool CanGoBack { get; }

        bool CanGoForward { get; }

        IRegionNavigationJournalEntry CurrentEntry {get;}

        INavigateAsync NavigationTarget { get; set; }

        void GoBack();

        void GoForward();

        void RecordNavigation(IRegionNavigationJournalEntry entry, bool persistInHistory);

        void Clear();
    }

我們將在登錄界面接入導航日誌功能,代碼如下:

LoginMainContent.xaml(前進箭頭代碼部分):

<TextBlock Width="30" Height="30" HorizontalAlignment="Right" Text="&#xe624;" FontWeight="Bold" FontFamily="pack://application:,,,/PrismMetroSample.Infrastructure;Component/Assets/Fonts/#iconfont" FontSize="30" Margin="10" Visibility="{Binding IsCanExcute,Converter={StaticResource boolToVisibilityConverter}}">
      <i:Interaction.Triggers>
           <i:EventTrigger EventName="MouseLeftButtonDown">
                 <i:InvokeCommandAction Command="{Binding GoForwardCommand}"/>
            </i:EventTrigger>
      </i:Interaction.Triggers>
      <TextBlock.Style>
           <Style TargetType="TextBlock">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                          <Setter Property="Background" Value="#F9F9F9"/>
                    </Trigger>
                 </Style.Triggers>
            </Style>
      </TextBlock.Style>
 </TextBlock>

BoolToVisibilityConverter.cs:

public class BoolToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
          if (value==null)
          {
              return DependencyProperty.UnsetValue;
          }
          var isCanExcute = (bool)value;
          if (isCanExcute)
          {
              return Visibility.Visible;
          }
          else
          {
              return Visibility.Hidden;
          }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
            throw new NotImplementedException();
    }
 }

LoginMainContentViewModel.cs(修改代碼部分):

IRegionNavigationJournal _journal;

private DelegateCommand<PasswordBox> _loginCommand;
public DelegateCommand<PasswordBox> LoginCommand =>
            _loginCommand ?? (_loginCommand = new DelegateCommand<PasswordBox>(ExecuteLoginCommand, CanExecuteGoForwardCommand));

private DelegateCommand _goForwardCommand;
public DelegateCommand GoForwardCommand =>
            _goForwardCommand ?? (_goForwardCommand = new DelegateCommand(ExecuteGoForwardCommand));

private void ExecuteGoForwardCommand()
{
    _journal.GoForward();
}

private bool CanExecuteGoForwardCommand(PasswordBox passwordBox)
{
    this.IsCanExcute=_journal != null && _journal.CanGoForward;
    return true;
}

public void OnNavigatedTo(NavigationContext navigationContext)
{
     //MessageBox.Show("從CreateAccount導航到LoginMainContent");
     _journal = navigationContext.NavigationService.Journal;

     var loginId= navigationContext.Parameters["loginId"] as string;
     if (loginId!=null)
     {
                this.CurrentUser = new User() { LoginId=loginId};
     }
     LoginCommand.RaiseCanExecuteChanged();
}

CreateAccountViewModel.cs(修改代碼部分):

IRegionNavigationJournal _journal;

private DelegateCommand _goBackCommand;
public DelegateCommand GoBackCommand =>
            _goBackCommand ?? (_goBackCommand = new DelegateCommand(ExecuteGoBackCommand));

void ExecuteGoBackCommand()
{
     _journal.GoBack();
}

 public void OnNavigatedTo(NavigationContext navigationContext)
 {
     //MessageBox.Show("從LoginMainContent導航到CreateAccount");
     _journal = navigationContext.NavigationService.Journal;
 }

效果如下:

選擇退出導航日誌

如果不打算將頁面在導航過程中不加入導航日誌,例如LoginMainContent頁面,可以通過實現IJournalAware並從PersistInHistory()返回false

    public class LoginMainContentViewModel : IJournalAware
    {
        public bool PersistInHistory() => false;
    }   

五.小結:

prism的導航系統可以跟wpf導航並行使用,這是prism官方文檔也支持的,因為prism的導航系統是基於區域的,不依賴於wpf,不過更推薦於單獨使用prism的導航系統,因為在MVVM模式下更靈活,支持依賴註入,通過區域管理器能夠更好的管理視圖View,更能適應複雜應用程式需求,wpf導航系統不支持依賴註入模式,也依賴於Frame元素,而且在導航過程中也是容易強依賴View部分,下一篇將會講解Prism的對話框服務

六.源碼

 最後,附上整個demo的源代碼:PrismDemo源碼


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • private void timer1_Tick(object sender, EventArgs e) { label1.Left -= 2; if (label1.Right < 0) { label1.Left = this.Width; } } 在窗體的定時器上編寫上面的代碼就可以看到效果了 ...
  • 只讀的自動屬性 通過聲明只有get訪問器的自動屬性,實現該屬性只讀 public string FirstName { get; } public string LastName { get; } 自動只讀屬性在能在構造函數中賦值,任何其他地方的賦值都會報編譯錯誤。 自動屬性初始化器 在聲明自動屬性 ...
  •  <import name="address" src="../Common/ui/h-ui/advance/c_address"></import> <template> <div class="container"> <address header-text="聯繫作者" header-sty ...
  • 護照資料也下方的兩行成為MRZ碼(或護照機讀碼),每行44個字元(0 9,A Z,<),如下例: 1 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 201 2 3 4 5 6 7 8 9 301 2 3 4 5 6 7 8 9 401 2 3 4 P O C H N Z H ...
  • 前幾天要做一個數據導出Excel 我就打算寫一個通用的。 這樣一來用的時候也方便,數據主要是通過Orm取的List。這樣寫一個通用的剛好。 public static void ListToExcel(List<dynamic> ts, string[] RowName, string[] List ...
  • 最近很對朋友、同事問起,vs怎麼自動把css、js合併壓縮,所以這裡簡單說說介紹 插件功能、特征說明: 1、將CSS,JavaScript或HTML文件捆綁到單個輸出文件中 2、保存源文件會自動觸發重新捆綁 3、縮小單個或捆綁的CSS,JavaScript和HTML文件 4、支持globbing模式 ...
  • 測試代碼: 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.L ...
  • 檢索 COM 類工廠中 CLSID 為 {000209FF-0000-0000-C000-000000000046} 的組件時失敗,原因是出現以下錯誤: 80070005 報錯信息為:檢索 COM 類工廠中 CLSID 為 {000209FF-0000-0000-C000-000000000046} ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...