如何在 WPF 中启动时打开两个 windows?
How do I open two windows on launch in WPF?
我觉得我在这里遗漏了一些非常基本的东西,但我似乎找不到答案。
当我的应用程序启动时,除了 MainWindow.xaml
之外,我还想打开基于现有 xaml
的第二个 window
。我发现了很多关于使用代码隐藏来创建新 window
的信息,但我想打开在另一个 xaml
文件中预定义的 window
。
两者都在使用 MahApps 并且定义为
<Controls:MetroWindow x:Class=...
...
</Controls:MetroWindow>
第二个 window
称为 ControlWindow.xaml
,与 MainWindow.xaml
位于根目录中
谢谢
编辑:
尝试在 app.xaml.cs 的 App_Startup 事件中创建和显示 window 时,即使 window 继承自相同的 class MainWindow.xaml,它没有可用的 Show() 方法。
MainWindow.xaml.cs
using MahApps.Metro.Controls;
namespace RollCallDisplayDemo
{
public partial class MainWindow : MetroWindow
{
public MainWindow()
{
InitializeComponent();
}
}
}
ControlWindow.xaml.cs
using MahApps.Metro.Controls;
namespace RollCallDisplayDemo
{
public partial class ControlWindow : MetroWindow
{
public ControlWindow()
{
InitializeComponent();
}
}
}
App.xaml.cs
using System.Windows;
using GalaSoft.MvvmLight.Threading;
namespace RollCallDisplayDemo
{
public partial class App : Application
{
void App_Startup(object sender, StartupEventArgs e)
{
MainWindow NewWindowA = new MainWindow();
ControlWindow NewWindowB = new ControlWindow();
}
static App()
{
DispatcherHelper.Initialize();
}
}
}
NewWindowA 的行为与您预期的一样,并允许创建和显示新实例。 NewWindowB 只有 InitializeComponent 方法可用,没有其他应该从 MetroWindow 继承的方法 class.
您需要调用 NewWindowB.Show() 才能使其可见。也没有必要覆盖主要 window 的创建方式,只需做这样的事情:
public partial class App : Application
{
protected override void OnActivated(EventArgs e)
{
base.OnActivated(e);
ControlWindow NewWindowB = new ControlWindow();
NewWindowB.Show();
}
}
我觉得我在这里遗漏了一些非常基本的东西,但我似乎找不到答案。
当我的应用程序启动时,除了 MainWindow.xaml
之外,我还想打开基于现有 xaml
的第二个 window
。我发现了很多关于使用代码隐藏来创建新 window
的信息,但我想打开在另一个 xaml
文件中预定义的 window
。
两者都在使用 MahApps 并且定义为
<Controls:MetroWindow x:Class=...
...
</Controls:MetroWindow>
第二个 window
称为 ControlWindow.xaml
,与 MainWindow.xaml
谢谢
编辑:
尝试在 app.xaml.cs 的 App_Startup 事件中创建和显示 window 时,即使 window 继承自相同的 class MainWindow.xaml,它没有可用的 Show() 方法。
MainWindow.xaml.cs
using MahApps.Metro.Controls;
namespace RollCallDisplayDemo
{
public partial class MainWindow : MetroWindow
{
public MainWindow()
{
InitializeComponent();
}
}
}
ControlWindow.xaml.cs
using MahApps.Metro.Controls;
namespace RollCallDisplayDemo
{
public partial class ControlWindow : MetroWindow
{
public ControlWindow()
{
InitializeComponent();
}
}
}
App.xaml.cs
using System.Windows;
using GalaSoft.MvvmLight.Threading;
namespace RollCallDisplayDemo
{
public partial class App : Application
{
void App_Startup(object sender, StartupEventArgs e)
{
MainWindow NewWindowA = new MainWindow();
ControlWindow NewWindowB = new ControlWindow();
}
static App()
{
DispatcherHelper.Initialize();
}
}
}
NewWindowA 的行为与您预期的一样,并允许创建和显示新实例。 NewWindowB 只有 InitializeComponent 方法可用,没有其他应该从 MetroWindow 继承的方法 class.
您需要调用 NewWindowB.Show() 才能使其可见。也没有必要覆盖主要 window 的创建方式,只需做这样的事情:
public partial class App : Application
{
protected override void OnActivated(EventArgs e)
{
base.OnActivated(e);
ControlWindow NewWindowB = new ControlWindow();
NewWindowB.Show();
}
}