.NET Core 3 WPF MVVM框架 Prism系列之命令

来源:https://www.cnblogs.com/ryzen/archive/2020/01/03/12143825.html
-Advertisement-
Play Games

本文將介紹如何在.NET Core3環境下使用MVVM框架Prism的命令的用法 一.創建DelegateCommand命令 我們在上一篇.NET Core 3 WPF MVVM框架 Prism系列之數據綁定中知道prism實現數據綁定的方式,我們按照標準的寫法來實現,我們分別創建Views文件夾和 ...


本文將介紹如何在.NET Core3環境下使用MVVM框架Prism的命令的用法

一.創建DelegateCommand命令

     我們在上一篇.NET Core 3 WPF MVVM框架 Prism系列之數據綁定中知道prism實現數據綁定的方式,我們按照標準的寫法來實現,我們分別創建Views文件夾和ViewModels文件夾,將MainWindow放在Views文件夾下,再在ViewModels文件夾下麵創建MainWindowViewModel類,如下:

 

xaml代碼如下:

<Window x:Class="CommandSample.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:CommandSample"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="450" prism:ViewModelLocator.AutoWireViewModel="True">
    <StackPanel >
        <TextBox Margin="10" Text="{Binding CurrentTime}" FontSize="32"  />
        <Button  x:Name="mybtn"  FontSize="30"  Content="Click Me" Margin="10" Height="60" Command="{Binding GetCurrentTimeCommand}"/>
        <Viewbox Height="80" >
            <CheckBox IsChecked="{Binding IsCanExcute}"  Content="CanExcute" Margin="10"  HorizontalAlignment="Center" VerticalAlignment="Center" />
        </Viewbox>
    </StackPanel>
</Window>

MainWindowViewModel類代碼如下:

using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Windows.Controls;

namespace CommandSample.ViewModels
{
   public class MainWindowViewModel: BindableBase
    {
        private bool _isCanExcute;
        public bool IsCanExcute
        {
            get { return _isCanExcute; }
            set 
            { 
                SetProperty(ref _isCanExcute, value);
                GetCurrentTimeCommand.RaiseCanExecuteChanged();
            }
        }

        private string _currentTime;
        public string CurrentTime
        {
            get { return _currentTime; }
            set { SetProperty(ref _currentTime, value); }
        }

        private DelegateCommand _getCurrentTimeCommand;
        public DelegateCommand GetCurrentTimeCommand =>
            _getCurrentTimeCommand ?? (_getCurrentTimeCommand = new DelegateCommand(ExecuteGetCurrentTimeCommand, CanExecuteGetCurrentTimeCommand));

        void ExecuteGetCurrentTimeCommand()
        {
            this.CurrentTime = DateTime.Now.ToString();
        }

        bool CanExecuteGetCurrentTimeCommand()
        {
            return IsCanExcute;
        }
    }
}

運行效果如下:

      在代碼中,我們通過using Prism.Mvvm引入繼承BindableBase,因為我們要用到屬性改變通知方法SetProperty,這在我們上一篇就知道了,再來我們using Prism.Commands,我們所定義的DelegateCommand類型就在該命名空間下,我們知道,ICommand介面是有三個函數成員的,事件CanExecuteChanged,一個返回值bool的,且帶一個參數為object的CanExecute方法,一個無返回值且帶一個參數為object的Execute方法,很明顯我們實現的GetCurrentTimeCommand命令就是一個不帶參數的命令

      還有一個值得註意的是,我們通過Checkbox的IsChecked綁定了一個bool屬性IsCanExcute,且在CanExecute方法中return IsCanExcute,我們都知道CanExecute控制著Execute方法的是否能夠執行,也控制著Button的IsEnable狀態,而在IsCanExcute的set方法我們增加了一句:

GetCurrentTimeCommand.RaiseCanExecuteChanged();

其實通過prism源碼我們可以知道RaiseCanExecuteChanged方法就是內部調用ICommand介面下的CanExecuteChanged事件去調用CanExecute方法

public void RaiseCanExecuteChanged()
{
    OnCanExecuteChanged();
}

protected virtual void OnCanExecuteChanged()
{
    EventHandler handler = this.CanExecuteChanged;
    if (handler != null)
    {
        if (_synchronizationContext != null && _synchronizationContext != SynchronizationContext.Current)
        {
            _synchronizationContext.Post(delegate
            {
                handler(this, EventArgs.Empty);
            }, null);
        }
        else
        {
            handler(this, EventArgs.Empty);
        }
    }
}

