简体   繁体   English

C#WPF窗口问题

[英]C# wpf window issue

Currently running into an issue with opening a new wpf window. 当前在打开新的wpf窗口时遇到问题。 The current program sets a boolean called confirmed to false, the program then opens a new window and passes an ID to the new window where the user will be prompted to click a yes button if they can confirm the ID is their ID(if so the boolean is set to true) or no if the ID is not their ID(if so the boolean is set to false again). 当前程序将一个名为Confirmed的布尔值设置为false,然后程序打开一个新窗口并将ID传递到新窗口,如果用户可以确认ID是其ID,则系统将提示用户单击yes按钮(如果是,则布尔值设置为true),或者如果ID不是它们的ID,则为no(如果是,则布尔值再次设置为false)。 The new window will then close and return to the normal window with the new value of the boolean. 然后,新窗口将关闭,并使用布尔值的新值返回正常窗口。 An if statement then runs to check the value of the boolean, if false then a message is displayed to the user, if true the customer is then moved onto a new window. 然后运行if语句检查布尔值,如果为false,则向用户显示一条消息,如果为true,则将客户移至新窗口。

The issue I am having is the program seems to open up the new window and prompt the user as it should, however at the same time the program steps into the next if statement checking the value of boolean before the user has a change to click yes or no. 我遇到的问题是该程序似乎打开了新窗口并提示用户,但是与此同时,程序在用户进行更改以单击“是”之前,进入了下一个if语句,检查布尔值。或者没有。 So the new window will open and then the next if statement will run even though I want to wait for user input, how do I prevent this from happen, code is listed below 因此,即使我要等待用户输入,也会打开新窗口,然后运行下一个if语句,如何防止这种情况发生,代码如下

Code for opening new window and boolean check 用于打开新窗口和布尔检查的代码

bool confirmed == false;
int id = 1;
promptWindow = new promptWindow(id, confirmed);

code for new window 新窗口的代码

public(int id, bool confirmed)
{
    InitializeComponent(); 
}

private void btnYes_Click(object sender, RoutedEventArgs e)
{
    //confirms the user wants to book and returns value
    bool confirmation = true;
    return confirmation;
    this.Close; 
}

private void btnNo_Click(object sender, RoutedEventArgs e)
{
    //confirms the user doesn't want to book and returns value
    bool confirmation = false;
    this.Close();
}

Code for checking boolean value 检查布尔值的代码

if (confirmation == true)
{
    //adds new customer to customer as they have confirmed booking
    add.list(id);
}
else
{
     MessageBox.Show("Booking not added");
}

You should use events or ShowDialog() . 您应该使用eventsShowDialog()

bool confirmed = false;
int id = 1;
promptWindow = new promptWindow(id, confirmed);
bool result = promptWindow.ShowDialog();

if (result)
{
    // confirm
}
else
{
    // not confirmed
}

And your window 还有你的窗户

public(int id, bool confirmed)
{
    InitializeComponent(); 
}

private void btnYes_Click(object sender, RoutedEventArgs e)
{
    DialogResult = true;
}

private void btnNo_Click(object sender, RoutedEventArgs e)
{
    DialogResult = false;
}

The function Window.ShowDialog() will wait until the window has been closed. 函数Window.ShowDialog()将等待直到窗口关闭。 That means your code will wait in ShowDialog() line and you can check for result. 这意味着您的代码将在ShowDialog()行中等待,您可以检查结果。 Function Window.Show() open the window and continue your code. 函数Window.Show()打开窗口,然后继续执行代码。

Another way is events: declare something like that on your Window 另一种方法是事件:在Window上声明类似的内容

public event EventHandler<int> OnConfirmButton;
public event EventHandler<int> OnCancelButton;

On your buttons: 在您的按钮上:

private void btnYes_Click(object sender, RoutedEventArgs e)
{
    OnConfirmButton?.Invoke(this, this.id);
}

private void btnNo_Click(object sender, RoutedEventArgs e)
{
    OnCancelButton?.Invoke(this, this.id);
}

And you can subscribe on your call: 您可以订阅电话:

bool confirmed = false;
int id = 1;
promptWindow = new promptWindow(id, confirmed);
promptWindow.OnConfirmButton += (sender, id) =>
{
    // do something when confirm
};

promptWindow.OnCancelButton += (sender, id) =>
{
    // do something when cancel
}

promptWindow.Show();

The id on arg is equal the id opened the window. id在arg是相等的ID打开了窗户。 This is just an example. 这只是一个例子。 Actually You do not need events on that case because you just need the result of window. 实际上,您不需要这种情况下的events ,因为您只需要window的结果。 But you can use on other cases, just follow the example. 但是您可以在其他情况下使用,只需按照示例操作即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM