В UWP как отобразить окно сообщения из синхронной функции (т.е. не асинхронно) - PullRequest
0 голосов
/ 04 мая 2018

Есть ли способ обернуть асинхронную функцию UWP для создания диалоговых окон таким образом, чтобы их можно было вызывать из обычного метода без ключевого слова async? Пример:

var msgbox = new ContentDialog
             {
                   Title   = "Error",
                   Content = "Already at the top of the stack",
                   CloseButtonText = "OK"
             };
             await msgbox.ShowAsync();

Ответы [ 3 ]

0 голосов
/ 05 мая 2018

Нет, в данный момент нет способа сделать это. Если вы попытаетесь заблокировать ожидание закрытия диалога, ваше приложение будет заблокировано.

0 голосов
/ 05 мая 2018

Вот как я это делаю (я видел это в какой-то живой демонстрации, когда только что был представлен UWP):

var msgbox = new ContentDialog
             {
                   Title   = "Error",
                   Content = "Already at the top of the stack",
                   CloseButtonText = "OK"
             };
             var ignored = msgbox.ShowAsync();

Это работает, как и ожидалось, в не асинхронном пустом методе.

0 голосов
/ 04 мая 2018
public Task<ContentDialogResult> MsgBox(string title, string content)
{
    Task<ContentDialogResult> X = null;

    var msgbox = new ContentDialog
    {
        Title = title,
        Content = content,
        CloseButtonText = "OK"
    };

    try
    {
        X = msgbox.ShowAsync().AsTask<ContentDialogResult>();
        return X;
    }
    catch { 
        return null;
    }
}

private void B1BtnBack_Click(object sender, RoutedEventArgs e)
{
    MsgBox("Beep", "Already at the top of stack");
    return; 

    // ^^^ Careful here. MsgBox returns with an active task
    // running to display dialog box.  This works because
    // the next statement is a return that directly 
    // returns to the UI message loop.  And the 
    // ContentDialog is modal meaning it disables 
    // the page until ok is clicked in the dialog box.
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...