我决定在WPF中创建自己的对话窗口。我使用框架工作并导航到框架中的WPF页面,以获得我以前制作的相应对话窗口。尝试返回值时出现问题。例如,如果我有“确定”和“取消”,我希望在帧中显示的页面上按'ok'或'cancel'时返回一个布尔值。C#WPF暂停处理,直到按下按钮
//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;
}
会发生什么是会立即返回布尔值,当然这总是错误的。我需要等到页面内的“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#中接受任何正式培训,所以我很可能会后退。感谢您对本网站上的第一篇文章的回应。
因为一切我已经完成已经围绕建它只是落在原地。我非常喜欢它与其他程式化GUI的适用方式。这真的是我碰到的唯一障碍。当然,如果做不到这一点,那么我可能不得不使用模态对话解决方案来获得足够近的东西,但我无法想象这是不可能的。 – 2014-09-03 21:05:07