WinUI3: Unable to access UIElement defined programatically inside a event delegate function

Harshithraj1871 1,516 Reputation points
2022-10-20T07:29:51.407+00:00

Hi,

I'm trying to build a Winui3 screen programmatically with cpp.

This is my Mainwindow file

namespace winrt::TestWinUI::implementation  
{  
struct MainWindow : MainWindowT<MainWindow>  
{  
    MainWindow();  

    int32_t MyProperty();  
    void MyProperty(int32_t value);  

    void ComboxSelectionChanged(IInspectable const& sender, RoutedEventArgs const& args);  

    Window      window;  
    StackPanel  line;  
    ComboBox combobox1;  
    ComboBox combobox2;  
}  

MainWindow constructor

 MainWindow::MainWindow(){  
      combobox1.PlaceholderText(L"Select Country");  
      combobox1.Items().Append(box_value(L"India"));  
      combobox1.SelectionChanged({ this,&MainWindow::ComboxSelectionChanged });  
      line.Children().Append(combobox1);  
      window.Content(line);  
      window.Activate();  
 }  

This is my App file

void App::OnLaunched(Microsoft::UI::Xaml::LaunchActivatedEventArgs const&)  
{  
make<MainWindow>();  
}  

Here I was able to access all the UIElements[like combobox1] of MainWindow in MainWindow() constructor and was able to get the screen programmatically. I have assigned a ComboxSelectionChanged event[defined inside MainWindow] when item selection is changed in combobox1. And I got the event when the item selection was changed in combobox1. But when I tried to access combobox2 inside the ComboxSelectionChanged() event,
252328-image.png

I'm getting a runtime error like this

252275-image.png

I would be of great help if you could help me with this issue

Universal Windows Platform (UWP)
Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
752 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,636 questions
{count} votes