簡體   English   中英

C#WPF暫停處理,直到按下按鈕

[英]C# WPF Pause processing until button has been pressed

我決定在WPF中創建自己的對話框窗口。 我使用框架來工作,並導航到框架中的WPF頁面以獲取我之前制作的相應對話框窗口。 嘗試返回值時出現問題。 例如,如果我有“ ok”和“ cancel”,則我想在框架中顯示的頁面上按“ ok”或“ cancel”時返回一個布爾值。

//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。 我需要它等待頁面中按下“確定”或“取消”,然后繼續返回它的值。

這是頁面中的代碼。

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#的任何正式培訓,所以我很可能在做事落后。 感謝您對我在本網站上的第一篇文章的友好答復。

我實際上在這里找到了解決此問題的方法:

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);
    }

您可以在對話框Window創建delegate ,並使用與創建和顯示delegate的方法相同的方法附加處理程序。 然后,您可以在單擊Button時調用委托,然后將調用啟動類。 然后,您將知道該值並能夠關閉Window

如果您不了解delegate那么您絕對應該閱讀MSDN上的委托(C#編程指南)頁面以幫助您了解此解決方案。 您可以執行以下操作:

在對話框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評論,它與delegate s幾乎沒有區別,它的工作原理相同。 當完全不涉及UI時,您可以使用它們。

暫無
暫無

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

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