WPF表单 - InvalidOperationException(Presentationcore.dll) [英] WPF Form - InvalidOperationException (Presentationcore.dll)

查看:78
本文介绍了WPF表单 - InvalidOperationException(Presentationcore.dll)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计,

我目前有一个非常奇怪的错误,我无法解决。我用谷歌搜索,我认为它是关于威胁安全的,但我只有一个威胁。我甚至希望代码暂停,直到用户关闭WPF窗口。



这是我的代码:

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:

static void Main(string[] args)
{
    getUserCrediatls();
}

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



这是在LoginWindow上进行的:


and this is going on at LoginWindow:

public partial class LoginWindow : Window
{
    public LoginWindow()
    {
        InitializeComponent();
    }
}



没有什么不寻常的,但他在公共LoginWindow()中打破了 System.InvalidOperationException 来自 PresentationCore.dll



我已经检查过是否存在异常情况用户界面,但是当我使用一个空的WPF窗口时,他会抛出相同的异常。



感谢阅读,并感谢您的帮助:)


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 :)

推荐答案

您好,

您可以在App.xaml文件中调用您的LoginWindow。



类似这样:StartupUri =Windows \LoginWindow.xaml



或者如果你想将LoginWindow显示为DialogWindow,你应该从 MainWindow 的<加载事件。



编辑:

您的项目中有一个App.xaml文件,因此这是您的应用程序的入口点。现在,您想要登录您的应用程序以便携带另一个表单。

因此,您必须在App类(位于App.xaml.cs)中编写LoginWindow。



你的App.xaml必须喜欢这样的东西:

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.


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:
<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>



现在在登录按钮(在LoginWindow类中)重定向登录后到你想要的课程。



希望它有所帮助。


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

Hope it helps.


我终于喜欢我的问题的解决方案,我不得不将其作为STA-Thread启动

I finally fond the Solution to my Problem, i had to launch it as STA-Thread
static void Main(string[] args)
        {
            Thread loginThread = new Thread(getUserCrediatls);
            loginThread.SetApartmentState(ApartmentState.STA);
            loginThread.Start();
            loginThread.Join();
        }




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


这篇关于WPF表单 - InvalidOperationException(Presentationcore.dll)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