Wpf學習片段

IRegionManager?和IContainerExtension

IRegionManager?是 Prism 框架中用于管理 UI 區域(Regions)的核心接口,它實現了模塊化應用中視圖(Views)的動態加載、導航和生命周期管理。

IContainerExtension?是依賴注入(DI)容器的抽象接口,而?Resolve<T>()?方法用于從容器中解析指定類型的實例

 
public class u1: UserControl
{IRegionManager _regionManager;IContainerExtension _container;public u1(IContainerExtension container, IRegionManager regionManager){_regionManager = regionManager;_container = container;//從容器中解析ListView類型的實例。如果ListView已注冊為單例,則返回單例實例;否則返回新實例ListView ListView11 =_container.Resolve<ListView>();//獲取中心顯示區域IRegion region= _regionManager.Regions["ContentRegion"];//為中心顯示區域添加視圖(ListView11),并為視圖分配一個名稱“ListView1”region.Add(ListView11 , "ListView1");//將指定視圖(ListView11)設置為區域(region)中的活動視圖region.Activate(ListView11);}}

u1的xaml中有:

<ContentControl Grid.Column="1" prism:RegionManager.RegionName="ContentRegion" />

......

<dx:DXTabItem Header="名稱">
? ? ? ?<local:ListView/>//將?ListView?視圖嵌入到?DXTabItem?中,作為選項卡頁的內容。

</dx:DXTabItem>

其中ListView是自定義的另一個用戶控件。

在 Prism 框架中,結合第三方控件庫(如 DevExpress 的?DXTabItem)時,可以通過 XAML 直接定義視圖(如?ListView)并將其嵌入到選項卡控件中。

region.Activate(view)?方法用于將指定視圖(view)設置為區域(IRegion)中的活動視圖。

單選框的動態綁定

? <StackPanel Margin="20">
? ? ? ? ? ? <TextBlock Text="組合單選框" FontWeight="Bold"/>
? ? ? ? ? ? <DockPanel x:Name="GroupRadioButton">
? ? ? ? ? ? ? ? <StackPanel DockPanel.Dock="Left">
? ? ? ? ? ? ? ? ? ? <ItemsControl ItemsSource="{Binding RadioButtons}">
? ? ? ? ? ? ? ? ? ? ? ? <ItemsControl.ItemTemplate>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <DataTemplate>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <RadioButton Content="{Binding SecurityId}"?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?IsChecked="{Binding IsSelected, Mode=TwoWay}"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?GroupName="RadioButtons"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Command="{Binding DataContext.RadioCheckCommand,?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?RelativeSource={RelativeSource AncestorType=Window}}"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?CommandParameter="{Binding}">
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </RadioButton>
? ? ? ? ? ? ? ? ? ? ? ? ? ? </DataTemplate>
? ? ? ? ? ? ? ? ? ? ? ? </ItemsControl.ItemTemplate>
? ? ? ? ? ? ? ? ? ? </ItemsControl>
? ? ? ? ? ? ? ? </StackPanel>

? ? ? ? ? ? ? ? <StackPanel DockPanel.Dock="Right"?
? ? ? ? ? ? ? ? ? ? Orientation="Horizontal"?
? ? ? ? ? ? ? ? ? ? HorizontalAlignment="Center"?
? ? ? ? ? ? ? ? ? ? VerticalAlignment="Center">
? ? ? ? ? ? ? ? ? ? <TextBlock Text="{Binding SelectedRadioButton.SecurityId, StringFormat='結果:{0}'}" />
? ? ? ? ? ? ? ? </StackPanel>
? ? ? ? ? ? </DockPanel>
? ? ? ? </StackPanel>

using Prism.Commands;
using Prism.Mvvm;
using StrategyClient.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;namespace StrategyClient.ViewModels
{class SellStrategyViewModel : BindableBase{/// <summary>/// 當前選擇的單選框/// </summary>private ConfigAccount _selectedRadioButton;/// <summary>/// 當前選擇的單選框/// </summary>public ConfigAccount SelectedRadioButton{get => _selectedRadioButton;set => SetProperty(ref _selectedRadioButton, value);}/// <summary>/// 需要顯示的一組單選框的信息鏈表/// </summary>public ObservableCollection<ConfigAccount> RadioButtons { get; } = new ObservableCollection<ConfigAccount>();/// <summary>/// 綁定命令觸發(單選框選擇改變時)/// </summary>public DelegateCommand<ConfigAccount> RadioCheckCommand { get; }public SellStrategyViewModel(){// 初始化單選框選項RadioButtons.Add(new ConfigAccount { SecurityId = "選項1", IsSelected = false });RadioButtons.Add(new ConfigAccount { SecurityId = "選項2", IsSelected = false });RadioButtons.Add(new ConfigAccount { SecurityId = "選項3", IsSelected = false });// 設置默認選中項if (RadioButtons.Count > 0){RadioButtons[0].IsSelected = true;SelectedRadioButton = RadioButtons[0];}// 注冊命令RadioCheckCommand = new DelegateCommand<ConfigAccount>(OnRadioChecked);}private void OnRadioChecked(ConfigAccount item){// 更新選中項foreach (var radioButton in RadioButtons){radioButton.IsSelected = radioButton == item;}SelectedRadioButton = item;}}
}

檢查UI綁定路徑

xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"

......

<CheckBox IsChecked="{Binding IsSelectedV, Mode=TwoWay, diagnostics:PresentationTraceSources.TraceLevel=High}"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>

如果直接綁定到?IsSelectedV?屬性不起作用,可以嘗試使用?CellValue?綁定:

<CheckBox IsChecked="{Binding RowData.Row.IsSelectedV, Mode=TwoWay}"/>

在這種情況下,RowData.Row?通常是指當前行的數據對象。

實現彈窗功能:

?//App中注冊對話框
containerRegistry.RegisterDialog<View, ViewModel>();

// 顯示彈窗
void ShowDialog(string name, IDialogParameters parameters, Action<IDialogResult> callback);
?
// 顯示模態彈窗(阻塞式)
void Show(string name, IDialogParameters parameters, Action<IDialogResult> callback);

?主視圖的ViewModel:

public ObservableCollection<string> Items
{
? ? ? ? ? ? get;
? ? ? ? ? ? set;
?} = new ObservableCollection<string>();

? ? ? ?/// <summary>
? ? ? ? /// 按鈕按下彈窗
? ? ? ? /// </summary>
? ? ? ? private void Button_Click()
? ? ? ? {
? ? ? ? ? ? //傳遞參數
? ? ? ? ? ? var parameters = new DialogParameters
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? { "DataList", Items }
? ? ? ? ? ? ? ? };

????????????????//View:視圖名,parameters:要傳遞的參數

? ? ? ? ? ? _dialogService.ShowDialog("View", parameters, (IDialogResult result) =>
? ? ? ? ? ? {//彈窗關閉后回調函數
? ? ? ? ? ? ? ? // 從結果中獲取數據鏈表
? ? ? ? ? ? ? ? if (result.Parameters.TryGetValue<ObservableCollection<string>>("DataList", out var dataList))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? Items = dataList;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? });

? ? ? ? }

?// 定義彈窗事件
public class ShowDialogEvent : PubSubEvent<DialogParameters> { }

// 發布彈窗請求
var parameters = new DialogParameters { { "message", "保存成功!" } };
_eventAggregator.GetEvent<ShowDialogEvent>().Publish(parameters);
?
// 彈窗服務訂閱事件并顯示彈窗
_eventAggregator.GetEvent<ShowDialogEvent>()
? ? .Subscribe(ShowDialog, ThreadOption.UIThread);
private void ShowDialog(DialogParameters parameters)
{
? ? _dialogService.ShowDialog("MessageDialog", parameters);
}

View視圖的ViewModel

class ViewModel : BindableBase, IDialogAware

{

public ObservableCollection<string> Items
{
? ? ? ? ? ?get => _items;
? ? ? ? ? ? set => SetProperty(ref _items, value);
?}

?/// <summary>
? ? ? ? /// 對話框事件,傳遞對話框的結果
? ? ? ? /// </summary>
? ? ? ? public event Action<IDialogResult> RequestClose;
? ? ? ? /// <summary>
? ? ? ? /// 關閉對話框時傳遞參數
? ? ? ? /// </summary>
? ? ? ? public event Action<IDialogParameters> RequestClosed;

? // 對話框標題
? ? ? ? public string Title => "彈窗標題";

? /// <summary>
? ? ? ? /// 允許關閉對話框
? ? ? ? /// </summary>
? ? ? ? /// <returns></returns>
? ? ? ? public bool CanCloseDialog()
? ? ? ? {
? ? ? ? ? ? return true;
? ? ? ? }

? ? ? ? /// <summary>
? ? ? ? /// 關閉對話框時
? ? ? ? /// </summary>
? ? ? ? public void OnDialogClosed()
? ? ? ? {
? ? ? ? ? ? var resultParameters = new DialogParameters
? ? ? ? ? ? {
? ? ? ? ? ? ? ? { "DataList", Items }
? ? ? ? ? ? };
? ? ? ? ? ? // 觸發請求關閉事件
? ? ? ? ? ? RequestClosed?.Invoke(resultParameters);
? ? ? ? }

? ? ? ? /// <summary>
? ? ? ? /// 打開對話框時
? ? ? ? /// </summary>
? ? ? ? /// <param name="parameters"></param>
? ? ? ? public void OnDialogOpened(IDialogParameters parameters)
? ? ? ? {
? ? ? ? ? ? if (parameters.TryGetValue<ObservableCollection<SellStrategyModel>>("DataList", out var initialName))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Items = initialName;
? ? ? ? ? ? }
? ? ? ? }

}

單選框:

<DockPanel x:Name="GroupRadioButton">
? ? ? ? ? ? ? ? ? ? <ItemsControl ItemsSource="{Binding RadioButtons}">
? ? ? ? ? ? ? ? ? ? ? ? <ItemsControl.ItemsPanel>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <ItemsPanelTemplate>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <WrapPanel Orientation="Horizontal"/>
? ? ? ? ? ? ? ? ? ? ? ? ? ? </ItemsPanelTemplate>
? ? ? ? ? ? ? ? ? ? ? ? </ItemsControl.ItemsPanel>
? ? ? ? ? ? ? ? ? ? ? ? <ItemsControl.ItemTemplate>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <DataTemplate>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <RadioButton Content="{Binding Id}"?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?IsChecked="{Binding IsSelected, Mode=TwoWay}"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?GroupName="RadioButtons"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Command="{Binding DataContext.RadioCheckCommand,?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?RelativeSource={RelativeSource AncestorType=Window}}"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?CommandParameter="{Binding}">
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </RadioButton>
? ? ? ? ? ? ? ? ? ? ? ? ? ? </DataTemplate>
? ? ? ? ? ? ? ? ? ? ? ? </ItemsControl.ItemTemplate>
? ? ? ? ? ? ? ? ? ? </ItemsControl>
? ? ? ? ? ? ? ? </DockPanel>

viewModel

?/// <summary>
? ? ? ? /// 當前選擇的單選框
? ? ? ? /// </summary>
? ? ? ? private ConfigAccount _selectedRadioButton;
? ? ? ? /// <summary>
? ? ? ? /// 當前選擇的單選框
? ? ? ? /// </summary>
? ? ? ? public ConfigAccount SelectedRadioButton
? ? ? ? {
? ? ? ? ? ? get => _selectedRadioButton;
? ? ? ? ? ? set => SetProperty(ref _selectedRadioButton, value);
? ? ? ? }

?/// <summary>
? ? ? ? /// 需要顯示的一組單選框的信息鏈表
? ? ? ? /// </summary>
? ? ? ? public ObservableCollection<ConfigAccount> RadioButtons { get; } = new ObservableCollection<ConfigAccount>();
? ? ? ? /// <summary>
? ? ? ? /// 綁定命令觸發(單選框選擇改變時)
? ? ? ? /// </summary>
? ? ? ? public DelegateCommand<ConfigAccount> RadioCheckCommand { get; }

?????????public ViewModel()
? ? ? ? {
? ? ? ? ? ? AddSecurityStrategy();
? ? ? ? ? ? // 設置默認選中項
? ? ? ? ? ? if (RadioButtons.Count > 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? RadioButtons[0].IsSelected = true;
? ? ? ? ? ? ? ? SelectedRadioButton = RadioButtons[0];
? ? ? ? ? ? }
? ? ? ? ? ? RadioCheckCommand = new DelegateCommand<ConfigAccount>(OnRadioChecked);
? ? ? ? }

?/// <summary>
? ? ? ? /// 單選框按鈕選擇時觸發
? ? ? ? /// </summary>
? ? ? ? /// <param name="item">選擇的單選框對象</param>
? ? ? ? private void OnRadioChecked(ConfigAccount item)
? ? ? ? {
? ? ? ? ? ? // 更新選中項
? ? ? ? ? ? //foreach (var radioButton in RadioButtons)
? ? ? ? ? ? //{
? ? ? ? ? ? // ? ?radioButton.IsSelected = radioButton == item;
? ? ? ? ? ? //}
? ? ? ? ? ? SelectedRadioButton = item;
? ? ? ? }

? ? ? ? /// <summary>
? ? ? ? /// 添加需要顯示的單選框按鈕
? ? ? ? /// </summary>
? ? ? ? private void AddSecurityStrategy()
? ? ? ? {
? ? ? ? ? ? string[] addStrategys = System.Configuration.ConfigurationManager.AppSettings["SellStrategy"].ToString().Split('|');
? ? ? ? ? ? foreach (string addStrategy in addStrategys)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? RadioButtons.Add(new ConfigAccount() { IsSelected = false, SecurityId = addStrategy });
? ? ? ? ? ? }
? ? ? ? }
?

