为什么在新窗口中设置属性未打开?

时间:2017-10-10 08:30:06

标签: c# wpf mvvm

我正在学习MVVM并创建了一个首先打开的登录窗口。 登录后,我的MainWindow打开。 MainWindows标题是通过MainWindowViewModel中的Property设置的,但是当我通过LoginWindow打开这个窗口而不是使它成为StartUp Window时,它现在没有显示(它为空)。

这是我如何从登录中打开MainWindow的代码。

LoginViewModel.cs

if (r)
{
    CurrentUser.Username = Username;
    Messenger.Default.Send(new NotificationMessage("CloseWindow"));
}

LoginView.xaml.cs

public Login()
    {
        InitializeComponent();

        Messenger.Default.Register<NotificationMessage>(this, (message) =>
        {
            switch (message.Notification)
            {
                case "CloseWindow":
                    Messenger.Default.Send(new NotificationMessage("NewCourse"));
                    var MainWindow = new MainWindow();
                    MainWindow.Show();
                    this.Close();
                    break;
            }
        });
    }

MainViewModel.cs

public MainViewModel()
    {
        if (IsInDesignMode)
        {
            WindowTitle = "Controlcenter (Designmode)";
            CurrentUserLoggedIn = "Logged in as: " + CurrentUser.Username;
            CurrentVersion = "Version: " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
        }
        else
        {
            WindowTitle = "Controlcenter";
            CurrentUserLoggedIn = "Logged in as: " + CurrentUser.Username;
            CurrentVersion = "Version: " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
        }
    }
    public string WindowTitle { get; private set; }
    public string CurrentUserLoggedIn { get; private set; }
    public string CurrentVersion { get; private set; }

我不知道为什么,但我认为没有调用MainViewModel()。 我使用MVVMLight和PropertyChanged.Fody。 所以我的ViewModelLocator看起来像这样

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

        SimpleIoc.Default.Register<MainViewModel>();
        SimpleIoc.Default.Register<DataErrorInfoViewModel>();
        SimpleIoc.Default.Register<LoginViewModel>();
    }

    public MainViewModel Main => ServiceLocator.Current.GetInstance<MainViewModel>();
    public LoginViewModel Login => ServiceLocator.Current.GetInstance<LoginViewModel>();
    public DataErrorInfoViewModel DataErrorInfo => ServiceLocator.Current.GetInstance<DataErrorInfoViewModel>();

ViewModelLocator有什么问题吗?

编辑: MainWindow.xaml

<Window x:Class="Ui.Desktop.MainWindow"
    [...]
    xmlns:logic="clr-namespace:Logic.Ui;assembly=ControlcenterMVVM.Logic.Ui"
    Title="{Binding WindowTitle, Mode=OneWay}"
    DataContext="{Binding Main, Source={StaticResource Locator}}">

<Window.Resources>
    <DataTemplate x:Name="firmcustomerViewTemplate" DataType="{x:Type logic:FirmcustomerViewModel}">
        <local:Firmcustomer DataContext="{Binding}" />
    </DataTemplate>
    <DataTemplate x:Name="privatecustomerViewTemplate" DataType="{x:Type logic:PrivatecustomerViewModel}">
        <local:Privatecustomer DataContext="{Binding}" />
    </DataTemplate>
</Window.Resources>
<Grid>
    [...]
        <Label Content="{Binding CurrentUser}" FontWeight="Normal" FontSize="13" />
        <Label Content="{Binding CurrentVersion}" />
    </StackPanel>

    <ContentControl Grid.Row="0" Grid.Column="1" Grid.RowSpan="3" Content="{Binding}" />
</Grid>

和MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new FirmcustomerViewModel();
    }

    private void firmcustomer_Click(object sender, RoutedEventArgs e)
    {
        DataContext = new FirmcustomerViewModel();
    }

    private void privatecustomer_Click(object sender, RoutedEventArgs e)
    {
        DataContext = new PrivatecustomerViewModel();
    }
}

那么PropertyChanged.Fody现在对我来说是什么? 在MainWindowViewModel中,我只需按如下方式添加属性:

    public string WindowTitle { get; private set; }
    public string CurrentUserLoggedIn { get; private set; }
    public string CurrentVersion { get; private set; }

不,我编译项目并使用dotpeek反编译项目,看看它现在的样子

public string WindowTitle
{
  get
  {
    return this.\u003CWindowTitle\u003Ek__BackingField;
  }
  private set
  {
    if (string.Equals(this.\u003CWindowTitle\u003Ek__BackingField, value, StringComparison.Ordinal))
      return;
    this.\u003CWindowTitle\u003Ek__BackingField = value;
    this.RaisePropertyChanged(nameof (WindowTitle));
  }
}

public string CurrentUserLoggedIn
{
  get
  {
    return this.\u003CCurrentUserLoggedIn\u003Ek__BackingField;
  }
  private set
  {
    if (string.Equals(this.\u003CCurrentUserLoggedIn\u003Ek__BackingField, value, StringComparison.Ordinal))
      return;
    this.\u003CCurrentUserLoggedIn\u003Ek__BackingField = value;
    this.RaisePropertyChanged(nameof (CurrentUserLoggedIn));
  }
}

public string CurrentVersion
{
  get
  {
    return this.\u003CCurrentVersion\u003Ek__BackingField;
  }
  private set
  {
    if (string.Equals(this.\u003CCurrentVersion\u003Ek__BackingField, value, StringComparison.Ordinal))
      return;
    this.\u003CCurrentVersion\u003Ek__BackingField = value;
    this.RaisePropertyChanged(nameof (CurrentVersion));
  }
}

所以RaisePropertyChaned就在那里。

0 个答案:

没有答案