Page 1 of 1
Noesis::GUI::LoadXaml, SetContent and FindName not working as expected
Posted: 23 Mar 2022, 19:24
by dazza
I have a MainWindow class derived from Noesis::ContentControl.
I call
auto content = Noesis::GUI::LoadXaml(...);
mainwindow->SetContent(content)
I then later call
mainwindow->FindName("WidgetName")
and it is always nullptr.
The Xaml contains a widget with
What am I doing wrong?
Re: Noesis::GUI::LoadXaml, SetContent and FindName not working as expected
Posted: 24 Mar 2022, 12:13
by dazza
Further to this, nothing seems to have a name:
auto filter = [](Noesis::Visual* target) -> Noesis::HitTestFilterBehavior { return Noesis::HitTestFilterBehavior::HitTestFilterBehavior_Continue; };
auto window = sState.mApplication->GetMainWindow();
Noesis::HitTestResultCallback resultcb = [](const Noesis::HitTestResult& result) -> Noesis::HitTestResultBehavior
{
auto felem = Noesis::DynamicCast<Noesis::FrameworkElement*>(result.visualHit);
if(felem)
{
auto classname = felem->GetClassType()->GetName();
ZU_Info(zu::LoggingSystem::UI) << "FELEM: " << (unsigned long long)felem << felem->GetName() << " " << classname;
}
return Noesis::HitTestResultBehavior::HitTestResultBehavior_Continue;
};
Noesis::Point point = Noesis::VisualTreeHelper::GetRoot(window)->PointFromScreen(Noesis::Point(mousex, mousey));
Noesis::VisualTreeHelper::HitTest(Noesis::VisualTreeHelper::GetRoot(window), point, filter, resultcb);
GetName always returns an empty string. I'm confused, multiple elements in the source XAML have x:Name attributes.
Re: Noesis::GUI::LoadXaml, SetContent and FindName not working as expected
Posted: 24 Mar 2022, 23:43
by sfernandez
Every named element in a xaml will be registered in the xaml's root namescope. In the code you attached this will be the namescope of the 'content' object. So you will be able to find the named elements by doing the FindName on that object:
auto content = Noesis::GUI::LoadXaml(...);
mainwindow->SetContent(content)
...
content->FindName("WidgetName");
Re: Noesis::GUI::LoadXaml, SetContent and FindName not working as expected
Posted: 25 Mar 2022, 15:51
by dazza
Thankyou, this worked but is very surprising. My mental model of how XAML is used is clearly very wrong.
Re: Noesis::GUI::LoadXaml, SetContent and FindName not working as expected
Posted: 28 Mar 2022, 22:14
by jsantos
One of the reasons behind this behavior is for example, UserControls. Instancing a UserControl several times would create name conflicts if the UserControl contains x:Name inside.