[Unity] Replace xaml at runtime
I want to replace an xaml file at runtime but I can't get it to work. A few things I have tried:
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
Code: Select all
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);
viewtopic.php?f=3&t=58&hilit=runtime+load
Re: Replace xaml at runtime
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.
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.
Code: Select all
NoesisXaml xaml = (NoesisXaml)UnityEngine.Resources.Load(path, typeof(NoesisXaml));
view.Xaml = xaml;
view.LoadXaml(true);
Re: Replace xaml at runtime
Thanks. Referencing the xaml from a script by drag and dropping it on the inspector works:
This also works:
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”.
Code: Select all
//Drag and drop on the inspector here
public NoesisXaml xaml;
NoesisView panel = GetComponent<NoesisView>();
panel.Xaml = xaml;
panel.LoadXaml(true);
Code: Select all
NoesisView panel = GetComponent<NoesisView>();
NoesisXaml xaml = (NoesisXaml)UnityEngine.Resources.Load("file1", typeof(NoesisXaml));
panel.Xaml = xaml;
panel.LoadXaml(true);
Last edited by elecman on 16 Nov 2017, 13:55, edited 3 times in total.
Re: Replace xaml at runtime
One other question, when I add some xaml code from another xaml file using the code below, I get this error message:
What is the proper way to insert xaml code?
NoesisException: Child already has a logical parent
Code: Select all
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);
Re: Replace xaml at runtime
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:
Code: Select all
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);
Re: Replace xaml at runtime
Marking this as solved then.
Who is online
Users browsing this forum: Semrush [Bot] and 88 guests