繁体   English   中英

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

[英]How can I choose the “Startup object” in project properties while the listbox is empty?

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

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

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

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

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

对不起各位,

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

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

  • 右键单击您的解决方案、属性、通用属性、启动项目,然后在那里选择您的启动项目。
  • 打开您的 app.xaml 并将 StartUpUri 设置为您的主 XAML 文件。
  • 卸载您的 WPF 项目,然后编辑它!

在 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");
}

希望能帮助到你

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

创建一个派生自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);
        }
    }
}

下一个代码块详细说明了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);
        }
    }
}

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

  1. 在代码视图中打开Program.cs文件
  2. 转到static void Main()
  3. 修改如下所示的行:

Application.Run(new Form1());

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

希望这可以帮助!

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

我希望“MyMainWindow”成为“MyProject”项目的起始元素。

在 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:local="clr-namespace:MyProject"
         StartupUri="MyMainWindow.xaml">
<Application.Resources>
     
</Application.Resources>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM