如何在模块的构造函数中发布事件?

发布于 2024-07-26 19:52:53 字数 2984 浏览 7 评论 0原文

当我尝试在我的客户模块中发布事件时,它不起作用(订阅者没有收到对象,不显示任何内容):

 public class CustomersRegister : IModule
    {
        private static IRegionManager regionManager;
        private static IRegion region;
        private static CustomersMainView view;
        private static CustomerAllView allview;
        private static CustomerEdit editView;

        public CustomersRegister(IRegionManager manager)
        {
            regionManager = manager;
        }

        public void Initialize()
        {
            StackPanel sp = LoadNavigation();
            sp.PublishEvent(PublishEventNames.AddCustomerMenu);
        }

        public static StackPanel LoadNavigation()
        {
            StackPanel sp = new StackPanel();
            sp.Margin = new Thickness { Left = 10.0, Top = 5.0, Right = 10.0 };
            sp.Children.Add(new CustomerMainMenu());
            return sp;
        }

但是,如果我执行此解决方案,以便将视图加载到调用viewmodel 中的命令,它发布事件,然后我停用视图,然后它就可以工作:

public class CustomersRegister : IModule
{
    private static IRegionManager regionManager;
    private static IRegion region;
    private static CustomersMainView view;
    private static CustomerAllView allview;
    private static CustomerEdit editView;

    public CustomersRegister(IRegionManager manager)
    {
        regionManager = manager;
    }

    public void Initialize()
    {
        region = regionManager.Regions[RegionNames.DockManagerContainer];
        view = new CustomersMainView();
        view.DataContext = new ViewModels.CustomersMainViewModel();
        region.Add(view);
        region.Activate(view);
    }

View:

<UserControl x:Class="CustomersModul.Views.CustomersMainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ncal="http://infragistics.com/ncal"
    xmlns:Commands="clr-namespace:CRMInfrastructure.Commands;assembly=CRMInfrastructure"
    ncal:XamDockManagerSettings.IsContentPaneInDocumentHost="True"              
    Commands:AvarioCommandBehavior.TheCommandToRun="{Binding LoadNavigation}"
    Commands:AvarioCommandBehavior.RoutedEventName="Loaded"
    Commands:AvarioCommandBehavior.CommandParameter="loading">
...

ViewModel:

using System.Windows.Input;
using CRMInfrastructure;
using CRMInfrastructure.Commands;
using CRMInfrastructure.Helpers;

namespace CustomersModul.ViewModels
{
  public class CustomersMainViewModel : ViewModelBase
  {
    public ICommand LoadNavigation { get; set; }

    public CustomersMainViewModel()
    {
      LoadNavigation = new DelegateCommand<object>(load);
    }

    void load(object o)
    {
        CustomersNavigation.LoadNavigation().PublishEvent(PublishEventNames.AddCustomerMenu);
        CustomersRegister.DeactivateView();
    }
  }
}

我怎样才能简单地在构造函数中发布事件模块而不是做这个奇怪的解决方法?

When I try to publish an event in my Customer Module, it doesn't work (the subscriber does not receive the object, displays nothing):

 public class CustomersRegister : IModule
    {
        private static IRegionManager regionManager;
        private static IRegion region;
        private static CustomersMainView view;
        private static CustomerAllView allview;
        private static CustomerEdit editView;

        public CustomersRegister(IRegionManager manager)
        {
            regionManager = manager;
        }

        public void Initialize()
        {
            StackPanel sp = LoadNavigation();
            sp.PublishEvent(PublishEventNames.AddCustomerMenu);
        }

        public static StackPanel LoadNavigation()
        {
            StackPanel sp = new StackPanel();
            sp.Margin = new Thickness { Left = 10.0, Top = 5.0, Right = 10.0 };
            sp.Children.Add(new CustomerMainMenu());
            return sp;
        }

However, if I do this work around so that I load a view into the region which calls a command in the viewmodel, which publishes the event, and then I deactive the view, then it works:

public class CustomersRegister : IModule
{
    private static IRegionManager regionManager;
    private static IRegion region;
    private static CustomersMainView view;
    private static CustomerAllView allview;
    private static CustomerEdit editView;

    public CustomersRegister(IRegionManager manager)
    {
        regionManager = manager;
    }

    public void Initialize()
    {
        region = regionManager.Regions[RegionNames.DockManagerContainer];
        view = new CustomersMainView();
        view.DataContext = new ViewModels.CustomersMainViewModel();
        region.Add(view);
        region.Activate(view);
    }

View:

<UserControl x:Class="CustomersModul.Views.CustomersMainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ncal="http://infragistics.com/ncal"
    xmlns:Commands="clr-namespace:CRMInfrastructure.Commands;assembly=CRMInfrastructure"
    ncal:XamDockManagerSettings.IsContentPaneInDocumentHost="True"              
    Commands:AvarioCommandBehavior.TheCommandToRun="{Binding LoadNavigation}"
    Commands:AvarioCommandBehavior.RoutedEventName="Loaded"
    Commands:AvarioCommandBehavior.CommandParameter="loading">
...

ViewModel:

using System.Windows.Input;
using CRMInfrastructure;
using CRMInfrastructure.Commands;
using CRMInfrastructure.Helpers;

namespace CustomersModul.ViewModels
{
  public class CustomersMainViewModel : ViewModelBase
  {
    public ICommand LoadNavigation { get; set; }

    public CustomersMainViewModel()
    {
      LoadNavigation = new DelegateCommand<object>(load);
    }

    void load(object o)
    {
        CustomersNavigation.LoadNavigation().PublishEvent(PublishEventNames.AddCustomerMenu);
        CustomersRegister.DeactivateView();
    }
  }
}

How can I simply publish the event in the constructor of the Module instead of doing this odd workaround?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦欢 2024-08-02 19:52:53

在我看来,您的 StackPanel 变量在事件传播之前就超出了范围。 这可以解释为什么当您将 ViewModel 加载到该区域并使用对发布方法的静态调用时,行为会有所不同。

尝试在 IModule 类中为 StackPanel 创建一个类级实例变量。

It looks to me as though your StackPanel variable is going out of scope before the event has been propogated. That would explain why the behaviour is different once you've loaded your ViewModel into the region and are using a static call to the publish method.

Try creating a class-level instance variable for the StackPanel in your IModule class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文