wpf 当列表框为空时,如何在项目属性中选择“启动对象”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22534805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:17:19  来源:igfitidea点击:

How can I choose the "Startup object" in project properties while the listbox is empty?

c#wpfstartupentry-pointproject-properties

提问by Eric Ouellet

I have a WPF project which I try to make it a single instance app using the recipe with Microsoft.VisualBasic dll described by Dale Ragan here at StackOverflow

我有一个 WPF 项目,我尝试使用 Dale Ragan在 StackOverflow 上描述的 Microsoft.VisualBasic dll 的配方使其成为单实例应用程序

Doing so in Visual Studio 2013 with Framework 4.5 give me 2x the same error while compiling: "... has more than one entry point defined..." for each entry point. Then I though that I would see both entry points in the comboBox choices of my "Startup Object" item of "Application" tab of my project properties. But it is empty. Why the "StartUp object" comboBox is empty and how to set the entry point? Could it be a Microsoft bug?

在带有 Framework 4.5 的 Visual Studio 2013 中这样做会在编译时给我 2 倍的相同错误:“...为每个入口点定义了多个入口点...”。然后我认为我会在我的项目属性的“应用程序”选项卡的“启动对象”项的组合框选项中看到两个入口点。但它是空的。为什么“启动对象”组合框为空,如何设置入口点?会不会是微软的bug?

Additional information: - The 2 files with entry points are "App.g.cs" (auto generated) and my newly defined class with entry point - main : "EntryPoint.cs"

附加信息: - 带有入口点的 2 个文件是“App.g.cs”(自动生成)和我新定义的带有入口点的类 - main :“EntryPoint.cs”

回答by Kai Neugebauer

I've solved the problem via hacking the csproj - file:

我已经通过破解 csproj - 文件解决了这个问题:

  <PropertyGroup>
    <StartupObject>Test.Program</StartupObject>
  </PropertyGroup>

回答by Eric Ouellet

Sorry folks,

对不起各位,

The problem disappeared. I restarted Visual Studio but I had same behavior. I made a new project to send to Microsoft as a bug but it was working fine. I then copied my startup class from my test project and the bug disappeared ????????? I don't understand.

问题消失了。我重新启动了 Visual Studio,但我有相同的行为。我制作了一个新项目作为错误发送给微软,但它运行良好。然后我从我的测试项目中复制了我的启动类,错误消失了?????????我不明白。

回答by Christian Amado

You can follow these steps (For an EntryPoint):

您可以按照以下步骤操作(对于 EntryPoint):

  • Right Click in your solution, Properties, Common Properties, Startup Project, and select your Startup Project there.
  • Open your app.xaml and set the StartUpUri to your Main XAML file.
  • Unload your WPF project, and after that Edit it!
  • 右键单击您的解决方案、属性、通用属性、启动项目,然后在那里选择您的启动项目。
  • 打开您的 app.xaml 并将 StartUpUri 设置为您的主 XAML 文件。
  • 卸载您的 WPF 项目,然后编辑它!

In App.xaml.cs file you can put these lines of code:

在 App.xaml.cs 文件中,您可以放置​​以下代码行:

using System.Diagnostics;
...
Process[] activeProcess = Process.GetProcessByName(Process.GetCurrentProcess().ProcessName);
if (activeProcess.Length == 1)
{
    Application.Run(new YOUR_MAIN_XAML_CLASS_HERE());
}
else
{
    MessageBox.Show("You already have an instance of this program");
}

Hope it helps

希望能帮助到你

回答by Nair

It's hard to predict without reviewing the code. However, ensure following points are covered.

不查看代码就很难预测。但是,请确保涵盖以下几点。

Create a class that derives from Microsoft.VisualBasic.ApplicationServices. WindowsFormsApplicationBase, and use it to wrap your WPF System.Windows.Application. The wrapper is initialized by supplying your own implementation of Main.

创建一个派生自Microsoft.VisualBasic.ApplicationServices. WindowsFormsApplicationBase,并使用它来包装您的 WPF System.Windows.Application。包装器通过提供您自己的Main.

namespace SingleInstanceNamespace
{
    using System;
    using System.Windows;
    using Microsoft.VisualBasic.ApplicationServices;

    public class SingleInstanceManager : WindowsFormsApplicationBase
    {
        public SingleInstanceManager()
        {
            this.IsSingleInstance = true;
        }

        protected override bool OnStartup(
            Microsoft.VisualBasic.ApplicationServices.StartupEventArgs eventArgs)
        {
            base.OnStartup(eventArgs);
            App app = new App(); //Your application instance
            app.Run();
            return false;
        }

        protected override void OnStartupNextInstance(
            StartupNextInstanceEventArgs eventArgs)
        {
            base.OnStartupNextInstance(eventArgs);
            string args = Environment.NewLine;
            foreach (string arg in eventArgs.CommandLine)
                {
                args += Environment.NewLine + arg;
                }
            string msg = string.Format("New instance started with {0} args.{1}",
            eventArgs.CommandLine.Count,
            args);
            MessageBox.Show(msg);
        }
    }
}

The next code block details the content of the App.csfile where the application's main entry point is defined:

下一个代码块详细说明了App.cs定义应用程序主入口点的文件的内容:

namespace SingleInstanceNamespace
{
    public class MyApp
    {
        [STAThread]
        public static void Main(string[] args)
        {
            //Create our new single-instance manager
            SingleInstanceManager manager = new SingleInstanceManager();
            manager.Run(args);
        }
    }
}

回答by user8581607

In my case, the forms were missing too from the Startup object dropdown list. So, I found another easey way to modify the startup form.

在我的情况下,启动对象下拉列表中也缺少表单。所以,我找到了另一种修改启动表单的简单方法。

  1. Open the Program.csfile on Code View
  2. Go to the static void Main()
  3. Modify the line that looks like the following:
  1. Program.cs在代码视图中打开文件
  2. 前往 static void Main()
  3. 修改如下所示的行:

Application.Run(new Form1());

Application.Run(new Form1());

To your desired Form, like this:
Application.Run(new BootloaderDialog());

到你想要的表格,像这样:
Application.Run(new BootloaderDialog());

Hope this helps!

希望这可以帮助!

回答by Kenneth Yang

In my case, I spelled entry name Main() to main(), then this entry not added into startup object list.When I changed it to Main(), the entry was added into startup object list. So be careful case sensitive.

就我而言,我将条目名称 Main() 拼写为 main(),然后该条目未添加到启动对象列表中。当我将其更改为 Main() 时,该条目已添加到启动对象列表中。所以要小心区分大小写。