Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 调用线程必须是STA,因为许多UI组件都需要它,发生在WPF的主函数中_C#_Wpf_Multithreading_Xaml - Fatal编程技术网

C# 调用线程必须是STA,因为许多UI组件都需要它,发生在WPF的主函数中

C# 调用线程必须是STA,因为许多UI组件都需要它,发生在WPF的主函数中,c#,wpf,multithreading,xaml,C#,Wpf,Multithreading,Xaml,此错误消息会在没有任何更改的情况下突然跳出 我使用应用程序_Startup()作为启动函数来调用MainWindow public partial class App : Application { public string FileName; private void Application_Startup(object sender, StartupEventArgs e) { //Thread nthrd = new Thread(new Thr

此错误消息会在没有任何更改的情况下突然跳出

我使用应用程序_Startup()作为启动函数来调用MainWindow

 public partial class App : Application
{
    public string FileName;
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        //Thread nthrd = new Thread(new ThreadStart(MainWindowStartupMethod));
        //nthrd.SetApartmentState(ApartmentState.STA);
        //nthrd.Start();
        //nthrd.Join();
        MainWindow wnd = new MainWindow();
public partial class MainWindow : Window
{
    public WizardSetup data = new WizardSetup();
    public Connectiontest ConnectionObj;
    public delegate void ZoneFocusContentUpdateEventHandler(object sender, TextChangedEventArgs e);
    WizardProcessBar Processor;
    Point InitialPosition = new Point();//using for image translate
    public bool WizardToHWSucceed = false;
    public MainWindow()
错误发生在最后一行“public MainWindow()”中

我知道main()函数应该是STA,系统默认情况下,它显示在这里:

    /// <summary>
    /// Application Entry Point.
    /// </summary>
    [System.STAThreadAttribute()]
    [System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
    public static void Main() {
        TPACquisitionWizard.App app = new TPACquisitionWizard.App();
        app.InitializeComponent();
        app.Run();
    }
//
///应用程序入口点。
/// 
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute(“PresentationBuildTasks”,“4.0.0.0”)]
公共静态void Main(){
TPACquisitionWizard.App App=新建TPACquisitionWizard.App();
app.InitializeComponent();
app.Run();
}
该项目使用一个新的STA线程,该线程在第一个代码部分的注释中显示,但在主线程中显示MainWindow之前它是有效的。即使我在App.xaml中更改“StartupUri=”MainWindow.xaml“也不起作用。
因此,我的问题是如何回到使用主线程的前一种方法。基于这些评论,看起来最简单的方法是创建一个新的WPF应用程序,然后复制任何您想要保留的有用代码。您在项目的黑暗、秘密角落编写代码,我怀疑代码不需要待写

创建新项目后,请立即单击“开始”并确保其正常工作。复制代码时,请确保所做的任何更改不会导致程序停止运行

编辑:这是我在一个工作项目中从App.xaml获得的代码

<Application x:Class="MyProject.App" 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:vm="clr-namespace:MyProject.ViewModel"
             StartupUri="ControlPanel.xaml" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">

</Application>

注意StartupUri-当我单击“开始”时,我看到ControlPanel.xaml。我在这里有一个资源字典(未显示),但没有多少自定义代码。这是我的app.xaml.cs

using System.Windows;
using GalaSoft.MvvmLight.Threading;

namespace MyProject
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        static App()
        {
            DispatcherHelper.Initialize();
        }
    }
}
使用System.Windows;
使用GalaSoft.MvvmLight.Threading;
名称空间MyProject
{
/// 
///App.xaml的交互逻辑
/// 
公共部分类应用程序:应用程序
{
静态应用程序()
{
DispatcherHelper.Initialize();
}
}
}

我想这一行代码是由MvvmLight toolkit自动生成的。我没有接触过这个文件,这是一个生产项目…

有趣的是,它与exe一起工作,而不是调试模式。为什么哦,为什么新手总是做
nthrd.Start();nthrd.Join();
??让我兴奋为什么你的WPF应用程序有
Main()
无论如何???这不是WinForms。WPF有一个完全不同的启动机制,由App.xaml驱动。我希望你没有创建一个控制台应用,而是决定将WPF插入其中,因为这只会让fairys失去翅膀“有趣的是,它与exe一起工作,而不是调试模式“-这就是为什么它被称为调试器。仅仅因为某些东西似乎运行成功,并不意味着它没有在场景后面做一些不可靠的事情。首先,“public main window()”看起来像构造函数的签名——这可能与错误有关。我不确定它的目的是什么,如图所示。您看到的实际错误消息是什么?第二,若你们创建了一个新的WPF项目,并且并没有碰任何东西,你们可以点击“开始”,它运行良好——默认情况下主窗口是正确连接的。你想通过改变默认代码来实现什么?这是一个想法,但我会把它看作是最终的。step@SamXia扔掉你现在所有的东西,重新开始一个新的WPF项目。不要修改应用程序启动代码。如果public MainWindow()是一个构造函数,并且在该构造函数中引发了错误,那么错误是由于您实例化窗口的方式造成的。在app.xaml.cs中,我的WPF应用程序都没有类似的代码-请删除所有这些代码。在我的app.xaml文件中,我的应用程序声明中有一个StartupUri=“MainWindow.xaml”。我让.Net知道如何以及何时创建并打开它。我将编辑我的答案以显示一个工作的实现。设置一个断点并进行检查,但应用程序启动时将运行应用程序启动,无论您是否进行更改。。。留下您需要的任何检查代码,我怀疑您会看到,即使您将StartupUri更改回来,它也会被命中。你的UI应该在主线程上;当您将StartupUri更改为UI以外的其他内容时,可能会弄乱UI线程和工作线程之间的分隔。MainWindow wnd=new MainWindow()肯定应该删除。如果设置StartupUri,则不需要它。如果要在打开MainWindow()之前检查某些内容,那么可以在MainWindow构造函数或MainWindow的“加载”事件处理程序中进行检查。