【问题标题】:How to publish an event in my module's constructor?如何在我的模块的构造函数中发布事件?
【发布时间】:2009-07-17 13:14:36
【问题描述】:

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

 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;
        }

但是,如果我这样做,以便将视图加载到调用视图模型中的命令的区域中,该命令发布事件,然后我停用视图,那么它可以工作:

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);
    }

查看:

<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">
...

视图模型:

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();
    }
  }
}

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

【问题讨论】:

    标签: c# wpf prism eventaggregator


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 2011-12-27
      相关资源
      最近更新 更多