首页 > 解决方案 > C#如何获得应用按钮以保持窗口打开?

问题描述

我使用this.Showfrom Main() 显示此窗口并动态添加内容,但窗口在打开后立即关闭。因此,我使用this.ShowDialog并且我想编写一个应用按钮,以允许用户在不关闭窗口的情况下测试参数。接受或取消关闭窗口。

这是代码:

ShowMyWindow()
{
    Button applyButton = new Button();
    applyButton.Content = "Apply";
    applyButton.Click += new RoutedEventHandler((obj, e) =>
    {
        DialogResult = true;
    });
    stackPanelEight.Children.Add(applyButton);

    Button acceptButton = new Button();
    acceptButton.Content = "Accept";
    acceptButton.Click += new RoutedEventHandler((obj, e) =>
    {
        DialogResult = true;
        this.Close();
    });
    stackPanelEight.Children.Add(acceptButton);

    Button cancelButton = new Button();
    cancelButton.Content = "Cancel";
    cancelButton.Click += new RoutedEventHandler((obj, e) =>
    {
        DialogResult = false;
        this.Close();
    });
    stackPanelEight.Children.Add(cancelButton);

    return (bool)ShowDialog();
}

标签: c#xaml

解决方案


我意识到我的主窗口是 Window 的子类,所以我不需要使用ShowDialog; app.Run为我做的。我试图通过在我的 Main 中调用 bool 方法来太花哨。

这是我为解决我的问题所做的事情:

applyButton.Click += new RoutedEventHandler((object sender, RoutedEventArgs e) =>
{
    CollectThePolygon();
    ProduceZmathCode();
});

acceptButton.Click += new RoutedEventHandler((object sender, RoutedEventArgs e) =>
{
    CollectThePolygon();
    ProduceZmathCode();
    Close();
    Environment.Exit(1);
});

cancelButton.Click += new RoutedEventHandler((object sender, RoutedEventArgs e) =>
{
    Close();
    Environment.Exit(1);
});

推荐阅读