替换 WPF 入口点

发布于 11-10 07:24 字数 135 浏览 4 评论 0原文

WPF 定义了自己的 Main() 方法。我应该如何用我自己的 Main 方法替换它(通常)打开 WPF MainWindow (例如通过命令行参数添加非 WPF 脚本模式) ?

WPF defines its own Main() method. How should I go about replacing it with my own Main method that (normally) opens the WPF MainWindow (e.g. to add a non-WPF scripting mode via command-line arguments)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

戏蝶舞2024-11-17 07:24:24

一些示例描述了将 App.xaml 的构建操作从 ApplicationDefinition 更改为 Page 并编写您自己的 Main() 来实例化 App code> 类并调用其 Run() 方法,但这可能会在 App.xaml 中解析应用程序范围资源时产生一些不良后果。

相反,我建议在自己的类中创建自己的 Main() ,并在项目属性中将启动对象设置为该类:

public class EntryPoint {
    [STAThread]
    public static void Main(string[] args) {
        if (args != null && args.Length > 0) {
            // ...
        } else {
            var app = new App();
            app.InitializeComponent();
            app.Run();
        }
    }
}

我这样做是为了利用一些 AppDomain< /code> 在发生任何其他事情之前必须订阅的事件(例如 AssemblyResolve)。我遇到的将 App.xaml 设置为 Page 的不良后果包括我的 UserControl 视图 (MV-VM) 在设计时无法解析 App.xaml 中保存的资源。< /em>

Some examples depict changing App.xaml's Build Action from ApplicationDefinition to Page and writing your own Main() that instantiates the App class and calls its Run() method, but this can produce some unwanted consequences in the resolution of application-wide resources in App.xaml.

Instead, I suggest making your own Main() in its own class and setting the Startup Object to that class in the project properties:

public class EntryPoint {
    [STAThread]
    public static void Main(string[] args) {
        if (args != null && args.Length > 0) {
            // ...
        } else {
            var app = new App();
            app.InitializeComponent();
            app.Run();
        }
    }
}

I do this to take advantage of some AppDomain events that must be subscribed to before anything else happens (such as AssemblyResolve). The unwanted consequences of setting App.xaml to Page that I experienced included my UserControl Views (M-V-VM) not resolving resources held in App.xaml during design-time.

冰葑2024-11-17 07:24:24

通常,我编辑 App.xaml 以添加此支持:

<Application x:Class="SomeNamespace.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         Startup="Application_Startup">

相关部分是我使用 中的事件处理程序从 StartupUri 更改为 Startup App.xaml.cs。这是一个例子:

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        int verbose = 0;
        var optionSet = new OptionSet
        {
            { "v|verbose", "verbose output, repeat for more verbosity.",   
                    arg => verbose++ }
        };

        var extra = optionSet.Parse(e.Args);
        var mainWindow = new MainWindow(verbose);
        mainWindow.Show();
    }
}

Typically I edit App.xaml to add this support:

<Application x:Class="SomeNamespace.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         Startup="Application_Startup">

The relevant part being I changed from StartupUri to Startup with an event handler in App.xaml.cs. Here is an example:

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        int verbose = 0;
        var optionSet = new OptionSet
        {
            { "v|verbose", "verbose output, repeat for more verbosity.",   
                    arg => verbose++ }
        };

        var extra = optionSet.Parse(e.Args);
        var mainWindow = new MainWindow(verbose);
        mainWindow.Show();
    }
}
中性美2024-11-17 07:24:24

伙计们
问题是你的程序有两个静态 Main() 方法,这会导致编译器抱怨;要解决此问题,请尝试以下操作之一:

  • 告诉编译器您的静态 Main() 方法应该是执行入口点 - 将项目的“启动对象”设置设置为包含静态 Main() 方法的类(右键单击在解决方案资源管理器中打开项目,选择“属性”,然后在“应用程序”选项卡下查找“启动对象”设置)。
  • 关闭 App.g.cs 的静态 Main() 方法的自动生成 - 在解决方案资源管理器中,右键单击 App.xaml,选择“属性”,然后将“生成操作”从“ApplicationDefinition”更改为“Page”。

guys
The problem is that your program has two static Main() methods, that will cause the compiler to complain between; To resolve this, try one of the following:

  • Tell the compiler that your static Main() method should be the execution entry point—Set your project’s “Startup object” setting to the class containing your static Main() method (right-click on the project in Solution Explorer, choose “Properties,” then look for the “Startup object” setting under the “Application” tab).
  • Turn off auto-generation of App.g.cs’s static Main() method—In Solution Explorer, right click on App.xaml, choose “Properties,” then change the “Build Action” from “ApplicationDefinition” to “Page”.
尾戒2024-11-17 07:24:24

使用您的自定义静态 Main 方法创建新类。在此方法的最后,只需调用 WPF 生成的原始 App.Main():

public class Program
{
    [STAThread]
    public static void Main(string[] args)
    {
        // Your initialization code
        App.Main();
    }
}

然后将项目的“启动对象”设置设置为包含静态 Main() 的类。

Create new class with your custom static Main method. At the end of this method just call original App.Main() generated by WPF:

public class Program
{
    [STAThread]
    public static void Main(string[] args)
    {
        // Your initialization code
        App.Main();
    }
}

Then set your project’s “Startup object” setting to the class containing your static Main().

抱着落日2024-11-17 07:24:24

我发布这个答案是因为上述答案都不适合我。
就我而言,StartupUri 已从 App.xaml 中删除,但我仍然收到错误。我最终做了什么我将以下代码添加到项目文件(Foo.csproj)中,它解决了问题

<ItemGroup>
    <ApplicationDefinition Remove="App.xaml" />
    <Page Include="App.xaml" />
</ItemGroup>

I am posting this answer as none of the above answers work for me.
In my case, StartupUri was removed from App.xaml and I was still getting the error. what I end up doing I added the following code to the project file (Foo.csproj) and it solved the issue

<ItemGroup>
    <ApplicationDefinition Remove="App.xaml" />
    <Page Include="App.xaml" />
</ItemGroup>
甜警司2024-11-17 07:24:24

使用自定义 Main() 可能会遇到问题,因为未设置 StartupUri。

您可以使用它在 App 类中轻松设置它(不要忘记从 App.xaml 中删除 StartupUri 并将其 Build Action 设置为 Page):

[STAThread]
static void Main()
{
    App app = new App();
    app.InitializeComponent();
    app.Run();
}

protected void OnStartup(object sender, StartupEventArgs e)
{
        var toUri = new UriTypeConverter();
        StartupUri = (Uri)toUri.ConvertFrom("MainWindow.xaml");
...
}

Using a custom Main() you might run into problems because StartupUri is not set.

You can use this to set it without headaches in your App class (Don't forget to remove StartupUri from App.xaml and set its Build Action to Page):

[STAThread]
static void Main()
{
    App app = new App();
    app.InitializeComponent();
    app.Run();
}

protected void OnStartup(object sender, StartupEventArgs e)
{
        var toUri = new UriTypeConverter();
        StartupUri = (Uri)toUri.ConvertFrom("MainWindow.xaml");
...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文