WPF Prsim Framework (area)

  1. IoC (Inversion of Control):
    IoC is a design pattern. Its basic idea is to transfer control rights such as object creation, destruction, and dependency maintenance from the program code and hand it over to the container for management. . In Spring, the IoC container is a container responsible for managing dependencies between objects. It is responsible for creating objects, maintaining relationships between objects, and injecting dependencies when needed.
  2. DI (Dependency Injection):
    DI is a specific implementation method of IoC. It refers to injecting other objects (i.e. dependencies) on which the object depends on into the object through the constructor, Setter method or other means, thus eliminating the need for Coupling relationships between objects. In Spring, DI is implemented through annotations or XML configuration files, which can make the dependencies between objects clearer, simpler and maintainable.

In general, IoC is an idea that emphasizes handing over the control of dependencies between objects to the container for management; DI is a specific implementation method that emphasizes injecting other objects on which the object depends. way to eliminate the coupling relationship between objects. In Spring, IoC and DI are usually used together to achieve loose coupling between objects by injecting dependencies between objects into the container, making the code clearer, simpler and easier to maintain.

The Prism framework is a manifestation of the idea of ​​decoupling

Region definition: In Prism , we can no longer fix the displayed content of a page, and this concept becomes the concept of region division. Divide the area displayed on the page into N Regions. At this time, each Region will become a dynamically allocated area.

Introduce Prism with the method mentioned in the MVVM article

Create the folder, xaml and viewmodel files as shown in the picture,

 Page code: (note that pageA and PageB are UserControl)

Main.xaml

 <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <WrapPanel>
            <Button Height="50" Width="100" Command="{Binding OpenCommnd}" CommandParameter="PageA"/>
            <Button Height="50" Width="100" Command="{Binding OpenCommnd}" CommandParameter="PageB"/>
            <Button Width="100" Height="40" Command="{Binding BackCommnd}"/>
        </WrapPanel>

        <ContentControl Grid.Row="1" prism:RegionManager.RegionName="con"/>
    </Grid>
<!--prism:RegionManager.RegionName="con" 创建区域化命名-->

PageA.xaml

<Grid>
        <StackPanel>
            <TextBlock Text="{Binding Id}"/>
            <TextBlock Text="{Binding Name}"/>
            <TextBlock Text="{Binding Sex}"/>
        </StackPanel>
    </Grid>

PageB.xaml

<Grid>
        <TextBlock Text="111111"/>
    </Grid>

ViewModel:

MainViewModel

namespace NavagationWpf.ViewModels
{
    public class MainViewModel
    {
        public MainViewModel(IRegionManager regionManager)
        {
           //xaml中绑定的事件
            OpenCommnd = new DelegateCommand<string>(Open); 
           //接收继承此接口的类        
            iRegionManager = regionManager;
        }

        private void Open(string obj)
        {
           //通过命名去寻找后台注册的区域块,并实现跳转
           iRegionManager.Regions["con"].RequestNavigate(obj);                         
        }

        public DelegateCommand<string> OpenCommnd { get; private set; }

       //可读属性,定义一个接口,目的为了接收
       private readonly IRegionManager iRegionManager;

      
    }
}

PageAViewModel

  public class PageAViewModel : BindableBase
    {

        private int id;

        public int Id
        {
            get { return id; }
            set
            {
                id = value;
                RaisePropertyChanged();
            }
        }

        private string name;

        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                RaisePropertyChanged();
            }
        }
        private bool sex;

        public bool Sex
        {
            get { return sex; }
            set
            {
                sex = value;
                RaisePropertyChanged();
            }
        }
        PageAViewModel()
        {
            Id = 0;
        }

PageBViewModel

public class PageBViewModel
    {
         
    }

in app.xaml.cs

namespace NavagationWpf
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<Main>();
        }
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
//注册usercontrol A
            containerRegistry.RegisterForNavigation<PageA>();
//注册usercontrol B
            containerRegistry.RegisterForNavigation<PageB>();
        }

    }
}

Guess you like

Origin blog.csdn.net/qq_57212959/article/details/131996288