简体   繁体   English

WPF WindowLess消息框

[英]WPF WindowLess MessageBox

I'm attempting to convert a very small C# console application to WPF application. 我正在尝试将非常小的C#控制台应用程序转换为WPF应用程序。 It takes in some parameters, does some work, and may bring up a MessageBox. 它需要一些参数,可以完成一些工作,并可以弹出一个MessageBox。

I'm moving it from a console app to WPF because it should run invisible, unless it has an error message. 我将其从控制台应用程序移至WPF,因为它应该运行不可见,除非出现错误消息。

The hitch, so far, has been getting it to show the MessageBox. 到目前为止,一直在努力使它显示MessageBox。 The following is the really short version, that compiles, runs... but does not show the MessageBox. 以下是真正的简短版本,可以编译,运行...但是不显示MessageBox。

namespace MyApp{
  public class EntryPoint {
    [STAThread]
    public static void Main(string[] args) {
        App app = new App();
        app.Run();
        MessageBox.Show("test", "test", MessageBoxButton.YesNo, MessageBoxImage.Question);
    }
  }
}

Anyone know how to get that pesky MessageBox to show, without having a main program window? 有谁知道如何在没有主程序窗口的情况下显示该讨厌的MessageBox?

Your best bet may be to try and run the code in the application's Startup event. 最好的选择是尝试在应用程序的Startup事件中运行代码。 Keep in mind that WPF wasn't really designed to be used this way, so there may be some odd quirks. 请记住,WPF并非真正设计为以这种方式使用,因此可能会有一些奇怪的怪癖。

Start by opening "App.xaml` in the solution explorer. The root of the XAML should look something like this: 首先在解决方案资源管理器中打开“ App.xaml”。XAML的根目录应如下所示:

<Application x:Class="Configurator.Application.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:FoodSafetyConfigurator"
             xmlns:s="clr-namespace:Configurator.Application.Properties"
             StartupUri="MainForm.xaml">

Remove the StartupUri="MainForm.xaml" attribute, and replace it with Startup="App_OnStartup" . 删除StartupUri="MainForm.xaml"属性,并将其替换为Startup="App_OnStartup" That will stop WPF from displaying a window by default, and hook the startup event to a method in "App.xaml.cs" called App_Starup . 这将阻止WPF在默认情况下显示窗口,并将启动事件挂接到“ App.xaml.cs”中名为App_Starup

Now expand "App.xaml" in solution explorer, and open "App.xaml.cs"; 现在,在解决方案资源管理器中展开“ App.xaml”,然后打开“ App.xaml.cs”; this is the code behind file for "App.xaml". 这是“ App.xaml”文件的代码。 Add a method called App_OnStartup , which will be the target of the event we attached earlier. 添加一个名为App_OnStartup的方法,该方法将成为我们前面提到的事件的目标。 It should look something like this: 它看起来应该像这样:

private void App_OnStartup(object sender, StartupEventArgs e)
{
    //Your code goes here.
}

Add any code you want to run in here. 在此处添加要运行的任何代码。 Any message boxes you display with MessageBox.Show() should display correctly, but I haven't actually tested it. 您使用MessageBox.Show()显示的任何消息框均应正确显示,但我尚未进行实际测试。

I ended up going with a standard Windows Forms application, and just removed the form. 我最终使用了标准的Windows窗体应用程序,然后删除了该窗体。 Figuring out how to make WPF do this was time consuming, frustrating and ultimately a failure. 弄清楚如何使WPF做到这一点很费时间,令人沮丧,最终是失败的。 Forms may not be the new hotness, but it works. 表单可能不是新的热点,但它可以工作。

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

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