【发布时间】:2022-01-14 13:39:32
【问题描述】:
我正在尝试使用 WPF 窗口作为消息弹出窗口,该窗口将在执行任务后关闭。我看到的所有文档都说这不能用 messageBox 完成,这就是我使用 WPF 的原因。我找到了一个代码片段,它允许我打开 WPF 窗口,但它不会将应用程序推进到下一个进程。下面是我发现的最后一个代码示例,我认为它显示了承诺,但窗口没有打开 -
[STAThread]
static void Main(string[] args)
{
try
{
string filePath = "my new directory";
var popup = new PopupTest();
popup.Dispatcher.BeginInvoke
(System.Windows.Threading.DispatcherPriority.Normal,
(Action)(() =>
{
popup.Show();
}));
// Do some console application stuff
do
{
Directory.CreateDirectory(filePath);
} while (!Directory.Exists(filePath));
popup.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
}
}
cs.xaml 文件只是默认文件
/// Interaction logic for PopupTest.xaml
/// </summary>
public partial class PopupTest : Window
{
public PopupTest()
{
InitializeComponent();
}
}
我觉得这应该比我做的更简单。任何可以为我指明正确方向的东西都会受到赞赏。
【问题讨论】:
-
恕我直言,如果您想显示某种窗口,只需创建一个 WPF 应用程序。为什么要混合控制台和 ui 项目,为什么两者都需要? This may be worth 也结帐。
-
此功能(在操作完成之前弹出警告)正在包含在更大的现有应用程序中。我所包含的只是我在网上找到的一种方法。我查看了您包含的链接,
Application app = new Application (); app.Run(new Window1());打开了窗口,但不允许应用程序继续。我尝试将该行添加到它自己的线程`var popup = new PopupTest();应用程序 = 新应用程序();线程 thread1 = new Thread(()=> app.Run(popup)); thread1.Start(); ` 但这也不会打开窗口
标签: c# wpf console-application