设置启动窗体
1.修改App.xaml
App.xaml中原代码:
1 <Application x:Class="WpfApplication1.App" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 StartupUri="MainWindow.xaml"> 5 <Application.Resources> 6 7 </Application.Resources> 8 </Application>
修改为:
<Application x:Class="WpfApplication1.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication1" Startup="Application_Startup"> <Application.Resources> </Application.Resources> </Application>
2.修改App.xaml.cs
原代码:
namespace WpfApplication1 { /// <summary> /// App.xaml 的交互逻辑 /// </summary> public partial class App : Application { } }
修改为:
namespace WpfApplication1 { /// <summary> /// App.xaml 的交互逻辑 /// </summary> public partial class App : Application { private void Application_Startup(object sender, StartupEventArgs e) { WpfApplication1.Login login = new WpfApplication1.Login(); login.Show(); } } }
设置、切换主窗体
private void Btn_Login_Click(object sender, RoutedEventArgs e) { if (TB_UserName.Text == "123456" && TB_Password.Password == "123456") { MainWindow mainWindow = new MainWindow(); Application.Current.MainWindow = mainWindow; //设置主窗体 this.Close(); mainWindow.Show(); } }
显示主窗体
副窗体关闭、切换回主窗体:
private void ManageWindow_Closed(object sender, EventArgs e) { Application.Current.MainWindow.Show(); }