elecman
Topic Author
Posts: 72
Joined: 20 Jul 2014, 04:28

[Unity] Replace xaml at runtime

15 Nov 2017, 23:13

I want to replace an xaml file at runtime but I can't get it to work. A few things I have tried:
NoesisView panel = GetComponent<NoesisView>();
Noesis.Canvas loadedCanvas = (Noesis.Canvas)Noesis.GUI.LoadXaml("Assets/file1.xaml");
Noesis.Canvas newRoot = (Noesis.Canvas)loadedCanvas.FindName("layer1");
Noesis.Canvas originalRoot = (Noesis.Canvas)panel.Content.FindName("layer1");

//Replace current xaml with another xaml. 
//This does nothing.
originalRoot = newRoot;

//Invalid cast.
NoesisXaml loadedxaml1 = (NoesisXaml)Noesis.GUI.LoadXaml("Assets/file1.xaml");
panel.Xaml = loadedxaml1;

//Unexpected empty XAML.Please reimport again
panel.Xaml.LoadComponent(loadedCanvas);
I saw here that it has something to do with SetContent() but that function is deprecated.
viewtopic.php?f=3&t=58&hilit=runtime+load
 
User avatar
jsantos
Site Admin
Posts: 1869
Joined: 20 Jan 2012, 17:18
Contact:

Re: Replace xaml at runtime

16 Nov 2017, 11:34

You need to change the Xaml property of the view and then force a LoadXaml in the view.
Type of Xaml property is NoesisXaml. That's a Unity asset, so you need to find a way, using Unity API, to obtain it. The easiest way is by having in referenced in one of your scripts. You can also have in in a /Resources/ folder and load it using Resources.Load.
NoesisXaml xaml = (NoesisXaml)UnityEngine.Resources.Load(path, typeof(NoesisXaml));
view.Xaml = xaml;
view.LoadXaml(true);
 
elecman
Topic Author
Posts: 72
Joined: 20 Jul 2014, 04:28

Re: Replace xaml at runtime

16 Nov 2017, 13:35

Thanks. Referencing the xaml from a script by drag and dropping it on the inspector works:
//Drag and drop on the inspector here
public NoesisXaml xaml;

NoesisView panel = GetComponent<NoesisView>();			

panel.Xaml = xaml;
panel.LoadXaml(true);
This also works:
NoesisView panel = GetComponent<NoesisView>();
NoesisXaml xaml = (NoesisXaml)UnityEngine.Resources.Load("file1", typeof(NoesisXaml));
panel.Xaml = xaml;
panel.LoadXaml(true);
It might be worth mentioning that the xaml file which has been converted to an .asset file has to be placed in a folder called Assets/Resources. The file is then referenced in the Resources.Load() function without the extension. For example “Assets/Resources/file1.asset” becomes “file1”.
Last edited by elecman on 16 Nov 2017, 13:55, edited 3 times in total.
 
elecman
Topic Author
Posts: 72
Joined: 20 Jul 2014, 04:28

Re: Replace xaml at runtime

16 Nov 2017, 13:37

One other question, when I add some xaml code from another xaml file using the code below, I get this error message:
NoesisException: Child already has a logical parent
NoesisView panel = GetComponent<NoesisView>();
			
Noesis.Canvas loadedCanvas = (Noesis.Canvas)Noesis.GUI.LoadXaml("Assets/file1.xaml");
Noesis.Canvas originalRoot = (Noesis.Canvas)panel.Content.FindName("layer1");
Noesis.Canvas newRoot = (Noesis.Canvas)loadedCanvas.FindName("layer1");

//Place new xaml content in current xaml.
originalRoot.Children.Add(newRoot);
What is the proper way to insert xaml code?
 
User avatar
jsantos
Site Admin
Posts: 1869
Joined: 20 Jan 2012, 17:18
Contact:

Re: Replace xaml at runtime

16 Nov 2017, 13:58

Framework elements cannot be inserted several times in the tree, they can only be located at one place, have only a single parent. That's what happening in your code. The recommended way to do this is just moving the entire XAML under the root. Something like this:
NoesisView panel = GetComponent<NoesisView>();
Noesis.Canvas root = (Noesis.Canvas)panel.Content.FindName("root");
			
Noesis.Canvas xaml = (Noesis.Canvas)Noesis.GUI.LoadXaml("Assets/file1.xaml");
root.Children.Add(xaml);
 
elecman
Topic Author
Posts: 72
Joined: 20 Jul 2014, 04:28

Re: Replace xaml at runtime

16 Nov 2017, 23:25

Thanks, that did the trick :-)
 
User avatar
jsantos
Site Admin
Posts: 1869
Joined: 20 Jan 2012, 17:18
Contact:

Re: Replace xaml at runtime

20 Nov 2017, 12:32

Marking this as solved then.

Who is online

Users browsing this forum: sfernandez and 13 guests