十年河東,十年河西,莫欺少年窮
學無止境,精益求精
1、新建項目wpfApp6,添加Nuget引用,並初始化App.xaml 及 cs 類
app.cs 如下
public partial class App : PrismApplication { protected override Window CreateShell() { return Container.Resolve<MainView>(); } protected override void RegisterTypes(IContainerRegistry containerRegistry) { containerRegistry.RegisterDialog<UserControlA, UserControlAModel>("UserControlA"); } }
app.xaml 如下:
<Prism:PrismApplication x:Class="WpfApp6.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApp6" xmlns:Prism="http://prismlibrary.com/" > <Application.Resources> </Application.Resources> </Prism:PrismApplication>
說明如下:
app.cs 中設定了啓動窗體
return Container.Resolve<MainView>();
app.cs 中注入了一個對話框,名稱爲:UserControlA ,並設定了viewModel數據上下文: UserControlAModel
containerRegistry.RegisterDialog<UserControlA, UserControlAModel>("UserControlA");
2、啓動窗體
啓動窗體必須寫在Views文件夾中,且必須以View結尾,啓動窗體的viewModel必須放在ViewModels文件夾中,且必須以Model結尾
xaml
<Window x:Class="WpfApp6.Views.MainView" 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:WpfApp6.Views" xmlns:Prism="http://prismlibrary.com/" Prism:ViewModelLocator.AutoWireViewModel="true" mc:Ignorable="d" Title="MainView" Height="450" Width="800"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="auto"/> </Grid.RowDefinitions> <StackPanel Orientation="Horizontal"> <Button Content="點擊並彈出用戶控件" Margin="5" Height="35" Width="150" Command="{Binding BtnCommand}" CommandParameter="UserControlA"></Button> </StackPanel> </Grid> </Window>
viewModel如下:
public class MainViewModel : BindableBase { /// <summary> /// 窗體Button按鈕事件,用於點擊後,彈出框 /// </summary> public DelegateCommand<string> BtnCommand { get; set; } /// <summary> /// 引入Prism 的 DialogService 服務,它可以彈出用戶控件,或者窗體 /// </summary> private readonly IDialogService dialogService; public MainViewModel(IDialogService dialogService) { BtnCommand = new DelegateCommand<string>(open); this.dialogService = dialogService; } private void open(string obj) { //彈框傳遞的參數,鍵值對 IDialogParameters parameters = new DialogParameters(); parameters.Add("schoolId", 1); parameters.Add("schoolName", "河南大學"); //彈出框 並傳遞參數、設定彈出窗體關閉時的回調函數 dialogService.ShowDialog(obj, parameters,callback); } /// <summary> /// 彈出窗體關閉時的回調函數 /// </summary> /// <param name="result"></param> private void callback(IDialogResult result) { if (result.Result == ButtonResult.OK) { var closeParam = result.Parameters.GetValue<string>("closeParam"); } } }
3、彈出的用戶控件
xaml
<UserControl x:Class="WpfApp6.UserControls.UserControlA" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WpfApp6.UserControls" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition /> <RowDefinition Height="auto"/> </Grid.RowDefinitions> <TextBlock Text="溫馨提示" FontSize="30" Grid.Row="0"/> <TextBlock Text="我是用戶控件A" FontSize="30" Grid.Row="1" TextAlignment="Center"/> <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right" Margin="5"> <Button Height="30" Width="100" Content="確定" Margin="5" Command="{Binding saveCommand}"/> <Button Height="30" Width="100" Content="取消" Margin="5" Command="{Binding cancelCommand}"/> </StackPanel> </Grid> </UserControl>
viewModel如下
using Prism.Commands; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WpfApp6.UserControlModels { /// <summary> /// 被彈出的框,用戶控件 /// </summary> public class UserControlAModel : IDialogAware { public string Title { get; set; } /// <summary> /// 關閉窗體請求事件 /// </summary> public event Action<IDialogResult> RequestClose; /// <summary> /// 保存按鈕事件 /// </summary> public DelegateCommand saveCommand { get; set; } /// <summary> /// 取消按鈕事件 /// </summary> public DelegateCommand cancelCommand { get; set; } public UserControlAModel() { this.Title = "用戶控件A"; saveCommand = new DelegateCommand(save); cancelCommand = new DelegateCommand(cancel); } /// <summary> /// 保存,關閉窗體,並傳值給主窗體 /// </summary> private void save() { OnDialogClosed(); } /// <summary> /// 取消,直接關閉窗體 /// </summary> private void cancel() { RequestClose.Invoke(new DialogResult(ButtonResult.No)); } /// <summary> /// 是否可關閉窗體,可以寫一些業務邏輯,這裏直接返回True /// </summary> /// <returns></returns> public bool CanCloseDialog() { return true; } /// <summary> /// 關閉窗體,並傳值給主窗體 /// </summary> public void OnDialogClosed() { IDialogParameters parameters = new DialogParameters(); parameters.Add("closeParam", "我是closeParam值"); RequestClose?.Invoke(new DialogResult(ButtonResult.OK, parameters)); } /// <summary> /// 窗體打開後,接收主窗體傳遞的參數,並賦值給窗體的Title屬性 /// </summary> /// <param name="parameters"></param> public void OnDialogOpened(IDialogParameters parameters) { var schoolName = parameters.GetValue<string>("schoolName"); var Id = parameters.GetValue<int>("Id"); this.Title = schoolName + "_" + Id; } } }
4、消息發佈與訂閱
新建Event文件夾並創建如下類
public class MessageEvent : PubSubEvent<string> { }
發佈消息
/// <summary> /// 窗體Button按鈕事件,用於點擊後,彈出框 /// </summary> public DelegateCommand<string> BtnCommand { get; set; } /// <summary> /// 引入Prism 的 DialogService 服務,它可以彈出用戶控件,或者窗體 /// </summary> private readonly IDialogService dialogService; /// <summary> /// /// </summary> private readonly IEventAggregator aggregator; public MainViewModel(IDialogService dialogService, IEventAggregator _aggregator) { BtnCommand = new DelegateCommand<string>(open); this.dialogService = dialogService; this.aggregator = _aggregator; } private void open(string obj) { //彈框傳遞的參數,鍵值對 IDialogParameters parameters = new DialogParameters(); parameters.Add("schoolId", 1); parameters.Add("schoolName", "河南大學"); //發佈一個消息 aggregator.GetEvent<MessageEvent>().Publish("hello 陳大六"); //彈出框 並傳遞參數、設定彈出窗體關閉時的回調函數 dialogService.ShowDialog(obj, parameters,callback); }
訂閱消息
public UserControlA(IEventAggregator aggregator) { var evn = aggregator.GetEvent<MessageEvent>().Subscribe(callback => { MessageBox.Show(callback); }); InitializeComponent(); }
@天才臥龍的博爾樂