其實上述prism還提供了一個更簡潔優雅的寫法:

 private bool _isCanExcute;
 public bool IsCanExcute
 {
    get { return _isCanExcute; }
    set { SetProperty(ref _isCanExcute, value);}
 }

 private DelegateCommand _getCurrentTimeCommand;
 public DelegateCommand GetCurrentTimeCommand =>
    _getCurrentTimeCommand ?? (_getCurrentTimeCommand = new  DelegateCommand(ExecuteGetCurrentTimeCommand).ObservesCanExecute(()=> IsCanExcute));

 void ExecuteGetCurrentTimeCommand()
 {
    this.CurrentTime = DateTime.Now.ToString();
 }

其中用了ObservesCanExecute方法,其實在該方法內部中也是會去調用RaiseCanExecuteChanged方法

我們通過上面代碼我們可以會引出兩個問題:

  • 如何創建帶參數的DelegateCommand?

  • 假如控制項不包含依賴屬性Command,我們要用到該控制項的事件,如何轉為命令?

 

二.創建DelegateCommand帶參命令

在創建帶參的命令之前,我們可以來看看DelegateCommand的繼承鏈和暴露出來的公共方法,詳細的實現可以去看下源碼

 

 

那麼,其實已經很明顯了,我們之前創建DelegateCommand不是泛型版本,當創建一個泛型版本的DelegateCommand<T>,那麼T就是我們要傳入的命令參數的類型,那麼,我們現在可以把觸發命令的Button本身作為命令參數傳入

xaml代碼如下:

<Button  x:Name="mybtn"  FontSize="30"  Content="Click Me" Margin="10" Height="60" Command="{Binding GetCurrentTimeCommand}"  CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}}"/>

GetCurrentTimeCommand命令代碼改為如下:

private DelegateCommand<object> _getCurrentTimeCommand;
public DelegateCommand<object> GetCurrentTimeCommand =>
    _getCurrentTimeCommand ?? (_getCurrentTimeCommand = new DelegateCommand<object>(ExecuteGetCurrentTimeCommand).ObservesCanExecute(()=> IsCanExcute));

 void ExecuteGetCurrentTimeCommand(object parameter)
 {
    this.CurrentTime =((Button)parameter)?.Name+ DateTime.Now.ToString();
 }

我們來看看執行效果:

 

三.事件轉命令

      在我們大多數擁有Command依賴屬性的控制項,大多數是由於繼承了ICommandSource介面,ICommandSource介面擁有著三個函數成員ICommand介面類型屬性Command,object 類型屬性CommandParameter,IInputElement 類型屬性CommandTarget,而基本繼承著ICommandSource介面這兩個基礎類的就是ButtonBase和MenuItem,因此像Button,Checkbox,RadioButton等繼承自ButtonBase擁有著Command依賴屬性,而MenuItem也同理。但是我們常用的Textbox那些就沒有。

     現在我們有這種需求,我們要在這個界面基礎上新增第二個Textbox,當Textbox的文本變化時,需要將按鈕的Name和第二個Textbox的文本字元串合併更新到第一個Textbox上,我們第一直覺肯定會想到用Textbox的TextChanged事件,那麼如何將TextChanged轉為命令?

首先我們在xmal界面引入:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

該程式集 System.Windows.Interactivity dll是在 Expression Blend SDK中的,而Prism的包也也將其引入包含在內了,因此我們可以直接引入,然後我們新增第二個Textbox的代碼:

<TextBox Margin="10" FontSize="32" Text="{Binding Foo,UpdateSourceTrigger=PropertyChanged}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="TextChanged">
                    <i:InvokeCommandAction Command="{Binding TextChangedCommand}" CommandParameter="{Binding ElementName=mybtn}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </TextBox>

MainWindowViewModel新增代碼:

private string _foo;
public string Foo
{
     get { return _foo; }
     set { SetProperty(ref _foo, value); }
}

private DelegateCommand<object> _textChangedCommand;
public DelegateCommand<object> TextChangedCommand =>
  _textChangedCommand ?? (_textChangedCommand = new DelegateCommand<object>(ExecuteTextChangedCommand));

void ExecuteTextChangedCommand(object parameter)
{
  this.CurrentTime = Foo + ((Button)parameter)?.Name;
}

執行效果如下:

 

