XAML 是否可以更新ContentDialog的content(text)属性?

lskq00tm  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(113)

我需要在ContentDialog显示后更新其中的文本。
这可能吗?

  • 谢谢-谢谢
eufgjt7s

eufgjt7s1#

这可能吗?
否,呼叫ContentDialogShowAsync会传回Task,直到使用者关闭对话方块后才会完成。
如果需要在对话框显示后更改其中的文本,则应考虑使用自己的自定义对话框窗口和数据绑定,而不是使用ContentDialog API。

eoxn13cs

eoxn13cs2#

你当然可以!
我是这样做的!不幸的是,我没有通过线程调用获得作为参数传递的ContentDialog。所以我决定将其存储为类变量。(m_ContentDialog)
请帮助我改进这个!

fire_and_forget MainWindow::ConnectClicked(IInspectable const& sender, RoutedEventArgs const& args)
{
    auto strong_this = get_strong();
    m_isConected = !m_isConected;
    if (m_isConected) {
        ConnectButton().IsChecked(true);

        m_devices.clear();
        m_ContentDialog.XamlRoot(MainGrid().XamlRoot());
        m_ContentDialog.RequestedTheme(m_theme);
        m_ContentDialog.Title(box_value(L"Scan Devices"));
        m_ContentDialog.Content(box_value(L"Please wait.."));
        m_ContentDialog.PrimaryButtonText(L"OK");

        thread consumer = thread(&MainWindow::InitDevicesDialog, this);
        consumer.detach();

        co_await m_ContentDialog.ShowAsync();

        co_await ui_thread;
        m_ContentDialog.Title();
        
    }
    else {
        ConnectButton().IsChecked(false);
    }
    co_return; // Exit
}

fire_and_forget MainWindow::InitDevicesDialog()
{
    auto strong_this = get_strong();
    //Log::debug(format("sdf {}", cd));
    
    co_await ui_thread;
    while (!m_ContentDialog.IsLoaded()) {
        co_await 1ms;
        co_await ui_thread;
    }

    string content = "";
    for (int i = 0; i < 8; i++) {
        auto& d = initDevice(i);
        if (d.name != "") {
            content += format("Device {}: Serial {}\n", i, d.name);
        }
        else {
            content += format("Device {}: not present \n", i);
        }
        co_await ui_thread;
        m_ContentDialog.Content(box_value(to_hstring(content)));
        m_ContentDialog.UpdateLayout();
        co_await resume_background();
    }
    co_await ui_thread;
    m_ContentDialog.Title(box_value(L"Scan Finished"));

    co_return; // Exit
}

相关问题