WPF's super easy-to-use framework Prism is used for getting started, and the host computer quickly learns it!

Introduction to Prism

The WPF framework Prism is a framework for developing modular, reusable and testable WPF applications. It provides a simple and powerful way to manage the code of complex applications and build highly scalable applications.

How to learn the Prism framework

If you want to use the Prism framework to develop WPF applications, you need to learn the following aspects:

  1. MVVM pattern : Prism is based on the MVVM pattern, so knowledge of the MVVM pattern is required. This is because Prism uses some of the best practices of the MVVM pattern, such as commands, bindings, etc.
  2. Dependency Injection : Prism uses the dependency injection pattern, so knowledge of dependency injection and IoC containers (such as Unity) is required.
  3. Modular development : Prism provides a modular development approach, making applications easier to expand and maintain. Therefore, it is necessary to understand how to use Prism to build reusable and extensible modules.
  4. Event aggregator : Prism provides an event aggregator that enables loosely coupled communication between modules. Therefore, it is necessary to understand how to use the event aggregator to realize the communication between various modules.

Detailed steps to use the Prism framework

  1. Create a new WPF application project in Visual Studio.
  2. Install the Prism.Wpf and Prism.Unity packages in the NuGet package manager.
  3. Create a Shell form and set it as the main form in App.xaml.cs.
  4. Create a Unity container in App.xaml.cs and set it as the default container for the application.
  5. Register all the regions to use with the RegionManager in the shell form. Regions are where modules are placed.
  6. Create a module project and define a module class in it. The module class needs to inherit from the IModule interface and implement the initialization logic of the module in it.
  7. In the Initialize method of the module class, register all views and services in the module with the RegionManager and the Unity container.
  8. Add modules to your application using the module catalog.
  9. Use the RegionManager in the Shell form to navigate to the initial view to display.

example

Suppose we want to create a simple login module. The module needs to contain the following views:

  • Login view: Contains username and password input boxes and a login button.
  • Welcome view: displayed after a successful login, including a welcome message and a logout button.

We can implement this module using the Prism framework as follows:

  1. Create a module project named LoginModule and create LoginModule class in it.
  2. Implement the IModule interface in the LoginModule class, and register views and services in the Initialize method. For example:
public void Initialize()
{
    _container.RegisterType<ILoginService, LoginService>();
    _regionManager.RegisterViewWithRegion("ContentRegion", typeof(LoginView));
    _regionManager.RegisterViewWithRegion("ContentRegion", typeof(WelcomeView));
}

  1. Create LoginView and WelcomeView views, and use dependency injection to get the ILoginService service instance. For example:
public LoginViewModel(ILoginService loginService)
{
    _loginService = loginService;
    LoginCommand = new DelegateCommand(Login, CanLogin);
}

  1. Use the RegionManager to register the ContentRegion region in the Shell form, and register the LoginView and WelcomeView views in the module. For example:
_regionManager.RegisterViewWithRegion("ContentRegion", typeof(LoginView));
_regionManager.RegisterViewWithRegion("ContentRegion", typeof(WelcomeView));

  1. Use the Command binding in LoginView to bind the login button to the LoginCommand command. For example:
<Button Content="Login" Command="{Binding LoginCommand}" />

  1. In the Login method of LoginViewModel, use the ILoginService service to verify the username and password. If the validation passes, use the RegionManager to navigate to the WelcomeView view. For example:
private void Login()
{
    if (!_loginService.ValidateCredentials(Username, Password))
    {
        ErrorMessage = "Invalid username or password";
        return;
    }

    _regionManager.RequestNavigate("ContentRegion", "WelcomeView");
}

  1. In the WelcomeView view, a welcome message and a logout button are displayed. For example:
<TextBlock Text="{Binding WelcomeMessage}" />
<Button Content="Logout" Command="{Binding LogoutCommand}" />

  1. In WelcomeViewModel, implement LogoutCommand to navigate back to the LoginView view. For example:
private void Logout()
{
    _regionManager.RequestNavigate("ContentRegion", "LoginView");
}

To create a Shell form using C#, follow these steps:

  1. Create a new WPF application project.
  2. Add a new form, eg ShellWindow.xaml.
  3. In the App.xaml.cs file, set the ShellWindow as the main form of the application.
  4. In ShellWindow.xaml, use Grid or other layout controls to create the layout of the application.
  5. In ShellWindow.xaml.cs, use the RegionManager to register all regions to use. Regions are where modules are placed.
  6. Create a module project and define a module class in it. The module class needs to inherit from the IModule interface and implement the initialization logic of the module in it.
  7. In the Initialize method of the module class, register all views and services in the module with the RegionManager and the Unity container.
  8. Add modules to your application using the module catalog.
  9. Use the RegionManager in the Shell form to navigate to the initial view to display.

For example, the following is sample code for a simple Shell form:

<Window x:Class="MyApp.ShellWindow"
        xmlns="<http://schemas.microsoft.com/winfx/2006/xaml/presentation>"
        xmlns:x="<http://schemas.microsoft.com/winfx/2006/xaml>"
        Title="MyApp" Height="500" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Menu Grid.Row="0">
            <!--菜单项-->
        </Menu>
        <ContentControl Grid.Row="1" prism:RegionManager.RegionName="MainRegion"/>
    </Grid>
</Window>

In this example, we use a Grid as the layout of the application and define a region called MainRegion to place the module's views. Then, in the ShellWindow.xaml.cs file, we use the RegionManager to register the MainRegion region.

public partial class ShellWindow : Window
{
    public ShellWindow(IRegionManager regionManager)
    {
        InitializeComponent();
        RegionManager.SetRegionManager(this, regionManager);
    }
}

Now that we have successfully integrated the Shell form with the Prism framework, we can start creating modules and adding them to the application.

To create a Prism module project, you can follow these steps:

  1. In Visual Studio, choose File -> New -> Project.
  2. In the New Project dialog, select WPF -> Prism -> Prism Modules.
  3. Enter a project name and choose where to create the project.
  4. Click the "Create" button.
  5. In the Prism Module Wizard, select the functionality (for example, views and services) to include in the module.
  6. Click the "Finish" button.

Now that you have created a Prism module project, you can start implementing your code logic in it.

If you cannot find the Prism option in the new project, you need to install the Prism template first. You can follow the steps below:

  1. Open Visual Studio and select Tools -> Extensions and Updates.
  2. In the Extensions and Updates dialog, select Online and search for "Prism Template Pack".
  3. Click the "Download" button and wait for the download to complete.
  4. Click the "Install" button and wait for the installation to complete.
  5. Close Visual Studio and reopen it.
  6. You should now see Prism options in the New Project dialog.

If you still can't find the Prism option, make sure the Prism templates are properly installed, and check that your version of Visual Studio is compatible with the version of Prism you're using.

If you are unable to install the Prism templates in Visual Studio 2022, make sure you are using the latest version of Prism and check if there is a version available for VS2022. Also try installing Prism templates manually. The latest version of the Prism template can be downloaded from the following link: https://marketplace.visualstudio.com/items?itemName=BrianLagunas.PrismTemplatePack. After downloading, run the downloaded file to install the template. If you still cannot install Prism templates, check your network connection and make sure you have the proper permissions to install the software. If the problem persists, please contact the Prism community for assistance.

Guess you like

Origin blog.csdn.net/Documentlv/article/details/130412030