我想首先显示我的登录表单(FrmLogin),如果用户正确放置他的凭据以关闭FrmLogin并显示主要表单(FrmMain),那么我执行了以下步骤:
在Application.xaml中,而不是我放置的StartupUri:
Startup="Application_Startup">
然后在Application.xaml.vb中:
Class Application
Private Sub Application_Startup(sender As Object, e As StartupEventArgs)
Dim result As New FrmLogin
result.ShowDialog()
If (result.DialogResult.HasValue And result.DialogResult.Value) Then
Dim FrmMain As New FrmMain()
Run(FrmMain)
Else
MessageBox.Show("User clicked Cancel")
'closing application...
End If
End Sub
End Class
在FrmLogin中:
Public Class FrmLogin
Private Sub btnOk_Click(sender As Object, e As RoutedEventArgs) Handles btnOk.Click
Me.DialogResult = True
End Sub
Private Sub btnCancel_Click(sender As Object, e As RoutedEventArgs) Handles btnCancel.Click
Me.DialogResult = False
End Sub
End Class
不幸的是,我在Applciation_Startup中的那些行上出错:
Dim FrmMain As New FrmMain()
Run(FrmMain)
错误消息:
其他信息:应用程序已在运行Dispatcher。
我做错了什么,我该怎么办?顺便说一句,这种方法是否正确?
答案 0 :(得分:0)
我在过去做过这个,我完成的方式如下:
修改App.xaml C#
或Application.xaml VB
:
<Application x:Class="LoginFormExample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LoginFormExample"
StartupUri="winMDI.xaml" ShutdownMode="OnExplicitShutdown" Startup="Application_Startup" >
<Application.Resources>
</Application.Resources>
Key Piece为ShutdownMode="OnExplicitShutdown"
,可防止程序在第一个窗口关闭后关闭,这将是您的登录窗口。
接下来修改App.xaml.cs,如下所示:
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
winLogin login = new winLogin();
bool? bResult = login.ShowDialog();
if (!bResult.HasValue || !bResult.Value)
{
MessageBox.Show("Failed Login");
this.Shutdown();
}
}
}
或Application.xaml.vb:
Class Application
Private Sub Application_Startup(sender As Object, e As StartupEventArgs)
Dim login As New winLogin()
Dim bResult As Nullable(Of Boolean) = login.ShowDialog()
If Not bResult.HasValue OrElse Not bResult.Value Then
MessageBox.Show("Failed Login")
Shutdown()
End If
End Sub
结束班
自ShutdownMode="OnExplicitShutdown"
以来,您需要在主窗口关闭后关闭您的应用,如下所示:
<Window x:Class="LoginFormExample.winMDI"
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"
xmlns:local="clr-namespace:LoginFormExample"
mc:Ignorable="d"
Title="winMDI" Height="300" Width="300" Closed="Window_Closed">
<Grid>
</Grid>
Key Piece = Closed="Window_Closed"
和:
C#
public partial class winMDI : Window
{
public winMDI()
{
InitializeComponent();
}
private void Window_Closed(object sender, EventArgs e)
{
Application.Current.Shutdown();
}
}
VB
Class winMDI
Private Sub Window_Closed(sender As Object, e As EventArgs)
Application.Current.Shutdown()
End Sub
结束班
希望有所帮助。
答案 1 :(得分:0)
您也可以尝试将FrmLogin.xaml设置为启动窗口。
StartupUri="FrmLogin.xaml"
在FrmLogin中,登录成功后,创建新的MainWindow和Show(),然后关闭FrmLogin。