動態加載不同模塊的 UI

方法一:

1.注冊區域
確保?"MainContent"?區域已通過?IRegionManager?注冊(通常在 XAML 中聲明或代碼中動態添加):

_regionManager.Regions.Add("MainContent", new Region());

或者

<!-- 或在 XAML 中聲明區域 --> <ContentControl prism:RegionManager.RegionName="MainContent" />

2.App.xaml.cs

?//MainView:要加載的視圖的名稱,MainView1:起的名字

containerRegistry.RegisterForNavigation<MainWindowView, MainWindowViewModel>("MainView1");

//MainView:要加載的視圖的名稱,沒起名字

registry.RegisterForNavigation<MainWindowView, MainWindowViewModel>();

3.點擊按鈕時實現ViewModel:

//起名字了這里傳入起的名字

_regionManager.RequestNavigate("MainContent", "MainView1");

//沒起名字,這里傳入類名

_regionManager.RequestNavigate("MainContent", "MainWindowView");

//傳遞參數:

var parameters = new NavigationParameters();

parameters.Add("orderId", 123);

_regionManager.RequestNavigate("MainRegion", "OrderDetailView", parameters);

方法二:

1.注冊區域
確保?"MainContent"?區域已通過?IRegionManager?注冊(通常在 XAML 中聲明或代碼中動態添加):

