Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Folks,
I currently have a really weird Error i can't fix. I googled and i think its about threat safety, but I only have one Threat. I even want the Code to pause until the user closes the WPF Window.

Here's my Code:
C#
static void Main(string[] args)
{
    getUserCrediatls();
}

static void getUserCrediatls()
{
    LoginWindow login = new LoginWindow();
    login.ShowDialog();
}

and this is going on at LoginWindow:
C#
public partial class LoginWindow : Window
{
    public LoginWindow()
    {
        InitializeComponent();
    }
}

Nothing unusual but he breaks at the "public LoginWindow()" with an System.InvalidOperationException from PresentationCore.dll.

I did already checked if there is something unusual in the User Interface, but he throws the very same Exception when i use an empty WPF Window.

Thanks for reading, and thanks for help :)
Posted

Hi there,
You can call to your LoginWindow in your App.xaml file.

Something like this: StartupUri="Windows\LoginWindow.xaml"

Or if you want to show the LoginWindow as DialogWindow you should call it from the Loaded event of your MainWindow.

Edit:
You have an App.xaml file in your project, so this is the Entry Point of your application. Now, you want to login in your application in order to bring another form.
So, you must write your LoginWindow inside the App class (located in App.xaml.cs).

Your App.xaml must like something like this:
XML
<Application x:Class="YourApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              StartupUri="LoginWindow.xaml">
    
    <Application.Resources>
    </Application.Resources>
</Application>

Now in your login button(inside your LoginWindow class) redirect to your desired class after login.

Hope it helps.
 
Share this answer
 
v2
Comments
[no name] 2-Jan-15 9:09am    
Nah that doesnt Help.

The Login window will only open at the first time. Then the login creditals will get safed encrypted offline.
Christian Amado 2-Jan-15 9:12am    
ShowDialog will not work inside Main method.
[no name] 2-Jan-15 9:18am    
I tried it with login.Show() - same error
The error also happens when I use an empty WPF Form.
Christian Amado 2-Jan-15 9:37am    
See my improved answer.
[no name] 2-Jan-15 9:41am    
Yes I know how StartupUri works, but i don't want my LoginWindow as the Entry Point. The Prohram has ti start, then he checks if the sentry.bin-File exists and when it does not exists he askes the user for Username and password by showing the LoginWindow.

The LoginWindow is more or less the engry point in that code example, but that's not the Point of this Question.

My Question is:
How can i open that WPF Window without getting the Exception.
I finally fond the Solution to my Problem, i had to launch it as STA-Thread
C#
static void Main(string[] args)
        {
            Thread loginThread = new Thread(getUserCrediatls);
            loginThread.SetApartmentState(ApartmentState.STA);
            loginThread.Start();
            loginThread.Join();
        }

C#
static void getUserCrediatls()
        {
            LoginWindow login = new LoginWindow();
            login.ShowDialog();
            
        }
 
Share this answer
 
v2
Comments
Richard Deeming 12-Jan-15 15:43pm    
Wouldn't it be simpler to add the [STAThread] attribute to your Main method?
http://msdn.microsoft.com/en-us/library/system.stathreadattribute%28v=vs.110%29.aspx[^]

[STAThread]
static void Main(string[] args)
{
LoginWindow login = new LoginWindow();
login.ShowDialog();
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900