WinUI3: Unable to access UIElement defined programatically inside a event delegate function
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,
I'm getting a runtime error like this
I would be of great help if you could help me with this issue