_regionManager.Regions.Add("MainContent", new Region());

或者

<!-- 或在 XAML 中聲明區域 --> <ContentControl prism:RegionManager.RegionName="MainContent" />

2.在模塊初始化時注冊視圖到指定區域:

void MainWindow_Loaded(object sender, RoutedEventArgs e)
? ? ? ? {//開始的時候MainContent會顯示loading視圖
? ? ? ? ? ? _regionManager.RegisterViewWithRegion("MainContent", typeof(loading));
? ? ? ? }

3.點擊按鈕時實現ViewModel:

? ?_regionManager.RequestNavigate("MainContent", "loading");

獲取UI界面實例的view Model,調用對應的函數

方法一:

? // 獲取目標區域
IRegion contentRegion = _regionManager.Regions["MainContent"];
// 獲取當前激活的視圖
object activeView = contentRegion.ActiveViews.FirstOrDefault();
?// 獲取視圖的 DataContext(即 ViewModel)
var viewModel = activeView.GetType().GetProperty("DataContext")?.GetValue(activeView) as DownLoadViewModel;
?//調用視圖方法改變UI內文字
viewModel?.testc();

方法二:

//注冊為單例

registry.RegisterSingleton<DownLoadViewModel>();

// 直接解析 ViewModel 實例
var viewMode2 = _container.Resolve<DownLoadViewModel>();
viewMode2?.testc();

注冊服務

方法用途生命周期控制
RegisterForNavigation<TView>()注冊視圖到導航系統,自動綁定 ViewModel(通過命名約定)默認瞬態(每次導航創建新實例)
RegisterSingleton<T>()注冊類型為單例,全局唯一實例單例
RegisterForNavigation<TView, TViewModel>()顯式指定視圖和 ViewModel 的綁定關系可自定義(結合其他注冊方法)
Register<TInterface, TImplementation>()注冊接口到具體實現默認瞬態
RegisterInstance<T>(T instance)注冊已創建的實例為單例單例

//舉例

registry.RegisterForNavigation<DownLoadView, DownLoadViewModel>("DownLoad1");
registry.RegisterForNavigation<MainWindowView, MainWindowViewModel>();
?

registry.RegisterSingleton<DownLoadViewModel>();
?

registry.RegisterForNavigation<DownLoadView>("downLoad");
registry.RegisterForNavigation<loading>();

containerRegistry.Register<IDownLoadService, DownLoadService>();


var downloadService = new DownLoadService(); containerRegistry.RegisterInstance<IDownLoadService>(downloadService);

管理區域和視圖:

方法用途特點
RegisterViewWithRegion靜態注冊視圖到區域,自動加載適用于固定視圖,初始化時自動加載
RequestNavigate動態導航到視圖,支持參數和回調適用于按需加載的視圖,支持導航邏輯
IRegion.Add?/?IRegion.Remove手動添加或移除視圖適用于精細控制視圖的顯示/隱藏
IRegion.Activate?/?IRegion.Deactivate激活或停用視圖適用于切換視圖的可見性
IRegion.NavigationService通過導航服務實現復雜導航邏輯適用于需要自定義導航流程的場景

//舉例

protected override void OnInitialized(IContainerProvider containerProvider)

{

var regionManager = containerProvider.Resolve<IRegionManager>();

// 將 OrderListView 注冊到 MainRegion,視圖會在區域初始化時自動加載 regionManager.RegisterViewWithRegion("MainRegion", typeof(OrderListView));

}
?

_regionManager.RequestNavigate("MainContent", "loading");


var region = _regionManager.Regions["MainRegion"];

var view = _container.Resolve<OrderListView>();

region.Add(view); region.Activate(view); // 激活視圖


var region = _regionManager.Regions["MainRegion"];

var view = region.Views.FirstOrDefault(v => v is OrderListView); if (view != null)

{ region.Remove(view); }


var region = _regionManager.Regions["MainRegion"];

var view = region.Views.FirstOrDefault(v => v is OrderListView); if (view != null)

{

region.Activate(view); // 激活視圖

// region.Deactivate(view); // 停用視圖

}

INavigationAware:

