C ++ WinRT - Возможно получить доступ к элементу управления XAML TextBlock в потоке, созданном через std :: thread? - PullRequest
0 голосов
/ 10 мая 2019

У меня есть программа C ++ / WinRT XAML с функцией _tproc (p) , где p равно MainPage * p , кратко.

Процесс компиляции в порядке, но не удалось, если вызов через создание std :: thread.

Вот исходный код:

MainPage.H

struct MainPage : MainPageT<MainPage>
{
    TextBlock           m_tbx;
    TranslateTransform  m_pos;
    int                 m_x;
    int                 m_y;
    int                 m_itest;

    MainPage();
    int32_t MyProperty  ();
    void    MyProperty  (int32_t value); 
    int     f_iOnInit   ();
};

MainPage.cpp

void _tproc ( winrt::WINRT_DEMO::implementation::MainPage* p = nullptr) 
{
    if (p == nullptr) return;
    auto x{ 500 };
    auto y{ 500 };

    // NOTE: Instructions below caused error if _tproc() is 
    //       called via std::thread  
    p->m_pos.X(x); 
    p->m_pos.Y(y); 
    p->m_tbx.RenderTransform(p->m_pos); 
}

MainPage::MainPage()
{
    InitializeComponent();
    f_iOnInit();
}

int32_t MainPage::MyProperty()
{
    throw hresult_not_implemented();
}

void MainPage::MyProperty(int32_t /* value */)
{
    throw hresult_not_implemented();
}

int MainPage::f_iOnInit ()
{
    m_tbx.FontFamily( Windows::UI::Xaml::Media::FontFamily( L"Segoe UI Semibold" ) );
    m_tbx.FontSize(24.0);
    m_tbx.Foreground( SolidColorBrush( Colors::Orange() ) );
    m_tbx.VerticalAlignment( VerticalAlignment::Center );
    m_tbx.HorizontalAlignment( HorizontalAlignment::Center );
    m_tbx.TextAlignment( TextAlignment::Left );              
    m_tbx.Text( L"Hello World!" );

    this->Content().as<Panel>().Children().Append( m_tbx );

    // Calling _tproc() below is fine.
    //_tproc( this ); 

    // Error occurred, explained within _tproc()
    std::thread t( _tproc, this ); 
    t.join();

    return 0;
}

Для справок: страница ошибок времени выполнения фиксируется ЗДЕСЬ .

Пожалуйста, сообщите.

...