dazza
Topic Author
Posts: 14
Joined: 10 Jan 2022, 20:41

Noesis::GUI::LoadXaml, SetContent and FindName not working as expected

23 Mar 2022, 19:24

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
x:Name="WidgetName".
What am I doing wrong?
 
dazza
Topic Author
Posts: 14
Joined: 10 Jan 2022, 20:41

Re: Noesis::GUI::LoadXaml, SetContent and FindName not working as expected

24 Mar 2022, 12:13

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.
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: Noesis::GUI::LoadXaml, SetContent and FindName not working as expected

24 Mar 2022, 23:43

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");
 
dazza
Topic Author
Posts: 14
Joined: 10 Jan 2022, 20:41

Re: Noesis::GUI::LoadXaml, SetContent and FindName not working as expected

25 Mar 2022, 15:51

Thankyou, this worked but is very surprising. My mental model of how XAML is used is clearly very wrong.
 
User avatar
jsantos
Site Admin
Posts: 4823
Joined: 20 Jan 2012, 17:18
Contact:

Re: Noesis::GUI::LoadXaml, SetContent and FindName not working as expected

28 Mar 2022, 22:14

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.

Who is online

Users browsing this forum: Bing [Bot] and 6 guests