其他分享
首页 > 其他分享> > MVVM框架

MVVM框架

作者:互联网

MVVM框架

Views和ViewModels自动关联关系

首先在xaml中设置命名空间

 xmlns:prism="http://prismlibrary.com/"

然后设置自动绑定ViewModels

prism:ViewModelLocator.AutoWireViewModel="True"

以点击button控件改变标题为例

继承BindableBase,使用DelegateCommand

在XAML文件中添加Button,绑定我们写好的OpenCommand函数。绑定标题为点击按钮改变的标题变量。

 <Grid>
        <ContentControl prism:RegionManager.RegionName="ContentRegion" />
        <StackPanel>
            <Button Command="{Binding OpenCommand}" Content="UpdateText"/>
            <TextBlock FontSize="38" Text="{Binding Title}"/>
        </StackPanel>
 </Grid>

在ViewModels里面新建一个MainWindowViewModel的类,加上之前在XAML中的设置,就绑定了Views中的MainWindow.xaml和ViewModels里面的MainWindowViewModel.cs

首先使新建的类继承BindableBase

然后创建一个简单的属性,来显示标题。

private string _title;
public string Title
{
    get { return _title; }
    set { SetProperty(ref _title, value); RaisePropertyChanged(); }
}
Title = "Hello";

创建一个函数,来改变标题

public DelegateCommand OpenCommand { get; private set; }

OpenCommand = new DelegateCommand(() =>
{
    Title = "Prism";
});

整体代码如下:

using Prism.Commands;
using Prism.Mvvm;

namespace ModelA.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private string _title;
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); RaisePropertyChanged(); }
        }
        public DelegateCommand OpenCommand { get; private set; }

        public MainWindowViewModel()
        {
            Title = "Hello";

            OpenCommand = new DelegateCommand(() =>
            {
                Title = "Prism";
            });
        }
    }
}

在XAML中创建一个按钮来绑定这个OpenCommand函数,用来改变标题。

<Button Command="{Binding OpenCommand}" Content="UpdateText"/>

创建复合按钮

继承 BindableBase,使用CompositeCommand

可以创建一个符合按钮,当按钮触发时,会触发复合按钮注册的所有按钮函数

整体代码

using Prism.Commands;
using Prism.Mvvm;

namespace ModelA.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private string _title;
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); RaisePropertyChanged(); }
        }
        public DelegateCommand OpenCommand { get; private set; }
        public DelegateCommand OpenCommand1 { get; private set; }
        public CompositeCommand OpenAll { get; private set; }

        public MainWindowViewModel()
        {
            //Title = "Hello";

            OpenCommand = new DelegateCommand(() =>
            {
                Title += "OpenCommand\r\n";
            });
            OpenCommand1 = new DelegateCommand(() =>
            {
                Title += "OpenCommand1\r\n";
            });

            OpenAll = new CompositeCommand();
            OpenAll.RegisterCommand(OpenCommand);
            OpenAll.RegisterCommand(OpenCommand1);

        }
    }
}

运行结果

消息订阅-时间聚合器

使用IEventAggregator创建接口

主要用来接收订阅以及发布消息,订阅者可以接收到发布者发送的内容。

创建事件

// 定义一个消息类型 预定义一个string类型
public class MessageEvent : PubSubEvent<string>
{

}
// 获取消息事件
public void OnMessageReceived(string message)
{
    Title += message + "\r\n";
}

发布订阅

// 发送一个消息
eventAggregator.GetEvent<MessageEvent>().Publish("Hello");

订阅消息

// 订阅一个消息
eventAggregator.GetEvent<MessageEvent>().Subscribe(OnMessageReceived);

全部代码

using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;

namespace ModelA.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        public MainWindowViewModel(IEventAggregator eventAggregator)
        {
            //Title = "Hello";

            OpenCommand = new DelegateCommand(() =>
            {
                // 订阅一个消息
                eventAggregator.GetEvent<MessageEvent>().Subscribe(OnMessageReceived);
            });

            SendCommand = new DelegateCommand(() =>
            {
                // 发送一个消息
                eventAggregator.GetEvent<MessageEvent>().Publish("Hello");
            });

            this.eventAggregator = eventAggregator;
        }

        private string _title;
        private readonly IEventAggregator eventAggregator;

        // 获取消息事件
        public void OnMessageReceived(string message)
        {
            Title += message + "\r\n";
        }
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); RaisePropertyChanged(); }
        }
        public DelegateCommand OpenCommand { get; private set; }
        public DelegateCommand SendCommand { get; private set; }

        // 注册一个IEventAggregator接口
        
    }

    // 定义一个消息类型 预定义一个string类型
    public class MessageEvent : PubSubEvent<string>
    {

    }

}

事件过滤器

当不同的地方订阅相同的事件时,可以选择性的进行传入。

// 订阅一个消息
eventAggregator.GetEvent<MessageEvent>()
.Subscribe(OnMessageReceived, ThreadOption.PublisherThread, false, msg => 
{
    // 过滤消息,如果是Hello就返回true
    if (msg.Equals("Hello")) return true;
    else
        return false;
});

取消订阅

// 取消订阅这个事件
eventAggregator.GetEvent<MessageEvent>().Unsubscribe(OnMessageReceived);

标签:DelegateCommand,string,框架,Title,title,OpenCommand,MVVM,public
来源: https://blog.csdn.net/qq_41636200/article/details/122093335