an error "EventAggregator is Null"?

Jul 25, 2012 at 1:34 PM
first ,thanks to the great Framework. 
I got a strange error .In OnImportsSatisfied()    or  InitializeVm() everything is ok!
but when i execute the LoginWorkFlow , the "EventAggregator" is null reference.
why?
    private IEnumerable LoginWorkFlow()
        {
            WorkflowAction wfa =new WorkflowAction();
            wfa.Execute =
                () => MessagePool.GetInstance().AddSendMessage(GameCMDFormat.ValidateUserLogin(UserName, Password));
            
             handler = null;
            MessageService.GetInstance().ReturnLoginTrueEventHandler += handler = (s, e) =>
                                                                                      {
                                                                                         
                MessageService.GetInstance().
                    ReturnLoginTrueEventHandler -= handler;
                GlobalService.IsLogined = true;
                int i = 10;
		
// The next, Raise an error "EventAggregator is Null" 
               JounceHelper.ExecuteOnUI(()=>EventAggregator.Publish(new LoginNotifyArgs()));
                
                wfa.Invoked();
            };
            MessageService.GetInstance().ReturnLoginFalseEventHandler += handler = (s, e) =>
            {
                MessageService.GetInstance().
                    ReturnLoginFalseEventHandler -= handler;
                int i = 10;
                wfa.Invoked();
            };
            int xI = 10;

            yield return wfa;


        }

z

Jul 25, 2012 at 1:38 PM

the EventAggregator inherit from BaseViewModel

  [ExportAsViewModel(Constants.VM_LOGIN)]
    public class LoginViewModel : BaseViewModel, ILoginViewModel, IPartImportsSatisfiedNotification, IEventSink<ViewNavigationArgs>, IEventSink<LoginNotifyArgs>

Jul 25, 2012 at 10:06 PM

When is LoginWorkFlow called?  If you call it from the constructor the imports will not have been satisfied yet.

Jul 25, 2012 at 10:45 PM

Thanks PaulLinton,

LoginWorkFlow is Called in a IActionCommand , named LoginCommand. and LoginCommand is binding to View.

       public LoginViewModel()
        {
          LoginCommand = new ActionCommand<object>(
           obj =>
           _Login(_userName, _password),
           obj => true);

         }

        private void _Login(string userName, string password)
        {
            WorkflowController.Begin(LoginWorkFlow(), ex => JounceHelper.ExecuteOnUI(() => MessageBox.Show(ex.Message)));
        }

 

Jul 25, 2012 at 11:34 PM

The "EventAggregator" used in view  is ok,

In ViewModel OnImportsSatisfied()  and InitializeVm() "EventAggregator" is ok!

Next, when I invoke the  IActionCommand   in ViewModel, the "EventAggregator" is null.              why?

[ExportAsView(Constants.VIEW_LOGIN,MenuName = "dddd")]
    [ExportViewToRegion(Constants.VIEW_LOGIN, LocalRegions.LOGIN_REGION)]
    public partial class Login : UserControl
    {
        [Import]
        public IEventAggregator EventAggregator { get; set; }
        public Login()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            EventAggregator.Publish(new LoginNotifyArgs());
        }
    }
}

Jul 25, 2012 at 11:40 PM

Have you bound the View to the ViewModel?

[Export]
public ViewModelRoute Binding { 
   get  { 
     return ViewModelRoute.Create(Constants.VM_LOGIN, Constants.VIEW_LOGIN); 
   } 
}
Jul 26, 2012 at 12:58 AM

yes, I've bound the View to the ViewModel in Binding.cs.

 public class Binding
    {
        [Export]
        public ViewModelRoute MainBinding
        {
            get { return ViewModelRoute.Create(Constants.VM_MAIN, Constants.VIEW_MAIN); }
        }

        [Export]
        public ViewModelRoute LoginBinding
        {
            get { return ViewModelRoute.Create(Constants.VM_LOGIN, Constants.VIEW_LOGIN); }
        }

Jul 26, 2012 at 1:10 AM

How is the Login view created?  Do you Publish ViewNavigationArgs or is it created from XAML?

Jul 26, 2012 at 1:20 AM

In its container view 's viewmodel.

     public void OnImportsSatisfied()
        {

 EventAggregator.Publish(new ViewNavigationArgs(Constants.VIEW_LOGIN));

Jul 26, 2012 at 1:26 AM

/*****************in container view  

         <ContentControl Margin="0,0,40,32"
                            HorizontalAlignment="Stretch"
                            VerticalAlignment="Stretch"
                            HorizontalContentAlignment="Stretch"
                            VerticalContentAlignment="Stretch"
                            Regions:ExportAsRegion.RegionName="LoginRegion" />

/******************************Login view

    [ExportAsView(Constants.VIEW_LOGIN,MenuName = "dddd")]
    [ExportViewToRegion(Constants.VIEW_LOGIN, LocalRegions.LOGIN_REGION)]
    public partial class Login : UserControl

/*********************** Region define

    public class LocalRegions
    {
        public const string TAB_REGION = "TabRegion";
        public const string APP_REGION = "AppRegion";
        public const string LOGIN_REGION = "LoginRegion";

Jul 26, 2012 at 4:19 AM

Thanks PaulLinton,

I Re-write the whole code of my project , Everything is ok! 

Too strange, so I can......