上面我們在xaml代碼就是添加了對TextBox的TextChanged事件的Blend EventTrigger的偵聽,每當觸發該事件,InvokeCommandAction就會去調用TextChangedCommand命令

將EventArgs參數傳遞給命令

     我們知道,TextChanged事件是有個RoutedEventArgs參數TextChangedEventArgs,假如我們要拿到該TextChangedEventArgs或者是RoutedEventArgs參數裡面的屬性,那麼該怎麼拿到,我們使用System.Windows.Interactivity的NameSpace下的InvokeCommandAction是不能做到的,這時候我們要用到prism自帶的InvokeCommandAction的TriggerParameterPath屬性,我們現在有個要求,我們要在第一個TextBox,顯示我們第二個TextBox輸入的字元串加上觸發該事件的控制項的名字,那麼我們可以用到其父類RoutedEventArgs的Soucre屬性,而激發該事件的控制項就是第二個TextBox

xaml代碼修改如下:

<TextBox x:Name="myTextBox" Margin="10" FontSize="32" Text="{Binding Foo,UpdateSourceTrigger=PropertyChanged}" TextChanged="TextBox_TextChanged">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="TextChanged">
                    <prism:InvokeCommandAction Command="{Binding TextChangedCommand}"  TriggerParameterPath="Source"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </TextBox>

MainWindowViewModel修改如下:

 void ExecuteTextChangedCommand(object parameter)
 {
    this.CurrentTime = Foo + ((TextBox)parameter)?.Name;
 }

實現效果:

還有一個很有趣的現象,假如上述xaml代碼將TriggerParameterPath去掉,我們其實拿到的是TextChangedEventArgs

四.實現基於Task的命令

    首先我們在界面新增一個新的按鈕,用來綁定新的基於Task的命令,我們將要做的就是點擊該按鈕後,第一個Textbox的在5秒後顯示"Hello Prism!",且期間UI界面不阻塞

xaml界面新增按鈕代碼如下:

<Button  x:Name="mybtn1"  FontSize="30"  Content="Click Me 1" Margin="10" Height="60" Command="{Binding AsyncCommand}" />

MainWindowViewModel新增代碼:

private DelegateCommand _asyncCommand;
  public DelegateCommand AsyncCommand =>
     _asyncCommand ?? (_asyncCommand = new DelegateCommand(ExecuteAsyncCommand));

  async void ExecuteAsyncCommand()
  {
     await ExampleMethodAsync();
  }

  async Task ExampleMethodAsync()
  {           
     await Task.Run(()=> 
     {
        Thread.Sleep(5000);
        this.CurrentTime = "Hello Prism!";
     } );
  }

也可以更簡潔的寫法:

 private DelegateCommand _asyncCommand;
 public DelegateCommand AsyncCommand =>
    _asyncCommand ?? (_asyncCommand = new DelegateCommand( async()=>await ExecuteAsyncCommand()));

 Task ExecuteAsyncCommand()
 {
    return Task.Run(() =>
    {
       Thread.Sleep(5000);
       this.CurrentTime = "Hello Prism!";
    });
  }

直接看效果:

 

五.創建複合命令

   prism提供CompositeCommand類支持複合命令,什麼是複合命令,我們可能有這種場景,一個主界面的不同子窗體都有其各自的業務,假如我們可以將上面的例子稍微改下,我們分為三個不同子窗體,三個分別來顯示當前年份,月日,時分秒,我們希望在主窗體提供一個按鈕,點擊後能夠使其同時顯示,這時候就有一種關係存在了,主窗體按鈕依賴於三個子窗體的按鈕,而子窗體的按鈕不依賴於主窗體的按鈕

下麵是創建和使用一個prism標準複合命令的流程:

  • 創建一個全局的複合命令

  • 通過IOC容器註冊其為單例

  • 給複合命令註冊子命令

  • 綁定複合命令

1.創建一個全局的複合命令

   首先,我們創建一個類庫項目,新增ApplicationCommands類作為全局命令類,代碼如下:

public interface IApplicationCommands
{
    CompositeCommand GetCurrentAllTimeCommand { get; }
}

public class ApplicationCommands : IApplicationCommands
{
   private CompositeCommand _getCurrentAllTimeCommand = new CompositeCommand();
   public CompositeCommand GetCurrentAllTimeCommand
   {
        get { return _getCurrentAllTimeCommand; }
   }
}

