views:

121

answers:

1

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?

A: 

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.

Mark Green