簡體   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