show()命令不会打开窗口Wpf

时间:2016-05-05 02:13:43

标签: c# wpf mvvm-light

我有一个在我的应用程序启动时运行的以下方法,我首先显示维护屏幕。方法运行后,我想显示我的登录屏幕。但是,登录屏幕无法打开。如果我注释掉显示维护屏幕的代码,它可以正常工作。

    private void Application_Startup(object sender, StartupEventArgs e)
    { 

        ILocalDbDataService _locDataService =new LocalDbDataService();

            Maintenance mWin = new Maintenance();
            mWin.Show();

            MaintenanceViewModel maintenanceViewModel = Microsoft.Practices.ServiceLocation.ServiceLocator.Current.GetInstance<MaintenanceViewModel>();
            maintenanceViewModel.RunMaintenance();
            Login lWin = new Login();                
            lWin.Show();//This windows doesn't open
}

public class MaintenanceViewModel : ViewModelBase
{

    private readonly ILocalDbDataService _localDbDataService;

    public MaintenanceViewModel(ILocalDbDataService localDbDataService)
    {
        _localDbDataService = localDbDataService;

    }

    public  void RunMaintenance()
    {
       bool result= _localDbDataService.RunTransArchiveMaintenance();
       MessengerInstance.Send(new NotificationMessage("CloseMaintenance"));
    }
}
public partial class Maintenance : Window
{
    public Maintenance()
    {
        InitializeComponent();

        Messenger.Default.Register<NotificationMessage>(this, msg =>
        {
            if (msg.Notification == "CloseMaintenance")
            {
                this.Close();
            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

在维护窗口的构造函数中,创建一个Login窗口类的新实例并调用Show()。

public Maintenance (){
Login login=new Login ();
login.Show();
}

答案 1 :(得分:0)

只需将其放在主窗口中即可。

<ContentControl x:Name="SomeName"
                Grid.Row="1"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Stretch"
                Content="{Binding CurrentScreen}" />

如果要更改

,请设置CurrentScreen usercontrol属性
CurrentScreen = new MaintenancePage();
CurrentScreen = new LogInPage(); // LoginPage.xaml is your login view.