首页 > 其他分享 >Prism导航

Prism导航

时间:2023-12-07 17:27:05浏览次数:26  
标签:regionManager void Prism View 导航 public navigationContext 页面

注册导航页面

注册区域

使用p:RegionManager.RegionName注册页面区域

<Window x:Class="Zhaoxi.PrismRegion.Navigation.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:local="clr-namespace:Zhaoxi.PrismRegion.Navigation"
        xmlns:p="http://prismlibrary.com/"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <DockPanel>
        <Grid Width="220" DockPanel.Dock="Left">
            <StackPanel>
                <Button Content="View A" Margin="0,3"
                        Command="{Binding OpenViewCommand}"
                        CommandParameter="ViewA"/>
                <Button Content="View B" Margin="0,3"
                        Command="{Binding OpenViewCommand}"
                        CommandParameter="ViewB"/>
                <Button Content="View C" Margin="0,3"/>
            </StackPanel>
        </Grid>
        <Grid>
            <!--<ContentControl p:RegionManager.RegionName="ViewRegion"/>-->
            <TabControl p:RegionManager.RegionName="ViewRegion">
                <TabControl.ItemContainerStyle>
                    <Style TargetType="TabItem">
                        <!--TabItem的绑定数据源是页面对象-->
                        <!--TabItem的DataContext=View对象-->
                        <!--View对象的DataContext=对应的ViewModel-->
                        <Setter Property="Header" Value="{Binding DataContext.Title}"/>
                    </Style>
                </TabControl.ItemContainerStyle>
            </TabControl>
        </Grid>
    </DockPanel>
</Window>

注册界面

注册两个UserControl用于测试

<UserControl x:Class="Zhaoxi.PrismRegion.Navigation.Views.Pages.ViewA"
             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:Zhaoxi.PrismRegion.Navigation.Views.Pages"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <StackPanel>
        <TextBox Text="View A" Foreground="Orange" FontSize="20"/>
        <TextBlock Text="{Binding Title}" FontSize="20" Foreground="Red"/>
        <Button Content="Close" Command="{Binding CloseTabCommand}"/>
    </StackPanel>
</UserControl>
<UserControl x:Class="Zhaoxi.PrismRegion.Navigation.Views.Pages.ViewB"
             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:Zhaoxi.PrismRegion.Navigation.Views.Pages"
             mc:Ignorable="d"
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <TextBlock Text="View B" Foreground="Green" FontSize="20"/>

    </Grid>
</UserControl>

RegisterTypes方法中注册界面

    public class Startup : PrismBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            // 注册需要导航的子页面,只有注册了才能处理
            containerRegistry.RegisterForNavigation<ViewA>();
            containerRegistry.RegisterForNavigation<ViewB>();
        }
    }

 界面关联区域

使用IRegionManager提供的RegisterViewWithRegion方法将界面名称关联到"ViewRegion"区域

    public class MainWindowViewModel
    {
        public ICommand OpenViewCommand { get; set; }

        // 区域管理,需要拿到RegionManager
        IRegionManager _regionManager;
        public MainWindowViewModel(IRegionManager regionManager)
        {
            _regionManager = regionManager;

            OpenViewCommand = new DelegateCommand<string>(DoOpenView);
        }

        private void DoOpenView(string viewName)
        {
            _regionManager.RegisterViewWithRegion("ViewRegion", viewName);
        }
    }

 运行效果如下

导航页面传参——INavigationAware接口

 页面对应的ViewAViewModel继承INavigationAware接口并实现方法

public class ViewAViewModel : INavigationAware
    {
        public string Title { get; set; } = "View A";
        public ICommand CloseTabCommand { get; set; }

        IRegionManager _regionManager;
        public ViewAViewModel(IRegionManager regionManager)
        {
            _regionManager = regionManager;
            CloseTabCommand = new DelegateCommand(DoCloseTab);
        }
        private void DoCloseTab()
        {

        }
        #region INavigationAware接口方法
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            // 是否允许重复导航进来
            // 主-》A-》B-》A (显示前对象)  返回True
            // 主-》A-》B-》A(新对象)     返回false
            return true;

            // 编辑页面,通过主页的子项点击,打开这个页面,
            // 子项有很多,可能同时打开多个子项进行编辑
        }
        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            // 从当前View导航出去的时候触发
            // 从某个页面跳转到另一个页面的时候,可以把这个信息带过去
            navigationContext.Parameters.Add("abcd", "Hello ViewA");
        }
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            // 打开当前View的时候触发
            string arg = navigationContext.Parameters.GetValue<string>("abcd");
        }
        #endregion
    }

IsNavigationTarget

