C#WPF暂停处理,直到按下按钮
我决定在WPF中创建自己的对话框窗口。我让它使用一个框架并导航到框架中的WPF页面,以获得我之前创建的适当的对话框窗口。当尝试返回一个值时会出现问题。例如,如果我有“ok”和“cancel”,我希望在按下框中显示的页面上的“ok”或“cancel”时返回一个布尔值C#WPF暂停处理,直到按下按钮,c#,wpf,dialog,wait,C#,Wpf,Dialog,Wait,我决定在WPF中创建自己的对话框窗口。我让它使用一个框架并导航到框架中的WPF页面,以获得我之前创建的适当的对话框窗口。当尝试返回一个值时会出现问题。例如,如果我有“ok”和“cancel”,我希望在按下框中显示的页面上的“ok”或“cancel”时返回一个布尔值 //This is what I'm using to display the dialog window frame. public bool DisplayQuestion(string subject, string messa
//This is what I'm using to display the dialog window frame.
public bool DisplayQuestion(string subject, string message)
{
AlertIsUp = true;
var questionPage = new QuestionPage(AlertFrame, subject, message);
AlertFrame.Navigate(questionPage);
if (MainFrame.Content != null && MainFrame.Content.ToString() == "System.Windows.Controls.WebBrowser")
{
MainFrame.Visibility = System.Windows.Visibility.Hidden;
}
//I need it to wait until a button on the dialog frame is pressed before continuing.
return QuestionResponse;
}
所发生的事情是,它将立即返回布尔值,当然,布尔值始终为false。我需要它等待直到在页面中按下“ok”或“cancel”,然后继续返回它的值
这是页面中的代码
Frame AlertFrame { get; set; }
public bool AlertIsUp { get; set; }
public bool QuestionResponse { get; set; }
public QuestionPage(Frame alertFrame, string subject, string message)
{
InitializeComponent();
theMesssage.Content = message;
subjectLabel.Content = subject;
AlertFrame = alertFrame;
AlertIsUp = MainWindow.AlertIsUp;
QuestionResponse = MainWindow.QuestionResponse;
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
AlertFrame.Content = null;
AlertIsUp = false;
QuestionResponse = false;
}
private void OkButton_Click(object sender, RoutedEventArgs e)
{
AlertFrame.Content = null;
AlertIsUp = false;
QuestionResponse = true;
}
当然,如果我只是添加While(AlertIsUp),那么如果冻结GUI。
很可能是因为我没有接受过任何正式的C#培训,所以我做事很落后。感谢您对我在本网站上的第一篇帖子的热情回复。您可以在对话框
窗口中创建一个委托
,并使用创建和显示该委托的相同方法附加一个处理程序。然后,您可以在单击按钮时调用委托,启动类将被调用。然后,您将知道该值并能够关闭窗口
如果您不了解委托
s,那么您肯定应该阅读MSDN上的页面,以帮助您理解此解决方案。你可以这样做:
在对话框窗口中
:
public void delegate Response(bool response);
public Response OnButtonClick { get; set; }
DialogWindow dialogWindow = new DialogWindow();
dialogWindow.OnButtonClick += OnButtonClick;
dialogWindow.Show();
然后在启动对话框窗口的代码中
:
public void delegate Response(bool response);
public Response OnButtonClick { get; set; }
DialogWindow dialogWindow = new DialogWindow();
dialogWindow.OnButtonClick += OnButtonClick;
dialogWindow.Show();
更新>>>
对不起,忘了给你看关键部分。单击按钮
时,对话框窗口
调用代理
:
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
AlertFrame.Content = null;
AlertIsUp = false;
QuestionResponse = false;
if (OnButtonClick != null) OnButtonClick(QuestionResponse);
}
private void OkButton_Click(object sender, RoutedEventArgs e)
{
AlertFrame.Content = null;
AlertIsUp = false;
QuestionResponse = true;
if (OnButtonClick != null) OnButtonClick(QuestionResponse);
}
当然,您实际上不太需要QuestionResponse
属性,也可以在QuestionResponse委托中轻松返回true
或false
。调用后,处理程序将获得响应
关于您对未使用不同的窗口的评论,它与委托
s没有什么区别,它的工作原理是一样的。您可以在完全没有UI的情况下使用它们。我在这里找到了解决此问题的方法:
解决方案最后放置了以下短代码:
while (AlertIsActive)
{
if (this.Dispatcher.HasShutdownStarted ||
this.Dispatcher.HasShutdownFinished)
{
break;
}
this.Dispatcher.Invoke(
DispatcherPriority.Background,
new ThreadStart(delegate { }));
Thread.Sleep(20);
}
由于我已经完成的所有其他工作都是围绕着页面构建的,所以它刚刚就位。我真的很喜欢它与其他样式化GUI相适应的方式。这是我遇到的唯一障碍。当然,如果不能做到这一点,那么我可能不得不考虑使用模态对话解决方案来接近某个对象,但我无法想象这是不可能的。感谢您的回复,尽管我不确定它是否适用。这未使用DialogWindow,未使用单独的窗口。所有这些都在一个窗口中,其中一个框架导航到该对话框的WPF页面。不过,答案可能在某个代表身上。我会仔细阅读你提供的信息,看看我能想出什么。是的,对不起,我忘了给你看关键部分。。。我现在就编辑它。