C# 我如何选择;“启动对象”;当列表框为空时,是否在项目属性中?
我有一个WPF项目,我尝试使用Dale Ragan描述的Microsoft.VisualBasic dll配方将其制作成一个单实例应用程序 在Visual Studio 2013中使用Framework 4.5执行此操作时,在编译时会出现两个相同的错误:“…为每个入口点定义了多个入口点…”。然后我想我会在项目属性的“应用程序”选项卡的“启动对象”项的组合框选项中看到这两个入口点。但它是空的。为什么“启动对象”组合框为空,以及如何设置入口点?这可能是微软的错误吗 其他信息:C# 我如何选择;“启动对象”;当列表框为空时,是否在项目属性中?,c#,wpf,startup,entry-point,project-properties,C#,Wpf,Startup,Entry Point,Project Properties,我有一个WPF项目,我尝试使用Dale Ragan描述的Microsoft.VisualBasic dll配方将其制作成一个单实例应用程序 在Visual Studio 2013中使用Framework 4.5执行此操作时,在编译时会出现两个相同的错误:“…为每个入口点定义了多个入口点…”。然后我想我会在项目属性的“应用程序”选项卡的“启动对象”项的组合框选项中看到这两个入口点。但它是空的。为什么“启动对象”组合框为空,以及如何设置入口点?这可能是微软的错误吗 其他信息: -带有入口点的两个文件
-带有入口点的两个文件是“App.g.cs”(自动生成)和我新定义的带有入口点的类-main:“EntryPoint.cs”如果不查看代码,很难预测。但是,请确保涵盖以下几点 创建一个派生自Microsoft.VisualBasic.ApplicationServices的类。
WindowsFormsApplicationBase
,并使用它包装您的WPFSystem.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);
}
}
}
您可以按照以下步骤操作(对于入口点):
- 右键单击解决方案、属性、公共属性、启动 项目,然后在那里选择您的启动项目
- 打开app.xaml并将StartUpUri设置为主xaml文件
- 卸载WPF项目,然后编辑它李>
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");
}
希望它能帮助对不起,各位
问题消失了。
我重新启动了VisualStudio,但我有同样的行为。
我做了一个新项目,作为一个bug发送给微软,但它运行良好。
然后我从我的测试项目中复制了我的startup类,错误消失了?????????
我不明白。我通过破解csproj文件解决了这个问题:
测试程序
在我的例子中,启动对象下拉列表中的表单也丢失了。因此,我找到了另一种简单的方法来修改启动表单
Program.cs
文件static void Main()
Application.Run(newform1())代码>
到您想要的形式,如下所示:
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>
您只需重复链接中描述的配方即可。我已经做到了。问题是我没有选择作为启动对象,根据我在许多地方的阅读,它们应该在那里(2个选择,每个入口点一个)。我知道我可以做第一点,但这就是我问题的重点,“启动项目”组合框为空。关闭并重新打开解决方案使我的新主功能也出现在“启动对象”列表中!谢谢