当IsNavigationTarget方法返回false时每次导航都会新建

 

OnNavigatedTo

在MainWindowViewModel中创建NavigationParameters对象,使用RequestNavigate在关联区域的同时传入NavigationParameters对象

public class MainWindowViewModel
    {
        public ICommand OpenViewCommand { get; set; }

        // 区域管理,需要拿到RegionManager
        IRegionManager _regionManager;
        public MainWindowViewModel(IRegionManager regionManager)
        {
            _regionManager = regionManager;

            OpenViewCommand = new DelegateCommand<string>(DoOpenView);
        }

        private void DoOpenView(string viewName)
        {
            //_regionManager.RegisterViewWithRegion("ViewRegion", viewName);
            // 打开/显示某个View的时候,希望给个参数它?
            // 上面的语句无法传参
            // 需要请求的View,先注册到容器中
            //_regionManager.RequestNavigate("ViewRegion", viewName);

            // 向某个View中传递特定参数,参数对接到View的ViewModel里
            if (viewName == "ViewA")
            {
                NavigationParameters parameters = new NavigationParameters();
                parameters.Add("abcd", "Hello");
                _regionManager.RequestNavigate("ViewRegion", viewName, parameters);
            }
            else if (viewName == "ViewB")
                _regionManager.RequestNavigate("ViewRegion", viewName);
        }
    }

 

OnNavigatedFrom

从当前View导航出去的时候触发,跳转到另一个页面的时候,可以把这个信息传递到下一个页面

第一次打开ViewB时无参数传进

打开ViewA,再次点击ViewB时参数从ViewA的OnNavigatedFrom传进ViewB的OnNavigatedTo

自动销毁——IRegionMemberLifetime接口

页面的ViewModel继承IRegionMemberLifetime接口

接口中的KeepAlive参数默认为true:非激活状态,在Region中保留

public class ViewAViewModel : INavigationAware,IRegionMemberLifetime
    {
        public string Title { get; set; } = "View A";
        public ICommand CloseTabCommand { get; set; }

        IRegionManager _regionManager;
        public ViewAViewModel(IRegionManager regionManager)
        {
            _regionManager = regionManager;
            CloseTabCommand = new DelegateCommand(DoCloseTab);
        }
        private void DoCloseTab()
        {

        }
        
        #region IRegionMemberLifetime接口方法
        // 用来控制当前页面非激活状态,是否在Region中保留
        public bool KeepAlive => true;

        #endregion

        #region INavigationAware接口方法
        
        #region INavigationAware接口方法
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            // 是否允许重复导航进来
            // 主-》A-》B-》A (显示前对象)  返回True
            // 主-》A-》B-》A(新对象)     返回false
            return true;

            // 编辑页面,通过主页的子项点击,打开这个页面,
            // 子项有很多,可能同时打开多个子项进行编辑
        }
        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            // 从当前View导航出去的时候触发
            // 从某个页面跳转到另一个页面的时候,可以把这个信息带过去
            navigationContext.Parameters.Add("abcd", "Hello ViewA");
        }
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            // 打开当前View的时候触发
            string arg = navigationContext.Parameters.GetValue<string>("abcd");
        }
        #endregion
    }

将KeepAlive参数改为false

public bool KeepAlive => false;

运行后点击ViewB,再点击ViewA

再次点击ViewB后,ViewA被自动销毁

页面离开前的事件——IConfirmNavigationRequest接口

页面ViewModel实现IConfirmNavigationRequest接口

public class ViewAViewModel : INavigationAware,IRegionMemberLifetime,IConfirmNavigationRequest
    {
        public string Title { get; set; } = "View A";
        public ICommand CloseTabCommand { get; set; }

        IRegionManager _regionManager;
        public ViewAViewModel(IRegionManager regionManager)
        {
            _regionManager = regionManager;
            CloseTabCommand = new DelegateCommand(DoCloseTab);
        }
        private void DoCloseTab()
        {

        }
        
        #region IRegionMemberLifetime接口方法
        // 用来控制当前页面非激活状态,是否在Region中保留
        public bool KeepAlive => true;

        #endregion

        #region INavigationAware接口方法
        
        #region INavigationAware接口方法
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            // 是否允许重复导航进来
            // 主-》A-》B-》A (显示前对象)  返回True
            // 主-》A-》B-》A(新对象)     返回false
            return true;

            // 编辑页面,通过主页的子项点击,打开这个页面,
            // 子项有很多,可能同时打开多个子项进行编辑
        }
        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            // 从当前View导航出去的时候触发
            // 从某个页面跳转到另一个页面的时候,可以把这个信息带过去
            navigationContext.Parameters.Add("abcd", "Hello ViewA");
        }
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            // 打开当前View的时候触发
            string arg = navigationContext.Parameters.GetValue<string>("abcd");
        }
        #endregion
        
        #region IConfirmNavigationRequest接口方法
        // 当从当前页面跳转到另一个页面时触发
        // OnNavigatedFrom调用前执行
        public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
        {
            // 打开某个页面,
            if (MessageBox.Show("是否离开当前页面?", "导航提示", MessageBoxButton.YesNo) ==
                MessageBoxResult.Yes)
            {
                /// 继续打开
                /// 
                continuationCallback?.Invoke(true);
            }
            else
                // 不被导航
                continuationCallback?.Invoke(false);
        }
        #endregion
    }

