代码之家  ›  专栏  ›  技术社区  ›  ZombieGoose

窗ShowDialog返回时失败

  •  0
  • ZombieGoose  · 技术社区  · 8 年前

    我有一个自定义输入对话框,它请求用户名和在应用程序中执行特定操作的原因。用户点击主窗口上的一个按钮,对话框就会显示出来。

    然后用户输入他/她的名字和原因并单击ok。然后对话框关闭,但我(程序)从未收到答案。这是我的输入对话框的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
    

    寻找解决方案已经有一段时间了,我正要换工作

    1 回复  |  直到 8 年前
        1
  •  1
  •   Krzysztof Bracha    8 年前

    我的猜测是问题不在于代码,代码似乎很好,而在于您的 if 陈述

    Debug

    我猜你是在分配变量 operatorName reasonForForced 但是它们在程序的其他任何地方都没有使用,因此整个 如果 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");
        }
    }
    
    推荐文章