当前位置: 首页 > news >正文

prism

添加包:

App.xaml:

<prism:PrismApplication x:Class="WpfApp2.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp2"
             xmlns:prism="http://prismlibrary.com/" 
             >
    <Application.Resources>
         
    </Application.Resources>
</prism:PrismApplication>
 

using Prism.Ioc;
using Prism.Unity;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp2
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App :PrismApplication
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();// 返回主窗口
        }
        protected override void RegisterTypes(IContainerRegistry registry)
        {
            // 注册服务
        }

    }
}

 class MainWindowViewMode :BindableBase
    {
        public MainWindowViewMode()
        {

        }

   }

页面跳转:

定义视图和视图模型
  • DownLoadView.xaml

xml

<UserControl x:Class="PrismApp.DownLoadView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Height="300" Width="300"><Grid Background="LightBlue"><TextBlock Text="这是下载界面" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="20"/></Grid>
</UserControl>

  • DownLoadView.xaml.cs

csharp

using System.Windows.Controls;namespace PrismApp
{public partial class DownLoadView : UserControl{public DownLoadView(){InitializeComponent();}}
}
配置主窗口
  • MainWindow.xaml

xml

<Window x:Class="PrismApp.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:prism="http://prismlibrary.com/"Title="MainWindow" Height="450" Width="800"><Grid><Button Content="跳转到下载界面" HorizontalAlignment="Left" Margin="20,20,0,0" VerticalAlignment="Top" Width="150" Command="{Binding NavigateCommand}"/><ContentControl prism:RegionManager.RegionName="MainContent" HorizontalAlignment="Left" Height="300" Margin="20,60,0,0" VerticalAlignment="Top" Width="300"/></Grid>
</Window>

  • MainWindowViewModel.cs

csharp

using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;namespace PrismApp
{public class MainWindowViewModel : BindableBase{private readonly IRegionManager _regionManager;private DownLoadView _downLoadView;public DelegateCommand NavigateCommand { get; private set; }public MainWindowViewModel(IRegionManager regionManager){_regionManager = regionManager;_downLoadView = new DownLoadView();NavigateCommand = new DelegateCommand(NavigateToDownloadView);}private void NavigateToDownloadView(){var mainContentRegion = _regionManager.Regions["MainContent"];if (!mainContentRegion.Views.Contains(_downLoadView)){mainContentRegion.Add(_downLoadView);}mainContentRegion.Activate(_downLoadView);}}
}
配置 Prism 应用程序
  • App.xaml.cs

csharp

using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
using Prism.Unity;
using System.Windows;namespace PrismApp
{public partial class App : PrismApplication{protected override Window CreateShell(){return Container.Resolve<MainWindow>();}protected override void RegisterTypes(IContainerRegistry containerRegistry){containerRegistry.RegisterForNavigation<DownLoadView>();}protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog){base.ConfigureModuleCatalog(moduleCatalog);}}
}


http://www.mrgr.cn/news/99187.html

相关文章:

  • 25.4.20学习总结
  • 【2025面试常问Java八股】AQS介绍(AbstractQueuedSynchronizer 抽象队列同步器)
  • Winform发展历程
  • 【前端】跟着maxkb学习logicflow流程图画法
  • UWP特性分析
  • 第七节:React HooksReact 18+新特性-并发模式(Concurrent Mode)解决了什么问题?
  • Golang 的 GMP 协程模型详解
  • 32-工艺品商城小程序
  • 33-公交车司机管理系统
  • 嵌入式硬件常用总线接口知识体系总结和对比
  • (二)mac中Grafana监控Linux上的MySQL(Mysqld_exporter)
  • 线性代数 | 知识点整理 Ref 1
  • Redis 事件循环(Event Loop)
  • RV1126网络环境TFTPNFS搭建(四)
  • 系统与网络安全------弹性交换网络(1)
  • 大数据应用开发——大数据平台集群部署(三)
  • 用Python玩转倒排索引:从原理到实战的趣味之旅
  • Semaphore的核心机制
  • Webview+Python:用HTML打造跨平台桌面应用的创新方案
  • 云梦数据平台