jgraham
Topic Author
Posts: 6
Joined: 24 Sep 2014, 21:08

Forcing a XAML to load via a throw away renderer

15 Oct 2014, 19:06

Hello,

So I am attempting to load a xaml dynamically with the following line:
       
user_control_ = Noesis::Gui::LoadXaml<Noesis::Gui::FrameworkElement>(xaml_filename.c_str());
However when I try to find the children or access any nodes under this element, I get a value of 0.
NsSize numChildren = VisualTreeHelper::GetChildrenCount(user_control_);
Once I add this to my main GUI tree like this:
panel->GetChildren()->Add((Noesis::Gui::FrameworkElement*) root.GetPtr());
and then call Noesis_Update I am able to traverse the new xaml tree. Now this works fine, but it will increase the complexity of my code since I make the update and children add calls at the top of my frame. To reduce this complexity a tried using a throw away renderer as follows based on (http://www.noesisengine.com/docs/Gui.Core.SDKGuide.html):
Ptr<IRenderer> xamlRenderer = CreateRenderer(user_control_.GetPtr());
This worked well and prevents me from restructuring my code, so my question is this...Is this a safe way to force a XAML to load? I am assuming that the renderer resources will be deallocated after it goes out of scope, but the loaded XAML elements are still being kept in memory (which is what I need). Really, all I need is some form of commit call to force a complete loading of the XAML, is there a better way to do this?
 
User avatar
sfernandez
Site Admin
Posts: 3188
Joined: 22 Dec 2011, 19:20

Re: Forcing a XAML to load via a throw away renderer

16 Oct 2014, 19:44

Hi,

If you want to look for a specific element in a xaml it is better to assign a Name to it and use FindName to obtain a reference.
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Button x:Name="okBtn" Content="OK"
      HorizontalAlignment="Right"
      VerticalAlignment="Bottom"
      Margin="10"/>
</Grid>
Ptr<FrameworkElement> root = LoadXaml<FrameworkElement>(filename.c_str());
Button* okBtn = root->FindName<Button>("okBtn");
//...
If you need that the Visual Tree of your loaded xaml is totally generated, you have to add it to the panel or container, because the styles and templates used to generate that visual tree depend on the place you attach your xaml.

Once attached you can use the UpdateLayout() function to ensure that everything gets generated and correctly measured for render:
Ptr<FrameworkElement> root = LoadXaml<FrameworkElement>(filename.c_str());
//...
panel->GetChildren()->Add(root.GetPtr());
panel->UpdateLayout();
Using a temporary renderer sometimes won't be a solution because the visual elements generated will be destroyed and regenerated again when connected to the main Renderer and final templates are assigned.
 
jgraham
Topic Author
Posts: 6
Joined: 24 Sep 2014, 21:08

Re: Forcing a XAML to load via a throw away renderer

17 Oct 2014, 19:22

Thanks for the info, but it looks like UpdateLayout is not exposed to me. I must be on an older version.
 
User avatar
sfernandez
Site Admin
Posts: 3188
Joined: 22 Dec 2011, 19:20

Re: Forcing a XAML to load via a throw away renderer

20 Oct 2014, 12:00

UpdateLayout was added in 1.1.12 version. In previous versions you need to call Measure and Arrange on the panel/container hosting your loaded xaml.

Who is online

Users browsing this forum: Ahrefs [Bot] and 5 guests