Noa3
Topic Author
Posts: 10
Joined: 03 Sep 2020, 16:13

How to convert a String in NoesisXaml and then show it on Screen

01 Oct 2020, 09:36

Title says it allready.

i do some tests with Noesisi and try to lean in the moment WPF myshelf.
i have a string like wich have xaml code in it like the sample below, wich i want to show on the screen. can someone give me a Piece of code wich will do the work?

i have allready try the last two days what i can do and in the moment the most easy way to archive the goal is to convert it in a file and then let Noesis.GUI.LoadComponent() do the work maybe.
    string XamlToRead = @"<UserControl x:Name=""BackgroundWindowRoot"" x:Class=""WPFvStageStyle.Background""
             xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
             xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
             xmlns:mc=""http://schemas.openxmlformats.org/markup-compatibility/2006""
             xmlns:d=""http://schemas.microsoft.com/expression/blend/2008""
             xmlns:local=""clr-namespace:WPFvStageStyle""
             xmlns:noesis=""clr-namespace:NoesisGUIExtensions;assembly=Noesis.GUI.Extensions""
             mc:Ignorable=""d"" 
             d:DataContext=""{d:DesignInstance {x:Type local:ViewModel}, IsDesignTimeCreatable=True}""
             d:DesignHeight=""1080"" d:DesignWidth=""1920""
             >
            <Grid>
                <!-- Background & Vignette-->
                <Canvas x:Name=""Background_Color"" Background=""#FF272727"">
                </Canvas>
                <Canvas x:Name=""Background_Vignette"">
                    <Canvas.Background>
                        <RadialGradientBrush>
                            <GradientStop/>
                            <GradientStop Color = ""#00000000"" Offset=""0.0""/>
                            <GradientStop Color = ""#19000000"" Offset=""0.5""/>
                            <GradientStop Color = ""#66000000"" Offset=""1""/>
                        </RadialGradientBrush>
                    </Canvas.Background>
                </Canvas>
            </Grid>
            </UserControl>" ;
 
User avatar
sfernandez
Site Admin
Posts: 2997
Joined: 22 Dec 2011, 19:20

Re: How to convert a String in NoesisXaml and then show it on Screen

01 Oct 2020, 12:20

Hi,

Calling Noesis.GUI.ParseXaml(xamlText) will load an UI from a xaml text. The returned object can be added to an existing UI tree, for example you can have a simple xaml with a root Grid attached to your main camera, and then do the following in a MonoBehavior:
NoesisView view = GetComponent<NoesisView>();
Noesis.Grid grid = (Noesis.Grid)view.Content;
grid.Children.Add((Noesis.UserControl)Noesis.GUI.ParseXaml(xamlText));
Is that what your are looking for?
 
Noa3
Topic Author
Posts: 10
Joined: 03 Sep 2020, 16:13

Re: How to convert a String in NoesisXaml and then show it on Screen

01 Oct 2020, 12:36

i can guess in wich direction this is going thanks.

sadly i get a error now: hope i can solve it somehow and will write here again
Last edited by Noa3 on 01 Oct 2020, 13:08, edited 1 time in total.
 
User avatar
sfernandez
Site Admin
Posts: 2997
Joined: 22 Dec 2011, 19:20

Re: How to convert a String in NoesisXaml and then show it on Screen

01 Oct 2020, 13:08

First you need a xaml file like this:
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Grid>
Then drag the generated xaml asset to the Main camera gameobject and it will automatically create a NoesisView component on the camera to render that xaml.
Next attach your TestNoesis_ReadXamlString MonoBehavior to the Main camera too.

That should work.
 
Noa3
Topic Author
Posts: 10
Joined: 03 Sep 2020, 16:13

Re: How to convert a String in NoesisXaml and then show it on Screen

01 Oct 2020, 13:12

Thank for your time, works fine now.
the problem was the simple placeholder xaml file was not a Grid .

is there a soulution without a "placeholder" file or should i cretae a ticket maybe for it?
 
Noa3
Topic Author
Posts: 10
Joined: 03 Sep 2020, 16:13

Re: How to convert a String in NoesisXaml and then show it on Screen

01 Oct 2020, 13:57

All is working now in Unity, thanks!

if i put in the method AddContentOverCreatedXaml now a xaml string, i dont even need a dummy fille anymore.
    static string FileName = "sammplesXaml.xaml";
    string unityPath = "/tmp/";
    string FilePath = "";
    string PathFull
    {
        get
        {
            return FilePath + unityPath + FileName;
        }
    }
    
    void AddContentOverCreatedXaml(string XamlStringToLoad)
    {
        NoesisXaml nx = new NoesisXaml();
        NoesisView view = GetComponent<NoesisView>();

        WriteXamlDummyFile();

        nx.source = PathFull;
        nx.content = StringToByteArray(XamlStringToLoad);
        nx.Load();

        view.Xaml = nx;
        view.LoadXaml(true);

    }

    private void WriteXamlDummyFile()
    {
        if (Directory.Exists(FilePath + unityPath) == false)
        {
            Directory.CreateDirectory(FilePath + unityPath);
        }
        System.IO.File.WriteAllText(PathFull, XamlToRead);
    }
 
User avatar
jsantos
Site Admin
Posts: 3925
Joined: 20 Jan 2012, 17:18
Contact:

Re: How to convert a String in NoesisXaml and then show it on Screen

05 Oct 2020, 14:43

Thanks for the feedback! Marking this as solved

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 14 guests