2014-09-03 103 views
2

我决定在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#中接受任何正式培训,所以我很可能会后退。感谢您对本网站上的第一篇文章的回应。

+0

因为一切我已经完成已经围绕建它只是落在原地。我非常喜欢它与其他程式化GUI的适用方式。这真的是我碰到的唯一障碍。当然,如果做不到这一点,那么我可能不得不使用模态对话解决方案来获得足够近的东西,但我无法想象这是不可能的。 – 2014-09-03 21:05:07

回答

3

我居然找到了解决这个问题就在这里:

http://www.codeproject.com/Articles/36516/WPF-Modal-Dialog

该解决方案最终将这个一小段代码:

while (AlertIsActive) 
    { 
     if (this.Dispatcher.HasShutdownStarted || 
      this.Dispatcher.HasShutdownFinished) 
     { 
      break; 
     } 

     this.Dispatcher.Invoke(
      DispatcherPriority.Background, 
      new ThreadStart(delegate { })); 
     Thread.Sleep(20); 
    } 
0

您可以在对话框Window中创建一个delegate,并使用创建并显示它的相同方法附加一个处理程序。然后,您可以在单击Button并调用启动类时调用该代理。您将知道价值并能够关闭Window

如果您不了解delegate,那么您应该阅读MSDN上的Delegates (C# Programming Guide)页面以帮助您理解此解决方案。你可以做这样的事情:

在对话框Window

public void delegate Response(bool response); 

public Response OnButtonClick { get; set; } 

然后在启动对话框Window代码:

DialogWindow dialogWindow = new DialogWindow(); 
dialogWindow.OnButtonClick += OnButtonClick; 
dialogWindow.Show(); 

...

public void OnButtonClick(bool response) 
{ 
    if (response) { /* Ok */ } 
    else { /* Cancel */ } 
} 

更新>>>

道歉忘记告诉你的关键部分。当点击Button,对话框Window调用delegate

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 delegate返回truefalse。一旦被调用,处理程序将获得响应。

关于您对您的评论没有使用不同的Window s,它与delegate s没什么区别,它的工作原理也是一样的。您可以在完全没有UI的情况下使用它们。

+0

谢谢你的回应,虽然我不确定它是否适用。这不是使用DialogWindow,单独的窗口没有被使用。所有这些都在一个窗口内,一个框架导航到对话框的WPF页面。尽管答案可能在某个代表中。我会阅读你提供的信息,看看我能不能拿出任何东西。 – 2014-09-03 21:19:48

+0

对不起,我忘了告诉你关键部分...我现在编辑它。 – Sheridan 2014-09-03 21:27:47