Prism an open source framework designed to develop scalable, modular and testable enterprise-class XAML applications for platforms such as WPF (Windows Presentation Foundation) and Xamarin Forms.
Prism is based on the MVVM (Model-View-ViewModel) design pattern , provides a rich set of tools and libraries to achieve modularity , dependency injection , navigation and event aggregation and other features .
This article describes the basic concepts, installation steps, and use of the Prism framework.
What is Prism?
Prism is a framework for developing flexible, maintainable WPF, Windows 10 UWP, and applications. It was developed by Microsoft's Patterns and Practices team to build modular, testable applications.Prism includes several core components to support application architecture and design patterns:
1、Dependency Injection
Prism provides a dependency injection container that decouples an application's components and services to improve code testability and maintainability.
2、Modularity
Prism supports a modular design that breaks down an application into separate modules, each responsible for a specific function. This helps reduce the complexity of the application and makes it easier to develop and maintain.
3、Navigation
Prism provides a flexible navigation system that defines navigation paths between views and manages view lifecycles.
4、Event Aggregator
This is a loosely coupled event publish/subscribe mechanism where different parts of the application communicate with each other without directly referencing each other.
5、Commands
Prism provides a simplified way to handle commands in the user interface, such as button click events.
6、Data Binding
Although Prism itself does not provide a data binding mechanism, it is tightly integrated with WPF and the data binding framework, making it easy to bind view models to views.
7、ViewModel
Prism encourages the use of the View Model pattern, a design pattern that separates the business logic from the user interface, contributing to a clearer and more maintainable program.
Installing Prism
Prism can be installed via the NuGet Solution Package Manager, which installs three main , , .
First create a new WPF, Xamarin Forms, Uno or WinUI project, then open the NuGet Package Manager, right-click on the project -> select "Manage NuGet Packages".
1. Install the Prism core package
In the NuGet Package Manager, search for and install the。
2、Install the container package
In the NuGet Package Manager, search for themaybe
(choose according to your needs) and click Install.
Unity is one of Prism's officially recommended containers, but DryIoc may offer higher performance in some cases.
3. Installation of platform packages
-
WPF Installation
-
Xamarin Forms Installation
-
Uno Platform Installation
-
WinUI Installation
For specific steps, you can refer to the following figure:
Using Prism
The WPF MVVM framework Prism navigation is implemented by a manual code knocking example, which can be found in the following code.
1, the new WPF project
First create a newWPF
Project, according to the above illustration to complete the installation of Prism, the specific project structure is shown in the following figure:
1. Framework using .NET 6.0, Visual Studio 2022.
2. New constructionViews
together withViewModels
file (paper)
2. Rewrite
Adding Namespacesxmlns:prism="/"
Remember to deleteStartupUri="
Inherited from Application->PrismApplication, the code is shown below.
<prism:PrismApplication x:Class="" xmlns="/winfx/2006/xaml/presentation" xmlns:x="/winfx/2006/xaml" xmlns:local="clr-namespace:" xmlns:prism="/"> <> </> </prism:PrismApplication>
3. Modification
Inherited from Application->PrismApplication, the code is shown below:
public partial class App : PrismApplication { protected override Window CreateShell() { return <MainWindow>(); } protected override void RegisterTypes(IContainerRegistry containerRegistry) { <Home, HomeViewModel>(); <Edge, EdgeViewModel>(); } protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); } }
Two abstract methods are implemented here: the
CreateShell
This method returns a window of type Window, which is actually the main window of the application.
RegisterTypes
This method is used during the initialization of Prism to define some of our own needs.enrollmenttype, so that it can be used in Prism.
Attention:
Views
Two new UserControls were created in the folderHome、Edge
beginning ofRegisterTypes
Make a registration.
ViewModels
Two new VMs were created in the folderHomeViewModel、EdgeViewModel
Make a registration.
4. Modification
-
Adding Namespaces
xmlns:prism="/"
-
set up
prism:="True" Prism
The framework will automatically find the ViewModel corresponding to the view based on the rules. -
Used
WPFDevelopers
hit the nail on the headDrawerMenu
Perform switching menus.
<wd:Window x:Class="" xmlns="/winfx/2006/xaml/presentation" xmlns:x="/winfx/2006/xaml" xmlns:d="/expression/blend/2008" xmlns:wd="/WPFDevelopersOrg/WPFDevelopers" xmlns:mc="/markup-compatibility/2006" xmlns:local="clr-namespace:" xmlns:vm="clr-namespace:" xmlns:prism="/" xmlns:i="/xaml/behaviors" Title= "Prism - navigation bar" Width="800" Height="450" prism:="True"> <Grid> </Grid> </wd:Window>
5、MainWindowViewModel
decide upon a candidateViewModels
File right-click creationMainWindowViewModel
predecessorBindableBase
-
utilization
RegionManager
uplinkRequestNavigate
method, which allows you to specify the area to navigate. -
utilization
RegionManager
upperRegisterViewWithRegion
(of cargo etc) loadView
。 -
utilization
RegionManager
upperRequestNavigate
Navigation menu.
pass (a bill or inspection etc)
prism:="True"
attribute is automatically bound to theMainWindowViewModel
。
In this way, whenMainWindow
When loaded, Prism automatically creates and associates theMainWindowViewModel
Example.
public class MainWindowViewModel : BindableBase { private DrawerMenuItem _selectedItem; public DrawerMenuItem SelectedItem { get { return _selectedItem; } set { SetProperty(ref _selectedItem, value); } } public DelegateCommand SelectionChangedCommand { get; } private readonly IRegionManager _regionManager; /// <summary> /// /// </summary> /// <param name="regionManager"></param> public MainWindowViewModel(IRegionManager regionManager) { } void UpdateRegionViews() { } }
The example creates a simple Prism application where thecap (a poem)
Configured Prism startup and dependency injection.
4. Start-up procedure
Through the writing of the above code to complete the WPF framework application, the specific running results are shown below:
summarize
Prism is an MVVM pattern framework designed for WPF applications that promotes loosely coupled design in teamwork through dependency injection and control inversion containers.
If you found this article helpful, why not support it with a like? Your support is what keeps me motivated to continue sharing my knowledge. Feel free to leave a comment if you have any questions or need further help.
You can also join WeChat[DotNet Technician] community to share ideas and grow with other tech-loving peers!Excellence is a habit, so feel free to leave a comment and learn!