wpf 如何从另一个视图模型中调用方法

ttygqcqt  于 2023-05-08  发布在  其他
关注(0)|答案(1)|浏览(261)

HomeViewModel和MainViewModel。我想在HomeViewModel中调用MainViewModelTestEvent方法,但我不知道该怎么做?任何人都可以帮我。我试图使用棱镜,但我总是得到错误时,我通过IEventAggregator参数在MainViewModel。

Exception thrown: 'System.MissingMethodException' in System.Xaml.dll
Exception thrown: 'System.Xaml.XamlObjectWriterException' in System.Xaml.dll
Exception thrown: 'System.NullReferenceException' in System.Private.CoreLib.dll
Object reference not set to an instance of an object.

这是我的MainViewModel

using Learning.Core;
using Learning.EventRegister;
using Learning.MVVM.Model;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Learning.MVVM.ViewModel
{
    public class MainViewModel: ObservableObject
    {
        public RelayCommand HomeViewConmmand { get; set; }
        public RelayCommand DiscoveryViewConmmand { get; set; }
        public RelayCommand TestCommand { get; set; }

        public HomeViewModel HomeVM { get; set; }
        public DiscoveryViewModel DiscoveryVM { get; set; }
        public RoomViewModel RoomVM { get; set; }

        private object _currentView;
        private object _rightView;

        IEventAggregator _eventAggregator;

        public object CurrentView
        {
            get { return _currentView; }
            set { 
                _currentView = value;
                OnPropertyChanged();
            }
        }

        public object RightView
        {
            get { return _rightView; }
            set
            {
                _rightView = value;
                OnPropertyChanged();
            }
        }

        public MainViewModel(IEventAggregator ea) {
            HomeVM = new HomeViewModel();
            DiscoveryVM = new DiscoveryViewModel();
            RoomVM = new RoomViewModel();
            CurrentView = HomeVM;
            RightView = RoomVM;

            HomeViewConmmand = new RelayCommand(o =>
            {
                CurrentView = HomeVM;
            });

            DiscoveryViewConmmand = new RelayCommand(o =>
            {
                CurrentView = DiscoveryVM;
            });

            TestCommand = new RelayCommand(o =>
            {
                MessageBox.Show("Clicked");
            });
        }

        public void TestEvent(string message)
        {
            MessageBox.Show(message);
        }
    }
}

这是我的HomeViewModel。如果我在HomeViewModel的构造器中传递IEventAggregator,我也会得到类似MainViewModel的错误

using Learning.Core;
using Learning.EventRegister;
using Learning.MVVM.Model;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Learning.MVVM.ViewModel
{
    public class HomeViewModel
    {
        // Data
        public List<Room> rooms { get; }

        // Command
        private RelayCommand openRoomCommand;

        //IEventAggregator 
        IEventAggregator _eventAggregator;

        public RelayCommand NotifyCommand { get; set; }

        public HomeViewModel()
        {
            openRoomCommand = new RelayCommand(o =>
            {
                Room cur_room = (Room)o;
                cur_room.IsActive = true;
            });

            rooms = new List<Room>();
            Room room1 = new Room
            {
                Name = "1",
                IsActive = false,
            };
            Room room2 = new Room
            {
                Name = "2",
                IsActive = false,
            };
            Room room3 = new Room
            {
                Name = "3",
                IsActive = false,
            };
            Room room4 = new Room
            {
                Name = "4",
                IsActive = false,
            };

            rooms.Add(room1);
            rooms.Add(room2);
            rooms.Add(room3);
            rooms.Add(room4);
        }

        public void SendEvent()
        {
            //_eventAggregator.GetEvent<CommandSendEvent>().Publish("Something happening");
        }

        public RelayCommand OpenRoomCommand { get => openRoomCommand; }
    }
}
s4n0splo

s4n0splo1#

有三种方法可以从另一个视图模型执行属于另一个视图模型的方法:
1.你在MainViewModel中声明了这个方法static,所以很容易从任何视图模型中调用这个方法(不是最好的,但存在...)
1.你可以通过依赖注入在HomeViewModel构造器中添加一个MainViewModel的示例,你可以找到很多教程来了解注入。
1.您可以使用ViewModel之间的消息传递(IEventAggregator),与第2点相同,您必须在homeViewModel和MainViewModel构造函数中注入IEventAggragor的示例,然后在MainViewModel必须订阅将执行您想要的方法的事件之后,该事件将由HomeViewModel激活

相关问题