Window.ShowDialog 在返回时失败



我有一个自定义输入对话框,该对话框请求用户名和 reson(因为原因(在我的应用程序中执行特定操作。用户单击主窗口上的按钮,对话框就会显示,如下所示。

然后,用户输入他/她的姓名和原因,然后单击确定。然后对话框关闭,但我(程序(从未收到答案。下面是输入对话框的 XAML:

<Window x:Class="Sequence_Application_2.GUI.ForcedRackInput"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="Forcera Rack" Height="300" Width="300"
    WindowStartupLocation="CenterScreen">

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="21*"/>
        <ColumnDefinition Width="274*"/>
    </Grid.ColumnDefinitions>
    <TextBox Name="OperatorNameText"  HorizontalAlignment="Left" Height="23" Margin="15,36,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2"/>
    <Label x:Name="label" Content="Namn:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Grid.ColumnSpan="2"/>
    <Label x:Name="label1" Content="Orsak:" HorizontalAlignment="Left" Margin="10,72,0,0" VerticalAlignment="Top" Grid.ColumnSpan="2"/>
    <Border BorderThickness="1" BorderBrush="Black" Grid.ColumnSpan="2" Margin="0,0,0,0.5">
        <TextBox Name="ReasonText" Margin="15,98,15,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="116" />
    </Border>
    <Button Name="OkButton"    IsDefault="True" Content="OK" Click="OkButtonPressed"  HorizontalAlignment="Left" Margin="26.202,233,0,0" VerticalAlignment="Top" Width="75" Cursor="Arrow" Grid.Column="1"/>
    <Button Name="CancelButton" IsCancel="True" Content="Avbryt" Margin="152.202,233,47,0" VerticalAlignment="Top" Cursor="Arrow" Grid.Column="1"/>
</Grid>
</Window>

这是"背后的代码":

namespace Sequence_Application_2.GUI
{
using System.Windows;
public partial class ForcedRackInput : Window
{
    public string OperatorName { get { return OperatorNameText.Text; }  }
    public string ForcedRackReason { get { return ReasonText.Text; } }
    public ForcedRackInput()
    {
        InitializeComponent();
    }
    private void OkButtonPressed(object sender, RoutedEventArgs e)
    {
        this.DialogResult = true;
    }

}
}

这就是我调用代码的方式(来自模型,而不是"窗口类"(

public void ForceClosingRack(Flow forcedFlow)
    {
        var forcedRackWindow = new ForcedRackInput();
        string operatorName = "";
        string reasonForForced = "";
        if( forcedRackWindow.ShowDialog() == true)
        {
            operatorName = forcedRackWindow.OperatorName;
            reasonForForced = forcedRackWindow.ForcedRackReason;
        }
    } // code jumps from "if(forcedRackWindow.... to this line when ok is clicked in the dialog

寻找解决方案已经有一段时间了,我即将改变职业

感谢您抽出宝贵时间

我的猜测是,问题不在于代码,这似乎很好,而在于您的if语句。

当您在Debug模式下运行程序时,它应该按预期工作。

我的猜测是,您正在if语句中分配变量operatorNamereasonForForced,但它们没有在程序的其他任何地方使用,因此编译器会忽略整个if语句,并且在Release模式下运行时不存在。

代码中的一个小修改,根据变量值嵌入不同的行为,可以证明我的猜测:

private void Button_Click(object sender, RoutedEventArgs e)
{
    var forcedRackWindow = new ForcedWindow();
    string operatorName = "foo";
    string reasonForForced = "foo";
    if (forcedRackWindow.ShowDialog() == true)
    {
        operatorName = forcedRackWindow.OperatorName;
        reasonForForced = forcedRackWindow.ForcedRackReason;
    }
    if(!operatorName.Equals(reasonForForced))
    {
        MessageBox.Show("We are not the same");
    }
}

最新更新