wpf prism Helloworld

本文详细介绍了一种基于WPF的模块化应用程序开发方法。通过创建一个简单的“Hello World”模块,展示了如何从新建项目开始,逐步实现模块注册与加载的过程。文章涵盖了App.xaml及CS文件的配置、Shell窗口的设计、用户自定义控件的建立、模块类的编写以及Bootstrapper类的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 新建wpf项目 ,修改App.xaml

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

2 修改App.xaml.cs

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

namespace MyHelloWorld
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            Bootstrapper bootstrapper = new Bootstrapper();
            bootstrapper.Run();
        }
    }
}

3 新建shell.xaml

<Window x:Class="HelloWorld.Shell"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cal="http://www.codeplex.com/prism"
    Title="Hello World" Height="300" Width="300">
    <ItemsControl Name="MainRegion" cal:RegionManager.RegionName="MainRegion" />
</Window>

4新建wpf库项目,在其中新建View目录新建用户自定义控件

 

<UserControl x:Class="HelloWorldModule.Views.HelloWorldView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <TextBlock Text="Hello World" Foreground="Green" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Calibri" FontSize="24" FontWeight="Bold"></TextBlock>
    </Grid>
</UserControl>

5 新建Module类

using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;

namespace HelloWorldModule
{
    public class HelloWorldModule : IModule
    {
        private readonly IRegionViewRegistry regionViewRegistry;

        public HelloWorldModule(IRegionViewRegistry registry)
        {
            this.regionViewRegistry = registry;   
        }

        public void Initialize()
        {
            regionViewRegistry.RegisterViewWithRegion("MainRegion", typeof(Views.HelloWorldView));
        }
    }
}

 

6 在主项目中新建Bootstrapper类

using System.Windows;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.UnityExtensions;

namespace HelloWorld
{
    class Bootstrapper : UnityBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            return this.Container.Resolve<Shell>();
        }

        protected override void InitializeShell()
        {
            base.InitializeShell();

            App.Current.MainWindow = (Window)this.Shell;
            App.Current.MainWindow.Show();
        }     

        protected override void ConfigureModuleCatalog()
        {
            base.ConfigureModuleCatalog();

            ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;
            moduleCatalog.AddModule(typeof(HelloWorldModule.HelloWorldModule));
        }

        
    }
}

WPF Prism框架是一种用于构建灵活、可维护和可测试的Xamarin.Forms、WPF和UWP应用程序的框架。它基于MVVM(Model-View-ViewModel)模式,并提供了模块化、依赖注入和事件聚合等功能[^2]。Prism帮助开发者实现应用程序的松耦合设计,从而提升代码的可重用性和可维护性。 ### Prism框架的核心概念 1. **MVVM模式**:Prism框架广泛支持MVVM模式,通过分离UI逻辑和业务逻辑,使得UI设计和开发可以并行进行,同时也便于单元测试。 2. **模块化(Modularity)**:Prism允许将应用程序划分为多个模块,每个模块可以独立开发、测试和部署,最终通过一个统一的容器进行集成。 3. **依赖注入(Dependency Injection)**:Prism内置了对依赖注入的支持,通过IoC容器管理对象的生命周期和依赖关系。 4. **事件聚合(Event Aggregator)**:Prism提供了一个事件聚合器,用于在不同模块或组件之间进行松耦合的通信。 5. **导航(Navigation)**:Prism提供了一套强大的导航机制,支持在不同页面或视图之间进行导航。 ### 示例代码:使用Prism框架实现MVVM模式 以下是一个简单的代码示例,展示如何使用Prism框架在WPF中实现MVVM模式,并绑定命令到按钮上。 ```csharp // ViewModel using Prism.Commands; using Prism.Mvvm; using System.Windows; namespace PrismExample.ViewModels { public class MainViewModel : BindableBase { private string _message; public string Message { get { return _message; } set { SetProperty(ref _message, value); } } public DelegateCommand ShowMessageCommand { get; } public MainViewModel() { ShowMessageCommand = new DelegateCommand(ShowMessage); Message = "Hello from Prism!"; } private void ShowMessage() { MessageBox.Show(Message); } } } ``` ```xml <!-- View (XAML) --> <Window x:Class="PrismExample.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Prism Example" Height="200" Width="400"> <Grid> <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"> <TextBlock Text="{Binding Message}" FontSize="16" Margin="10"/> <Button Content="Show Message" Command="{Binding ShowMessageCommand}" Width="120" Margin="10"/> </StackPanel> </Grid> </Window> ``` ```csharp // Bootstrapper using Prism.Ioc; using Prism.Modularity; using Prism.Unity; using System.Windows; using PrismExample.Views; using PrismExample.ViewModels; namespace PrismExample { public class Bootstrapper : UnityBootstrapper { protected override DependencyObject CreateShell() { return Container.Resolve<MainWindow>(); } protected override void InitializeShell() { Application.Current.MainWindow.Show(); } protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog) { // No modules in this example } protected override void RegisterTypes(IContainerRegistry containerRegistry) { containerRegistry.RegisterForNavigation<MainWindow, MainViewModel>(); } } } ``` ```csharp // App.xaml.cs using System.Windows; using PrismExample; namespace PrismExample { public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); var bootstrapper = new Bootstrapper(); bootstrapper.Run(); } } } ``` ### 教程资源推荐 1. **官方文档**:[Prism Documentation](https://prismlibrary.com/docs/) 提供了详细的指南,涵盖从入门到高级主题的内容。 2. **GitHub示例**:Prism的GitHub仓库中提供了多个示例项目,适合不同层次的学习者。[Prism GitHub Examples](https://github.com/PrismLibrary/Prism-Samples-Wpf) 3. **在线教程**: - [Prism 7 for WPF Tutorial](https://www.c-sharpcorner.com/article/prism-7-for-wpf-introduction-and-hello-world/) - [MVVM and Prism in WPF](https://www.codeproject.com/Articles/1267476/Mvvm-and-Prism-in-WPF) 这些资源可以帮助你更好地理解和使用Prism框架,从基础到高级功能逐步深入。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值