INavigationAware?是 Prism 框架中用于處理導航生命周期事件的核心接口。它允許 ViewModel 在導航過程中響應以下三個關鍵事件:

  • 導航到當前視圖時(OnNavigatedTo
  • 從當前視圖導航離開時(OnNavigatedFrom
  • 導航確認階段(IsNavigationTarget,用于決定是否復用現有 ViewModel 實例)

通過實現?INavigationAware,可以精細控制 ViewModel 在導航過程中的行為,例如初始化數據、清理資源或決定是否復用實例。

  • INavigationAware?是 Prism 中管理導航生命周期的核心接口。
  • 通過實現?IsNavigationTargetOnNavigatedTo?和?OnNavigatedFrom,可以精細控制 ViewModel 在導航過程中的行為。
  • 適用于需要初始化數據、清理資源或決定是否復用實例的場景。
  • 合理使用導航參數(NavigationParameters)可以傳遞上下文數據。

區域上下文?

是一種用于在區域(IRegion)和其宿主控件(如?ContentControlItemsControl?等)之間傳遞上下文數據的機制。它允許開發者在區域中共享數據,而無需直接依賴視圖或視圖模型,從而提升代碼的解耦性和靈活性。

// 獲取區域并設置上下文 var region = _regionManager.Regions["MainRegion"]; region.Context = new?AppContext?{ UserId = "123", Role = "Admin" };//AppContext:自定義Mode

App.config用法

App.config代碼:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
? ? <startup>?
? ? ? ? <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
? ? </startup>
??
? <appSettings>
? ? <add key="keyName" value="valueName"/>
? ? <add key="keyName2" value="valueName2"/>
? ? <add key="keyName3" value="valueName3"/>
? </appSettings>
</configuration>

??string settingValue = ?? ??? ???System.Configuration.ConfigurationManager.AppSettings["keyName"];

TemplateBinding 和Binding 的區別

  • TemplateBinding
    • 用途:專門用于控件模板(如ControlTemplateDataTemplate)中,用于將模板中的屬性綁定到模板化控件(即應用該模板的控件)的對應屬性。
    • 上下文:只能在控件模板內部使用,用于模板和模板化控件之間的屬性綁定。
  • Binding
    • 用途:是一種通用的數據綁定機制,用于在XAML中建立任何兩個屬性之間的綁定關系,無論是控件與控件之間、控件與數據源之間,還是其他任何對象屬性之間的綁定。
    • 上下文:可以在XAML的任何地方使用,不局限于控件模板。

<ControlTemplate TargetType="Button">
? ? <Border Background="{TemplateBinding Background}"?
? ? ? ? ? ? BorderBrush="{TemplateBinding BorderBrush}"?
? ? ? ? ? ? BorderThickness="{TemplateBinding BorderThickness}">
? ? ? ? <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
? ? </Border>
</ControlTemplate>

<TextBox Text="{Binding UserName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

ControlTemplate

ControlTemplate?是 WPF 和 UWP 等 XAML 框架中的核心概念,用于定義控件的視覺結構和交互行為。它允許開發者自定義控件的外觀,而不必修改控件的底層邏輯。

一個典型的?ControlTemplate?包含以下關鍵部分:

  • 觸發器(Triggers):用于響應控件狀態變化(如鼠標懸停、按下等),動態修改控件的外觀。
  • 內容呈現器(ContentPresenter):用于顯示控件的內容(如按鈕的文本或圖像)。
  • 控件部件(Parts):模板中定義的命名元素,控件邏輯可以通過?TemplateBinding?或?GetTemplateChild?方法訪問這些部件。
  • 綁定(Bindings):通過?TemplateBinding?將模板中的屬性綁定到模板化控件的屬性,實現動態數據驅動。

? <Style x:Key="CardButtonStyle" TargetType="Button">

?<Setter Property="Template">
? ? ? ? ? ? ? ? <Setter.Value>

<ControlTemplate TargetType="Button">
? ? ? ? ? ? ? ? <Border x:Name="border"
? ? ? ? ? ? ? ? ? ? ? ? Background="{TemplateBinding Background}"
? ? ? ? ? ? ? ? ? ? ? ? BorderBrush="{TemplateBinding BorderBrush}"
? ? ? ? ? ? ? ? ? ? ? ? BorderThickness="{TemplateBinding BorderThickness}"
? ? ? ? ? ? ? ? ? ? ? ? CornerRadius="10"
? ? ? ? ? ? ? ? ? ? ? ? Padding="5">//定義了按鈕的外觀,包括背景色、邊框、圓角和內邊距
? ? ? ? ? ? ? ? ? ? <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>//用于顯示按鈕的內容
? ? ? ? ? ? ? ? </Border>
? ? ? ? ? ? ? ? <ControlTemplate.Triggers>

//當鼠標懸停在按鈕上時(IsMouseOver=True),將?Border?的背景色設置為?LightBlue

? ? ? ? ? ? ? ? ? ? <Trigger Property="IsMouseOver" Value="True">
? ? ? ? ? ? ? ? ? ? ? ? <Setter TargetName="border" Property="Background" Value="LightBlue"/>
? ? ? ? ? ? ? ? ? ? </Trigger>

//當按鈕被按下時(IsPressed=True),將?Border?的背景色設置為?DarkBlue
? ? ? ? ? ? ? ? ? ? <Trigger Property="IsPressed" Value="True">
? ? ? ? ? ? ? ? ? ? ? ? <Setter TargetName="border" Property="Background" Value="DarkBlue"/>
? ? ? ? ? ? ? ? ? ? </Trigger>
? ? ? ? ? ? ? ? </ControlTemplate.Triggers>
? ? ? ? ? ? </ControlTemplate>

</Setter.Value>
? ? ? ? ? ? </Setter>

</Style>

或者將ControlTemplate定義在在控件中:

<Button Content="按鈕控件"? Width="300" Height="80" Margin="5" >
? ? <Button.Template>
? ? ? ? <ControlTemplate TargetType="Button">
? ? ? ? ? ? <Border Background="Transparent" CornerRadius="5" BorderThickness="1" BorderBrush="#C9CCD5">
? ? ? ? ? ? ? ? <ContentPresenter ?HorizontalAlignment="Center" VerticalAlignment="Center"/>
? ? ? ? ? ? </Border>
? ? ? ? </ControlTemplate>
? ? </Button.Template>
</Button>

查看控件的默認模板:

在設計界面中用鼠標單擊Button按鈕右鍵-編輯模板-編輯副本。

會自動出現:

觸發器

<ControlTemplate.Triggers>
????????????????<Trigger Property="IsMouseOver" Value="True">
????????????????????<Setter Property="Content" Value="MouseOver" TargetName="contentPresenter"/>
????????????????</Trigger>
????????????????<Trigger Property="IsMouseOver" Value="False">
????????????????????<Setter Property="Content" Value="將ControlTemplate定義在在控件中" TargetName="contentPresenter"/>
????????????????</Trigger>
????????????</ControlTemplate.Triggers>

在Triggers集合中增加了兩個Trigger 對象,條件是當鼠標移上去或鼠標移開的時候,更改了Button的Content屬性。

ControlTemplate 與 DataTemplate 的區別

  • ControlTemplate
    • 用于定義控件的外觀和交互行為。
    • 適用于自定義控件或修改現有控件的外觀。
  • DataTemplate
    • 用于定義數據的可視化方式。
    • 適用于將數據綁定到控件(如?ListBoxItemComboBoxItem)并自定義其顯示方式。

ContentPresenter?

ContentPresenter?是 WPF 和 UWP 等 XAML 框架中的一個核心控件,用于在控件模板(如?ControlTemplate?或?DataTemplate)中動態呈現內容。它的主要作用是承載并顯示被模板化的控件的內容,同時支持內容與模板的綁定和動態更新。

核心作用

  • 內容占位符:在控件模板中作為內容的占位符,確保內容能夠正確顯示。
  • 支持內容綁定:自動綁定到被模板化控件的?Content?或?ContentTemplate?屬性,實現動態內容展示。
  • 繼承樣式和屬性:自動繼承模板中定義的樣式(如?ForegroundFontFamily)和布局屬性(如?HorizontalAlignmentVerticalAlignment)。
  • 觸發器支持:與樣式觸發器(Triggers)配合,響應內容狀態變化(如?IsMouseOverIsEnabled)并動態調整外觀。

關鍵屬性

  • HorizontalAlignment="Center"?和?VerticalAlignment="Center":確保內容在按鈕中居中顯示。
  • RecognizesAccessKey="True":支持訪問鍵(如?&Save?中的?&?符號)

<Button Content="按鈕控件"? Width="300" Height="80" Margin="5" >
? ? <Button.Template>
? ? ? ? <ControlTemplate TargetType="Button">
? ? ? ? ? ? <Border Background="Transparent" CornerRadius="5" BorderThickness="1" BorderBrush="#C9CCD5">
? ? ? ? ? ? ? ? <ContentPresenter ?HorizontalAlignment="Center" VerticalAlignment="Center"/>
? ? ? ? ? ? </Border>
? ? ? ? </ControlTemplate>
? ? </Button.Template>
</Button>

常見問題

  • 內容不顯示
    • 檢查是否在模板中正確放置了?ContentPresenter
    • 確保模板化控件的?Content?或?ContentTemplate?已正確設置。
  • 樣式不繼承
    • 確保?ContentPresenter?的屬性(如?Foreground)未被模板中的其他控件覆蓋。
    • 使用?TemplateBinding?綁定樣式屬性。

DataTemplate?

DataTemplate?是 WPF 和 UWP 等 XAML 框架中用于定義數據可視化方式的模板。它允許開發者指定如何將數據對象(如業務模型、集合項等)呈現為 UI 元素(如文本、圖像、按鈕等)。通過?DataTemplate,開發者可以將數據與 UI 分離,實現數據的靈活展示和復用。

一個典型的?DataTemplate?包含以下關鍵部分:

  • 綁定(Bindings):用于將數據對象的屬性綁定到 UI 元素的屬性。
  • 控件組合:通過組合多個 XAML 控件(如?TextBlockImageButton)來定義數據的可視化結構。
  • 觸發器(Triggers):可選,用于響應數據狀態變化,動態修改 UI 元素的外觀。
  • 數據類型指定:通過?DataType?屬性指定模板適用的數據類型。

虛擬化:對于大型數據集合,啟用虛擬化(如?VirtualizingStackPanel)可以顯著提高性能。

<ListBox ItemsSource="{Binding People}" VirtualizingStackPanel.IsVirtualizing="True"> <!-- 模板內容 --> </ListBox>

<!-- 定義 Person 類 -->
<Window.Resources>
? ? <x:Type x:Key="PersonType" Type="local:Person"/>
? ??
? ? <!-- 定義 DataTemplate? ?使用?DataType?屬性指定模板適用于?Person?類型。-->
? ? <DataTemplate DataType="{x:Type local:Person}">
? ? ? ? <StackPanel Orientation="Horizontal" Margin="5">
? ? ? ? ? ? <TextBlock Text="{Binding Name}" FontWeight="Bold" Margin="0,0,10,0"/>
? ? ? ? ? ? <TextBlock Text="{Binding Age, StringFormat=Age: {0}}"/>
? ? ? ? </StackPanel>
? ? </DataTemplate>
</Window.Resources>
?
<!-- 使用 DataTemplate 的 ListBox -->
<ListBox ItemsSource="{Binding People}">
? ? <!-- 無需顯式指定 ItemTemplate,因為已經定義了隱式 DataTemplate -->

  • 由于?DataTemplate?定義了?DataType,當?ListBox?的?ItemsSource?包含?Person?對象時,XAML 會自動應用該模板,無需顯式指定?ItemTemplate

</ListBox>

如果需要為特定控件或場景顯式指定?DataTemplate,可以使用?ItemTemplateContentTemplate?等屬性

<ListBox ItemsSource="{Binding People}">
? ? <ListBox.ItemTemplate>
? ? ? ? <DataTemplate>
? ? ? ? ? ? <!-- 模板內容 -->
? ? ? ? </DataTemplate>
? ? </ListBox.ItemTemplate>
</ListBox>

數據觸發器(DataTriggers)

<DataTemplate DataType="{x:Type local:Person}">
? ? <StackPanel Orientation="Horizontal">
? ? ? ? <TextBlock Text="{Binding Name}"/>
? ? ? ? <TextBlock Text="{Binding Age}">
? ? ? ? ? ? <TextBlock.Style>
? ? ? ? ? ? ? ? <Style TargetType="TextBlock">
? ? ? ? ? ? ? ? ? ? <Style.Triggers>
? ? ? ? ? ? ? ? ? ? ? ? <DataTrigger Binding="{Binding Age}" Value="18">
? ? ? ? ? ? ? ? ? ? ? ? ? ? <Setter Property="Foreground" Value="Red"/>
? ? ? ? ? ? ? ? ? ? ? ? </DataTrigger>
? ? ? ? ? ? ? ? ? ? </Style.Triggers>
? ? ? ? ? ? ? ? </Style>
? ? ? ? ? ? </TextBlock.Style>
? ? ? ? </TextBlock>
? ? </StackPanel>
</DataTemplate>

ItemsPanelTemplate

ItemsPanelTemplate?是 WPF 和 UWP 等 XAML 框架中的一個關鍵組件,用于定義?集合控件(如?ListBoxListViewComboBoxItemsControl?等)中項目的布局容器。它允許開發者自定義項目的排列方式,

核心作用

  • 自定義布局容器:決定集合控件中項目的容器類型(如?StackPanelWrapPanelGrid?等)。
  • 動態調整布局:根據業務需求靈活切換布局方式,無需修改數據綁定邏輯。
  • 支持復雜布局:實現如網格、虛擬化滾動等高級布局需求。

關鍵特性

  • 布局容器定義:指定?ItemsControl?的?ItemsPanel?屬性所使用的模板。
  • 與?ItemTemplate?分離ItemsPanelTemplate?負責布局容器,ItemTemplate?負責單個項目的外觀。
  • 支持虛擬化:與?VirtualizingStackPanel?配合,優化大數據量時的性能。

<ItemsControl.ItemsPanel>
? ? <ItemsPanelTemplate>
? ? ? ? <WrapPanel/>
? ? </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>

<ListBox.ItemContainerStyle>
? ? <Style TargetType="ListBoxItem">
? ? ? ? <Setter Property="Template">
? ? ? ? ? ? <Setter.Value>
? ? ? ? ? ? ? ? <ControlTemplate TargetType="ListBoxItem">
? ? ? ? ? ? ? ? ? ? <ContentPresenter/>
? ? ? ? ? ? ? ? </ControlTemplate>
? ? ? ? ? ? </Setter.Value>
? ? ? ? </Setter>
? ? </Style>
</ListBox.ItemContainerStyle>

?<!-- 使用 ItemsPanelTemplate 將 ListBox 的項目布局為網格 -->
? ? ? ? <ListBox Width="400" Height="300">
? ? ? ? ? ? <ListBox.ItemsPanel>
? ? ? ? ? ? ? ? <ItemsPanelTemplate>
? ? ? ? ? ? ? ? ? ? <!-- 使用 UniformGrid 作為布局容器,自動排列為網格 -->
? ? ? ? ? ? ? ? ? ? <UniformGrid Columns="3" Rows="2"/>
? ? ? ? ? ? ? ? </ItemsPanelTemplate>
? ? ? ? ? ? </ListBox.ItemsPanel>
? ? ? ? ? ? <ListBox.ItemTemplate>
? ? ? ? ? ? ? ? <DataTemplate>
? ? ? ? ? ? ? ? ? ? <Border BorderBrush="LightGray" BorderThickness="1" Margin="5" Padding="10">
? ? ? ? ? ? ? ? ? ? ? ? <TextBlock Text="{Binding}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
? ? ? ? ? ? ? ? ? ? </Border>
? ? ? ? ? ? ? ? </DataTemplate>
? ? ? ? ? ? </ListBox.ItemTemplate>
? ? ? ? ? ? <!-- 示例數據 -->
? ? ? ? ? ? <sys:String xmlns:sys="clr-namespace:System;assembly=mscorlib">Item 1</sys:String>
? ? ? ? ? ? <sys:String xmlns:sys="clr-namespace:System;assembly=mscorlib">Item 2</sys:String>
? ? ? ? </ListBox>

  1. ItemsPanelTemplate?的定義
    • 使用?<UniformGrid Columns="3" Rows="2"/>?作為布局容器,將項目排列為 3 列 2 行的網格。
    • UniformGrid?會自動平均分配空間給每個項目。
  2. ItemTemplate?的定義
    • 使用?DataTemplate?定義每個項目的外觀(如邊框、文本對齊方式)。

ItemTemplate?與?ItemsPanelTemplate?的區別

  • ItemTemplate
    • 定義單個項目的視覺外觀(如文本、圖像、按鈕)。
    • 影響項目的樣式和內容。
  • ItemsPanelTemplate
    • 定義項目的布局容器(如?StackPanelGrid)。
    • 影響項目的排列方式。
維度ItemTemplateItemsPanelTemplate
職責定義單個項目的視覺外觀定義所有項目的布局容器
作用范圍單個項目所有項目
典型內容TextBlockImageButton?等StackPanelWrapPanelGrid?等
數據綁定通過?{Binding}?綁定到數據屬性不涉及數據綁定
性能影響輕量級,通常不影響性能布局復雜度可能影響性能

<ListBox ItemsSource="{Binding Users}">
? ? <ListBox.ItemsPanel>
? ? ? ? <ItemsPanelTemplate>
? ? ? ? ? ? <WrapPanel Orientation="Horizontal"/>
? ? ? ? </ItemsPanelTemplate>
? ? </ListBox.ItemsPanel>
</ListBox>

Setter?

<Style TargetType="Button">
? ? <Setter Property="Foreground" Value="Red"/>
? ? <Setter Property="Background" Value="LightGray"/>
? ? <Setter Property="BorderThickness" Value="1"/>
? ? <Setter Property="Padding" Value="10,5"/>
? ? <Setter Property="Template">
? ? ? ? <Setter.Value>
? ? ? ? ? ? <ControlTemplate TargetType="Button">
? ? ? ? ? ? ? ? <Border Background="{TemplateBinding Background}"
? ? ? ? ? ? ? ? ? ? ? ? BorderBrush="Black"
? ? ? ? ? ? ? ? ? ? ? ? BorderThickness="{TemplateBinding BorderThickness}"
? ? ? ? ? ? ? ? ? ? ? ? CornerRadius="5">
? ? ? ? ? ? ? ? ? ? <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
? ? ? ? ? ? ? ? </Border>
? ? ? ? ? ? </ControlTemplate>
? ? ? ? </Setter.Value>
? ? </Setter>
</Style>

  1. Style?定義
    • TargetType="Button":表示該樣式適用于所有?Button?控件。
  2. Setter?元素
    • <Setter Property="Foreground" Value="Red"/>:將按鈕的文本顏色設置為紅色。
    • <Setter Property="Background" Value="LightGray"/>:將按鈕的背景色設置為淺灰色。
    • <Setter Property="BorderThickness" Value="1"/>:設置按鈕的邊框厚度為 1。
    • <Setter Property="Padding" Value="10,5"/>:設置按鈕的內邊距為 10(水平)和 5(垂直)。
  3. ControlTemplate
    • 自定義按鈕的視覺結構,使用?Border?和?ContentPresenter?實現圓角按鈕。

綁定表達式

  • Value?可以是一個綁定表達式,動態設置屬性值。

<Setter Property="Foreground" Value="{Binding TextColor}"/>

條件設置

  • 結合?Trigger?或?DataTrigger,根據條件動態修改屬性值。

<Style.Triggers>
? ? <Trigger Property="IsMouseOver" Value="True">
? ? ? ? <Setter Property="Foreground" Value="DarkRed"/>
? ? </Trigger>
</Style.Triggers>

作用域

Setter?只在定義的?Style?或?ControlTemplate?范圍內生效。

優先級

如果多個?Setter?嘗試設置同一屬性,后定義的?Setter?會覆蓋先前的值。

Style?

Style?的作用

  • 統一外觀:通過?Style,可以為控件定義統一的外觀(如背景色、字體、邊框等)。
  • 復用性:命名的樣式(通過?x:Key?定義)可以在多個控件中重復使用。
  • 動態修改:可以通過?Trigger?或?DataTrigger?動態修改樣式。

x:Key?屬性

  • 命名樣式x:Key?為樣式指定一個唯一的名稱,使其可以在 XAML 中被引用。
  • 復用方式:通過?StaticResource?或?DynamicResource?引用命名樣式。

<Window.Resources>
? ? ? ? <!-- 定義 CardButtonStyle 樣式 -->
? ? ? ? <Style x:Key="CardButtonStyle" TargetType="Button">
? ? ? ? ? ? <Setter Property="Background" Value="White"/>
? ? ? ? ? ? <Setter Property="Template">
? ? ? ? ? ? ? ? <Setter.Value>
? ? ? ? ? ? ? ? ? ? <ControlTemplate TargetType="Button">
? ? ? ? ? ? ? ? ? ? ? ? <Border Background="{TemplateBinding Background}"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? BorderBrush="{TemplateBinding BorderBrush}"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? BorderThickness="{TemplateBinding BorderThickness}"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CornerRadius="5"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Padding="{TemplateBinding Padding}">
? ? ? ? ? ? ? ? ? ? ? ? ? ? <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
? ? ? ? ? ? ? ? ? ? ? ? </Border>
? ? ? ? ? ? ? ? ? ? </ControlTemplate>
? ? ? ? ? ? ? ? </Setter.Value>
? ? ? ? ? ? </Setter>
? ? ? ? ? ? <Style.Triggers>
? ? ? ? ? ? ? ? <!-- 鼠標懸停時的效果 -->
? ? ? ? ? ? ? ? <Trigger Property="IsMouseOver" Value="True">
? ? ? ? ? ? ? ? ? ? <Setter Property="Background" Value="LightBlue"/>
? ? ? ? ? ? ? ? ? ? <Setter Property="Foreground" Value="DarkBlue"/>
? ? ? ? ? ? ? ? </Trigger>
? ? ? ? ? ? ? ? <!-- 按鈕按下時的效果 -->
? ? ? ? ? ? ? ? <Trigger Property="IsPressed" Value="True">
? ? ? ? ? ? ? ? ? ? <Setter Property="Background" Value="DarkBlue"/>
? ? ? ? ? ? ? ? ? ? <Setter Property="Foreground" Value="White"/>
? ? ? ? ? ? ? ? </Trigger>
? ? ? ? ? ? </Style.Triggers>
? ? ? ? </Style>
? ? </Window.Resources>
?
? ? <Grid>
? ? ? ? <!-- 使用 CardButtonStyle 樣式的按鈕 -->
? ? ? ? <Button Content="Card Button 1" Style="{StaticResource CardButtonStyle}" Width="120" Height="60"/>
? ? ? ? <Button Content="Card Button 2" Style="{StaticResource CardButtonStyle}" Width="120" Height="60" HorizontalAlignment="Right"/>
? ? </Grid>

通過?Style="{StaticResource CardButtonStyle}"?將樣式應用于按鈕控件。

動態資源

如果需要動態切換樣式,可以使用?DynamicResource

<Button Content="Dynamic Style" Style="{DynamicResource CardButtonStyle}"/>

全局樣式

如果不指定?x:Key,而是指定?TargetType,樣式將應用于所有指定類型的控件:

<Style TargetType="Button">
? ? <!-- 樣式定義 -->
</Style>

Trigger觸發器

Trigger?是 WPF 和 UWP 等 XAML 框架中的核心機制,用于動態改變控件的樣式或行為,而無需編寫代碼。通過?Trigger,開發者可以根據控件的屬性值、事件或數據狀態自動調整控件的外觀(如顏色、大小、可見性等)或觸發動畫。

核心作用

  • 狀態響應:根據控件的狀態(如鼠標懸停、選中、禁用)自動改變樣式。
  • 數據驅動:根據數據綁定屬性的值動態調整 UI。
  • 減少代碼:通過 XAML 聲明式地實現交互邏輯,避免后臺代碼。

關鍵特性

  • 條件判斷:通過?Condition?定義觸發條件。
  • Setter 集合:通過?Setter?定義觸發時的樣式更改。
  • 嵌套支持Trigger?可以嵌套在?StyleControlTemplate?或?DataTemplate?中。
Trigger 類型作用適用場景
PropertyTrigger根據控件的屬性值變化觸發樣式更改。例如:按鈕在鼠標懸停時改變背景色。
EventTrigger根據控件的事件(如?LoadedClick)觸發動畫或樣式更改。例如:頁面加載時播放動畫。
DataTrigger根據數據綁定屬性的值觸發樣式更改。例如:根據數據狀態(如?IsSelected)改變項目樣式。
MultiTrigger根據多個條件的組合觸發樣式更改。例如:同時滿足?IsMouseOver?和?IsEnabled?時改變樣式。
MultiDataTrigger根據多個數據綁定屬性的組合觸發樣式更改。例如:根據多個數據狀態(如?IsAdmin?和?IsLoggedIn)改變界面。

<Button Content="Hover Me" Width="100" Height="30">
? ? <Button.Style>
? ? ? ? <Style TargetType="Button">
? ? ? ? ? ? <Setter Property="Background" Value="LightGray"/>
? ? ? ? ? ? <Style.Triggers>
? ? ? ? ? ? ? ? <!-- 鼠標懸停時改變背景色 -->
? ? ? ? ? ? ? ? <Trigger Property="IsMouseOver" Value="True">
? ? ? ? ? ? ? ? ? ? <Setter Property="Background" Value="LightBlue"/>
? ? ? ? ? ? ? ? ? ? <Setter Property="Foreground" Value="White"/>
? ? ? ? ? ? ? ? </Trigger>
? ? ? ? ? ? </Style.Triggers>
? ? ? ? </Style>
? ? </Button.Style>
</Button>

DataTrigger(根據數據狀態改變樣式)

<ListBox ItemsSource="{Binding Users}">
? ? <ListBox.ItemTemplate>
? ? ? ? <DataTemplate>
? ? ? ? ? ? <Border BorderBrush="LightGray" BorderThickness="1" Margin="5" Padding="10">
? ? ? ? ? ? ? ? <Border.Style>
? ? ? ? ? ? ? ? ? ? <Style TargetType="Border">
? ? ? ? ? ? ? ? ? ? ? ? <Setter Property="Background" Value="White"/>
? ? ? ? ? ? ? ? ? ? ? ? <Style.Triggers>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <!-- 如果 IsSelected 為 True,則改變背景色 -->
? ? ? ? ? ? ? ? ? ? ? ? ? ? <DataTrigger Binding="{Binding IsSelected}" Value="True">
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <Setter Property="Background" Value="LightBlue"/>
? ? ? ? ? ? ? ? ? ? ? ? ? ? </DataTrigger>
? ? ? ? ? ? ? ? ? ? ? ? </Style.Triggers>
? ? ? ? ? ? ? ? ? ? </Style>
? ? ? ? ? ? ? ? </Border.Style>
? ? ? ? ? ? ? ? <TextBlock Text="{Binding Name}"/>
? ? ? ? ? ? </Border>
? ? ? ? </DataTemplate>
? ? </ListBox.ItemTemplate>
</ListBox>

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/905365.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/905365.shtml
英文地址,請注明出處:http://en.pswp.cn/news/905365.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

消息~組件(群聊類型)ConcurrentHashMap發送

為什么選擇ConcurrentHashMap&#xff1f; 在開發聊天應用時&#xff0c;我們需要存儲和管理大量的聊天消息數據&#xff0c;這些數據會被多個線程頻繁訪問和修改。比如&#xff0c;當多個用戶同時發送消息時&#xff0c;服務端需要同時處理這些消息的存儲和查詢。如果用普通的…

Stapi知識框架

一、Stapi 基礎認知 1. 框架定位 自動化API開發框架&#xff1a;專注于快速生成RESTful API 約定優于配置&#xff1a;通過標準化約定減少樣板代碼 企業級應用支持&#xff1a;適合構建中大型API服務 代碼生成導向&#xff1a;顯著提升開發效率 2. 核心特性 自動CRUD端點…

基于深度學習的水果識別系統設計

一、選擇YOLOv5s模型 YOLOv5&#xff1a;YOLOv5 是一個輕量級的目標檢測模型&#xff0c;它在 YOLOv4 的基礎上進行了進一步優化&#xff0c;使其在保持較高檢測精度的同時&#xff0c;具有更快的推理速度。YOLOv5 的網絡結構更加靈活&#xff0c;可以根據不同的需求選擇不同大…

Spring Security與SaToken的對比

Spring Security與SaToken的詳細對照與優缺點分析 1. 核心功能與設計理念 對比維度Spring SecuritySaToken核心定位企業級安全框架&#xff0c;深度集成Spring生態&#xff0c;提供全面的安全解決方案&#xff08;認證、授權、攻擊防護等&#xff09;輕量級權限認證框架&#…

【docker】--鏡像管理

文章目錄 拉取鏡像啟動鏡像為容器連接容器法一法二 保存鏡像加載鏡像鏡像打標簽移除鏡像 拉取鏡像 docker pull mysql:8.0.42啟動鏡像為容器 docker run -dp 8080:8080 --name container_mysql8.0.42 -e MYSQL_ROOT_PASSWORD123123123 mysql:8.0.42 連接容器 法一 docker e…

力扣HOT100之二叉樹:543. 二叉樹的直徑

這道題本來想到可以用遞歸做&#xff0c;但是還是沒想明白&#xff0c;最后還是去看靈神題解了&#xff0c;感覺這道題最大的收獲就是鞏固了我對lambda表達式的掌握。 按照靈神的思路&#xff0c;直徑可以理解為從一個葉子出發向上&#xff0c;在某個節點處拐彎&#xff0c;然后…

web 自動化之 yaml 數據/日志/截圖

文章目錄 一、yaml 數據獲取二、日志獲取三、截圖 一、yaml 數據獲取 需要安裝 PyYAML 庫 import yaml import os from TestPOM.common import dir_config as Dirdef read_yaml(key,file_name"test_datas.yaml"):file_path os.path.join(Dir.testcases_dir, file_…

rtty操作記錄說明

rtty操作記錄說明 前言 整理資料發現了幾年前做的操作記錄&#xff0c;分享出來&#xff0c;希望對大家有用。 rtty-master&#xff1a;rtty客戶端程序&#xff0c;其中buffer\log\ssl為源碼的子目錄&#xff0c;從git上下載https://github.com/zhaojh329&#xff0c; rtty…

mybatis中${}和#{}的區別

先測試&#xff0c;再說結論 userService.selectStudentByClssIds(10000, "wzh or 11");List<StudentEntity> selectStudentByClssIds(Param("stuId") int stuId, Param("field") String field);<select id"selectStudentByClssI…

【運維】MacOS藍牙故障排查與修復指南

在日常使用macOS系統過程中&#xff0c;藍牙連接問題時有發生。無論是無法連接設備、連接不穩定還是藍牙功能完全失效&#xff0c;這些問題都會嚴重影響我們的工作效率。本文將分享一些實用的排查方法和修復技巧&#xff0c;幫助你解決macOS系統上的藍牙故障。 問題癥狀 常見…

數據結構(一) 緒論

一. 時間復雜度: (1)定義: 時間復雜度是衡量算法執行時間隨輸入規模(通常用n表示)增長的變化趨勢的指標,時間復雜度用O符號表示 用于描述算法在最壞情況下或平均情況下的時間需求 時間復雜度關注的是操作次數的增長率&#xff0c;而非具體執行時間 常見的時間復雜度由小到大依次…

網絡協議與系統架構分析實戰:工具與方法全解

網絡協議與系統架構分析實戰&#xff1a;工具與方法全解 在互聯網系統的開發、運維與安全分析中&#xff0c;協議解析與抓包分析是不可或缺的核心技能。本文將系統梳理主流協議解析工具、協議自動識別方案&#xff0c;并結合實際抓包案例&#xff0c;講解如何還原和推測底層系…

發那科機器人4(編程實例)

發那科機器人4(編程實例) 一、編程實例1、直線運動實例2、圓弧運動實例3、曲線運動實例4、物料搬運實例5、異步輸送帶檢測一、編程實例 1、直線運動實例 本節內容:直線運動實例 本次實例,采用的是基礎模塊,以基礎模塊當中的四邊形為例,演示一下機器人的直線運動。 編程…

agent初識

AI Agent 時代已來&#xff1a;不止于聊天的智能體&#xff0c;將如何重塑我們的世界&#xff1f; AI Agent 時代已來&#xff1a;不止于聊天的智能體&#xff0c;將如何重塑我們的世界&#xff1f; 你是否曾驚嘆于 ChatGPT 的對答如流&#xff1f;或者 Midjourney 的妙筆生花…

.Net HttpClient 使用Json數據

HttpClient 使用Json數據 現代Web項目中&#xff0c;Json是最常用的數據格式。不論是前后端的交互中&#xff0c;還是純前端項目中&#xff0c;都是如此。因此&#xff0c;.Net HttpClient 能不能更加方便、快捷的處理Json格式數據&#xff0c;也就至關重要了&#xff01; 文末…

UDP--DDR--SFP,FPGA實現之指令監測模塊實現

指令監測模塊實現介紹 如下圖所示&#xff0c;為指令監測模塊的運行框圖 將指令設置為8bytes數據&#xff0c;故需要一個64位寄存器進行緩存&#xff0c;在進行數據緩存時&#xff0c;數據不可以輸出至下一級模塊&#xff0c;故對數據和有效指示信號也應該進行相應延遲&#…

JavaScript雙問號操作符(??)詳解,解決使用 || 時因類型轉換帶來的問題

目錄 JavaScript雙問號操作符&#xff08;??&#xff09;詳解&#xff0c;解決使用||時因類型轉換帶來的問題 一、雙問號操作符??的基礎用法 1、傳統方式的痛點 2、雙問號操作符??的精確判斷 3、雙問號操作符??與邏輯或操作符||的對比 二、復雜場景下的空值處理 …

智能體的典型應用:自動駕駛、智能客服、智能制造、游戲AI與數字人技術

本文為《React Agent&#xff1a;從零開始構建 AI 智能體》專欄系列文章。 專欄地址&#xff1a;https://blog.csdn.net/suiyingy/category_12933485.html。項目地址&#xff1a;https://gitee.com/fgai/react-agent&#xff08;含完整代碼示?例與實戰源&#xff09;。完整介紹…

Ubuntu 22.04(WSL2)使用Docker安裝Redis

Ubuntu 22.04&#xff08;WSL2&#xff09;使用Docker安裝Redis 本教程將指導您在運行于WSL2的Ubuntu 22.04上通過Docker安裝Redis 7.4.3。您將獲得一個配置了自定義設置、持久化存儲和安全選項的Redis實例。 前提條件 WSL2上已安裝Ubuntu 22.04。WSL2上已安裝并運行Docker&…

淺談 Redis 數據類型

淺談 Redis 數據類型 &#xff08;一&#xff09;String 類型 Redis 的 String 類型 是二進制安全的&#xff0c;可以用來存儲 文本字符串、int 類型數據和 bitmap 位圖 等數據。 1. 字符串操作 適用于存儲 文本、JSON、序列化數據 等任意二進制安全的內容 命令作用示例SET設…