其中我們創建了IApplicationCommands介面,讓ApplicationCommands實現了該介面,目的是為了下一步通過IOC容器註冊其為全局的單例介面

2.通過IOC容器註冊其為單例

   我們創建一個新的項目作為主窗體,用來顯示子窗體和使用複合命令,關鍵部分代碼如下:

App.cs代碼:

using Prism.Unity;
using Prism.Ioc;
using System.Windows;
using CompositeCommandsSample.Views;
using Prism.Modularity;
using CompositeCommandsCore;

namespace CompositeCommandsSample
{

 public partial class App : PrismApplication
 {
     protected override Window CreateShell()
     {
         return Container.Resolve<MainWindow>();
     }

     //通過IOC容器註冊IApplicationCommands為單例
     protected override void RegisterTypes(IContainerRegistry containerRegistry)
     {
        containerRegistry.RegisterSingleton<IApplicationCommands, ApplicationCommands>();
     }

     //註冊子窗體模塊
     protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
     {
        moduleCatalog.AddModule<CommandSample.CommandSampleMoudle>();
     }
  }
}

3.給複合命令註冊子命令

     我們在之前的CommandSample解決方案下麵的Views文件夾下新增兩個UserControl,分別用來顯示月日和時分秒,在其ViewModels文件夾下麵新增兩個UserControl的ViewModel,並且將之前的MainWindow也改為UserControl,大致結構如下圖:

 

關鍵部分代碼:

GetHourTabViewModel.cs:

IApplicationCommands _applicationCommands;

public GetHourTabViewModel(IApplicationCommands applicationCommands)
{
    _applicationCommands = applicationCommands;
    //給複合命令GetCurrentAllTimeCommand註冊子命令GetHourCommand
    _applicationCommands.GetCurrentAllTimeCommand.RegisterCommand(GetHourCommand);
}

private DelegateCommand _getHourCommand;
public DelegateCommand GetHourCommand =>
   _getHourCommand ?? (_getHourCommand = new DelegateCommand(ExecuteGetHourCommand).ObservesCanExecute(() => IsCanExcute));

void ExecuteGetHourCommand()
{
   this.CurrentHour = DateTime.Now.ToString("HH:mm:ss");
}

GetMonthDayTabViewModel.cs:

 IApplicationCommands _applicationCommands;

 public GetMonthDayTabViewModel(IApplicationCommands applicationCommands)
 {
     _applicationCommands = applicationCommands;
     //給複合命令GetCurrentAllTimeCommand註冊子命令GetMonthCommand
     _applicationCommands.GetCurrentAllTimeCommand.RegisterCommand(GetMonthCommand);
 }

 private DelegateCommand _getMonthCommand;
 public DelegateCommand GetMonthCommand =>
      _getMonthCommand ?? (_getMonthCommand = new DelegateCommand(ExecuteCommandName).ObservesCanExecute(()=>IsCanExcute));

 void ExecuteCommandName()
 {
    this.CurrentMonthDay = DateTime.Now.ToString("MM:dd");
 }

MainWindowViewModel.cs:

IApplicationCommands _applicationCommands;

public MainWindowViewModel(IApplicationCommands applicationCommands)
{
    _applicationCommands = applicationCommands;
    //給複合命令GetCurrentAllTimeCommand註冊子命令GetYearCommand
    _applicationCommands.GetCurrentAllTimeCommand.RegisterCommand(GetYearCommand);       
}

private DelegateCommand _getYearCommand;
public DelegateCommand GetYearCommand =>
   _getYearCommand ?? (_getYearCommand = new DelegateCommand(ExecuteGetYearCommand).ObservesCanExecute(()=> IsCanExcute));

void ExecuteGetYearCommand()
{
   this.CurrentTime =DateTime.Now.ToString("yyyy");
}

CommandSampleMoudle.cs:

using CommandSample.ViewModels;
using CommandSample.Views;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;

namespace CommandSample
{
  public class CommandSampleMoudle : IModule
  {
    public void OnInitialized(IContainerProvider containerProvider)
    {
       var regionManager = containerProvider.Resolve<IRegionManager>();
       IRegion region= regionManager.Regions["ContentRegion"];

       var mainWindow = containerProvider.Resolve<MainWindow>();
       (mainWindow.DataContext as MainWindowViewModel).Title = "GetYearTab";
       region.Add(mainWindow);

       var getMonthTab = containerProvider.Resolve<GetMonthDayTab>();
       (getMonthTab.DataContext as GetMonthDayTabViewModel).Title = "GetMonthDayTab";
       region.Add(getMonthTab);

       var getHourTab = containerProvider.Resolve<GetHourTab>();
       (getHourTab.DataContext as GetHourTabViewModel).Title = "GetHourTab";
       region.Add(getHourTab);
    }