运行后点击ViewA,再点击ViewB的时候会弹窗提醒

 

标签:regionManager,void,Prism,View,导航,public,navigationContext,页面
From: https://www.cnblogs.com/ZHIZRL/p/17883434.html

相关文章

  • uniapp---wap2app去掉系统自带的导航栏
    在用uniapp进行将wap站转化为app的时候,默认打包后的文件,带有系统的导航栏,下面是去除的办法:第一步:找到sitemap.json 设置titleNView为false: 第二步:在pages加入{"webviewId":"common","matchUrls":[{"hostname":"R:.*","pa......
  • Prism弹窗
    创建弹窗创建弹窗内容创建一个弹出窗口的内容:一般是UserControl(不是Window)<UserControlx:Class="Zhaoxi.PrismDialog.UCDetail"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.c......
  • 固定导航栏
    废话不多说,先看效果再上代码一、效果图二、html内容我这里用来外部样式表导入css,当然你可以根据自己的喜好<!DOCTYPEhtml><html> <head> <metacharset="utf-8"/> <title>导航栏</title><!--导入外部样式表--> <linkrel="stylesheet"h......
  • 技术分享丨 Prisma Cloud 增强云原生代码保护能力!
    现如今,企业业务向云计算转变已是主流,组织收到威胁、运营中断、威胁形势也持续升级,网络安全转型已变成当今企业的当务之急。Palo Alto PrismaCloud具有业界最广泛的安全性和合规性覆盖范围,它保护跨平台的云原生应用程序、数据、网络、计算、存储、用户和更高级别的PaaS+SaaS服务......
  • Prism使用Options选项
    Options是微软提供的选项模块,该模块依赖于容器使用。除了微软的IServiceCollection,当然也可以使用其它的依赖注入容器。本文演示如何在prism中使用Options。创建应用项目创建一个Avalonia应用(或其它类型应用),然后使用NuGet包管理器添加Prism.DryIoc.Avalonia包。创建Views和ViewM......
  • 界面控件DevExpress WPF导航组件,助力升级应用程序用户体验!(上)
    DevExpressWPF的SideNavigation(侧边导航)、TreeView、导航面板组件能帮助开发者在WPF项目中添加Windows样式的资源管理器栏或OutlookNavBar(导航栏),DevExpressWPFNavBar和Accordion控件包含了许多开发人员友好的功能,专门设计用于帮助用户构建极佳的应用功能。P.S:DevExpressWPF......
  • Android 9.0 app全屏通过系统属性控制手势上滑是否显示虚拟导航栏和状态栏
    1.前言在9.0的系统rom产品定制化os开发中,在系统设置app的全屏后,默认会隐藏导航栏和状态栏,页面全屏显示的时候,然后底部上滑会显示虚拟状态栏和导航栏显示几秒钟后会自动消失,由于项目开发需要要求通过api来控制全屏时上滑是否显示虚拟导航栏和状态栏,这就要从上滑事件分析看如何显......
  • wpf学习 Prism 使用入门
    一、手动添加安装包Prism.DryIocapp.xaml.cs修改继承基类为:PrismApplication实现其中的抽象成员:CreateShell用于指定启动的窗口类1publicpartialclassApp:PrismApplication2{3protectedoverrideWindowCreateShell()4{5......
  • Wpf Prism 导航(参数传递,路由守卫,路由记录)
    十年河东,十年河西,莫欺少年穷学无止境,精益求精1、新建项目wpfApp5,添加Nuget引用,并初始化App.xaml及cs类 app.xaml如下:<Prism:PrismApplicationx:Class="WpfApp5.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="......
  • 直播平台源代码,实现一个简单的带tabs选项卡切换的首页导航功能
    直播平台源代码,实现一个简单的带tabs选项卡切换的首页导航功能 package.json: { "name":"angular-router", "version":"0.0.0", "scripts":{  "ng":"ng",  "start":"ngserve",  "bui......