2

我有以下问题:

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

消息传递本身是有效的,因为当我稍后在这些 ViewModel 上发送消息时,它们会按预期做出反应。

所以我希望接收 ViewModel 在我从 MainViewModel 构造函数发送消息时没有监听。因此,我切换了使用 SimpleIoc 注册 ViewModel 的顺序,但无济于事。

我究竟做错了什么?

视图模型定位器

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

主视图模型

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

浏览器选择虚拟机

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

    }

用户凭据虚拟机

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

1 回答 1

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

浏览器选择VM.cs

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

用户凭据VM.cs

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

主视图模型.cs

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

主窗口.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>

于 2013-10-26T19:21:46.023 回答