    public void RegisterTypes(IContainerRegistry containerRegistry)
    {
            
    }
  }
}

4.綁定複合命令

主窗體xaml代碼:

<Window x:Class="CompositeCommandsSample.Views.MainWindow"
        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:prism="http://prismlibrary.com/"
        xmlns:local="clr-namespace:CompositeCommandsSample"
        mc:Ignorable="d" prism:ViewModelLocator.AutoWireViewModel="True"
        Title="MainWindow" Height="650" Width="800">
    <Window.Resources>
        <Style TargetType="TabItem">
            <Setter Property="Header" Value="{Binding DataContext.Title}"/>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Button Content="GetCurrentTime" FontSize="30" Margin="10" Command="{Binding ApplicationCommands.GetCurrentAllTimeCommand}"/>
        <TabControl Grid.Row="1" prism:RegionManager.RegionName="ContentRegion"/>
    </Grid>
</Window>

MainWindowViewModel.cs:

using CompositeCommandsCore;
using Prism.Mvvm;

namespace CompositeCommandsSample.ViewModels
{
  public  class MainWindowViewModel:BindableBase
  {
    private IApplicationCommands _applicationCommands;
    public IApplicationCommands  ApplicationCommands
    {
       get { return _applicationCommands; }
       set { SetProperty(ref _applicationCommands, value); }
    }

    public MainWindowViewModel(IApplicationCommands applicationCommands)
    {
        this.ApplicationCommands = applicationCommands;
    }
  }
}

最後看看實際的效果如何:

 

     最後,其中複合命令也驗證我們一開始說的關係,複合命令依賴於子命令,但子命令不依賴於複合命令,因此,只有當三個子命令的都為可執行的時候才能執行複合命令,其中用到的prism模塊化的知識,我們下一篇會仔細探討

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

-Advertisement-
Play Games
更多相關文章
  • 對於串口設備經常遇到重新拔插串口設備時候,程式又需要重新選擇串口打開。對此很少麻煩的要死。 現在我們可以根據該設備的id去遍歷串口設備,一旦符合就打開此串口即可。 public void init() { string portName = ""; List<USBDeviceInfo> usbLi ...
  • static void Main(string[] args) { string[] enumNames = GetEnumNames(typeof(Seasons)); if(enumNames!=null && enumNames.Any()) { foreach(var name in enu... ...
  • 說到正則表達式,大家就會想到那像火星文一樣的正則表達式字元串。雖然看起來很奇怪,但是一個個都搞清楚那些東西分別表示什麼意思的時候,會發現這東西其實也不難。說乾就乾,我們來一個個的理解。 先弄點數據 先來個最簡單的開個味,太深了都沒有興趣往下看了 沒有任何的正則表達式的元字元(也就是保留字),http ...
  • 我是一名 ASP.NET 程式員,專註於 B/S 項目開發。累計文章閱讀量超過一千萬,我的博客主頁地址:https://www.itsvse.com/blog_xzz.html 只需要設置JsonSerializerSettings參數即可。 代碼如下: 效果圖如下: 原文地址:https://do ...
  • 今天學習了while語句,自己想了一道題,試著練習,改了幾次之後調試通過: 學生做題,如果全部都做對了,那麼會得到老師的誇獎,如果做錯了,那麼每道題要罰抄十遍: namespace _999_1錯題罰抄 { class Program { /// <summary> /// 如果做題做錯一道那麼罰抄 ...
  • 學習while語句的正確用法:題目:老師教學生,講一次之後 問學生會不會,如果不會;就再講一遍。如果會了就放學,但是如果連講了十遍還是不會,那也要放學回家namespace _44講十遍會不會 { class Program { /// <summary> /// 此語句主要是對信息錄入進行計數,如 ...
  • 操作步驟:滑鼠右擊項目(註意是項目)->添加->引用->項目(在項目列表中選擇需要引用的項目)->確定 ...
  • 1.install-package log4net 2.add new config file, its name is log4net.config <?xml version="1.0" encoding="utf-8" ?><configuration> <log4net> <root> <l ...
一周排行
    -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.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...