簡體   English   中英

Window.ShowDialog失敗返回

[英]Window.ShowDialog failes on return

我有一個自定義輸入對話框,要求輸入用戶名和共振(由於原因),以便在應用程序中執行某些操作。 用戶單擊主窗口上的按鈕,對話框隨即顯示。

然后,用戶輸入他/她的姓名和原因,然后單擊“確定”。 然后對話框關閉,但我(程序)從不收到答案。 這是我的輸入對話框的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");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM