C++ XAML 使用控件作为函数参数

C++ XAML use control as function argument

本文关键字:函数 参数 控件 XAML C++      更新时间:2023-10-16

为了简化向UI::XAML::TextBlock添加文本的过程,我编写了一个函数:

public ref class MainPage sealed
{
public:
    MainPage();
private:
    void add_to_console(TextBlock^ text_block, String^ str)
    {
        text_block->Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler(
            [text_block, str]()
        {
            text_block->Text += str;
        }));
    }
};

但是,它会产生很多错误:

c2061 - syntax error - identifier TextBlock,

c2065 - undeclared identifier text_block.

我怎样才能编写这个函数才能使用任何文本块?

似乎

在参数列表中我需要放置一个平台::Object^ obj,然后

Windows::UI::Xaml::Controls::TextBlock^ text_block = reinterpret_cast<Windows::UI::Xaml::Controls::TextBlock^>(obj);

随心所欲地使用它