从ViewModel构造函数发送时未收到MVVM Light消息

时间:2013-10-26 12:25:05

标签: c# wpf mvvm mvvm-light

我遇到了以下问题:

我在ViewModelLocator中使用SimpleIoc注册4个ViewModel。 在MainViewModel的Contructor中,我向其他3个ViewModel中的2个发送消息。 这些ViewModel不会收到这些消息。

消息本身正在起作用,因为当我稍后在这些ViewModel上发送消息时,会按预期做出反应。

所以我希望接收的ViewModel在我从MainViewModel构造函数发送消息时不会监听。所以我改变了使用SimpleIoc注册ViewModels的顺序,但无济于事。

我做错了什么?

ViewModelLocator

    static ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

        SimpleIoc.Default.Register<LoginMethodVM>();
        SimpleIoc.Default.Register<DatabaseConnDataVM>();
        SimpleIoc.Default.Register<UserCredentialsVM>();
        SimpleIoc.Default.Register<BrowserSelectionVM>();
        SimpleIoc.Default.Register<MainViewModel>();
    }

    public LoginMethodVM LoginMethodVM
    {
        get
        {
            return ServiceLocator.Current.GetInstance<LoginMethodVM>();
        }
    }

    public DatabaseConnDataVM DatabaseConnDataVM
    {
        get
        {
            return ServiceLocator.Current.GetInstance<DatabaseConnDataVM>();
        }
    }

    public UserCredentialsVM UserCredentialsVM
    {
        get
        {
            return ServiceLocator.Current.GetInstance<UserCredentialsVM>();
        }
    }

    public BrowserSelectionVM BrowserSelectionVM
    {
        get
        {
            return ServiceLocator.Current.GetInstance<BrowserSelectionVM>();
        }
    }

MainViewModel

    public MainViewModel()
    {
        Messenger.Default.Send(System.Windows.Visibility.Visible, "UserCredentialsVisible");
        Messenger.Default.Send(System.Windows.Visibility.Visible, "BrowserSelectionVisible");
    }

BrowserSelectionVM

    public BrowserSelectionVM()
    {
        Messenger.Default.Register<System.Windows.Visibility>
            (this,
            "BrowserSelectionVisible",
            msg => { Visible = msg; });

    }

UserCredentialsVM

    public UserCredentialsVM()
    {
        Messenger.Default.Register<System.Windows.Visibility>
            (this,
            "UserCredentialsVisible",
            msg => { Visible = msg; });
    }

1 个答案:

答案 0 :(得分:1)

ViewModelLocator.cs

    static ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

        SimpleIoc.Default.Register<UserCredentialsVM>(true);
        SimpleIoc.Default.Register<BrowserSelectionVM>(true);
        SimpleIoc.Default.Register<MainViewModel>();
    }

    public UserCredentialsVM UserCredentialsVM
    {
        get
        {
            return ServiceLocator.Current.GetInstance<UserCredentialsVM>();
        }
    }

    public BrowserSelectionVM BrowserSelectionVM
    {
        get
        {
            return ServiceLocator.Current.GetInstance<BrowserSelectionVM>();
        }
    }

    public MainViewModel MainViewModel
    {
        get
        {
            return ServiceLocator.Current.GetInstance<MainViewModel>();
        }
    }

    public static void Cleanup()
    {
    }

BrowserSelectionVM.cs

public class BrowserSelectionVM : ViewModelBase
{
    public BrowserSelectionVM()
    {
        Messenger.Default.Register<System.Windows.Visibility>(this,
            "BrowserSelectionVisible",
            msg => { Console.WriteLine(msg); });
    }
}

UserCredentialsVM.cs

public class UserCredentialsVM : ViewModelBase
{
    public UserCredentialsVM()
    {
        Messenger.Default.Register<System.Windows.Visibility>(this,
            "UserCredentialsVisible",
            msg => { Console.WriteLine(msg); });
    }
}

MainViewModel.cs

public class MainViewModel : ViewModelBase
{
    public MainViewModel()
    {
        Messenger.Default.Send(System.Windows.Visibility.Visible, "UserCredentialsVisible");
        Messenger.Default.Send(System.Windows.Visibility.Visible, "BrowserSelectionVisible");
    }
}

MainWindow.xaml

<Window x:Class="MvvmLightRegistrationMock.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    DataContext="{Binding MainViewModel, Source={StaticResource Locator}}">

<Grid x:Name="LayoutRoot">
</Grid>