C#-Wpf-Prism.DryIoc-【3】導航日誌
阿新 • • 發佈:2022-04-20
【1】先完成基本的環境搭建,可參考C#-WPF-Prism.DryIoc-【1】環境搭建 - 輕吟淺唱,驀然花開 - 部落格園 (cnblogs.com)
【2】建立基本專案結構如下圖,其中MainView是窗體(Window),View1、View2和View3是使用者控制元件(UserControl)
【3】編輯App.xaml.cs 將View1、View2和View3註冊為導航
using Prism.DryIoc; using Prism.Ioc; using System.Windows; using Wpf_Prism.DryIoc_NavigationJournal.Views;namespace Wpf_Prism.DryIoc_NavigationJournal { /// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App { protected override Window CreateShell() { return Container.Resolve<MainView>(); } protectedoverride void RegisterTypes(IContainerRegistry containerRegistry) { containerRegistry.RegisterForNavigation<View1>(); containerRegistry.RegisterForNavigation<View2>(); containerRegistry.RegisterForNavigation<View3>(); } } }
【4】編輯MainView.xaml,新增5個按鈕並繫結命令和引數(用來切換3個頁面)和一個ContentControl(用來承載三個頁面)程式碼如下:
<Window x:Class="Wpf_Prism.DryIoc_NavigationJournal.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:Wpf_Prism.DryIoc_NavigationJournal.Views" xmlns:prism="http://prismlibrary.com/" prism:ViewModelLocator.AutoWireViewModel="True" mc:Ignorable="d" Title="MainView" Height="450" Width="800"> <DockPanel> <UniformGrid Columns="5" DockPanel.Dock="Top"> <Button Content="開啟View1" Command="{Binding OpenViewCommand}" CommandParameter="View1"/> <Button Content="開啟View2" Command="{Binding OpenViewCommand}" CommandParameter="View2"/> <Button Content="開啟View3" Command="{Binding OpenViewCommand}" CommandParameter="View3"/> <Button Content="上一頁" Command="{Binding LastCommand}"/> <Button Content="下一頁" Command="{Binding NextCommand}"/> </UniformGrid> <!--#region 用來承載 View1 View2 View3 這三個不同的頁面--> <ContentControl prism:RegionManager.RegionName="MainContentRegion"/> </DockPanel> </Window>
【5】編輯MainViewModel.cs 分別配置相應的命令 以及導航的實現,程式碼如下
using Prism.Commands; using Prism.Regions; namespace Wpf_Prism.DryIoc_NavigationJournal.ViewModels { public class MainViewModel { private readonly IRegionManager regionManager; private IRegionNavigationJournal regionNavigationJournal; /// <summary> /// 開啟頁面命令 /// </summary> public DelegateCommand<string> OpenViewCommand { get;private set; } public DelegateCommand LastCommand { get;private set; } public DelegateCommand NextCommand { get;private set; } public MainViewModel(IRegionManager regionManager) { this.regionManager = regionManager; //註冊三個命令 OpenViewCommand = new DelegateCommand<string>(OpenView); LastCommand = new DelegateCommand(Last); NextCommand = new DelegateCommand(Next); } private void Last() { if (regionNavigationJournal.CanGoBack) regionNavigationJournal.GoBack(); } private void Next() { if (regionNavigationJournal.CanGoForward) regionNavigationJournal.GoForward(); } private void OpenView(string obj) { regionManager.Regions["MainContentRegion"].RequestNavigate(obj, callback => { if (callback.Result != null) regionNavigationJournal = callback.Context.NavigationService.Journal; }); } } }
【6】啟動專案,效果如下
完成,謝謝!