Home > Software design >  LoadFromXAML does not work with custom namespaces
LoadFromXAML does not work with custom namespaces

Time:02-11

I'm developing an App which needs to download and display XAML-Code from a webserver. Till now my code works like expected but now I need to display an WebView which needs to have access to GPS-Data (which be can used with a chrome-browser on android). I tested the implementation and everything. When compiling the XAML-Code within the app-code, everything works like a charme. Even XAML-Hot-Reload works, but as soon as I try to load my content-page with following code:

public static class GUIFramework
    {
        public static readonly string PRE_FRAMEWORK_XAML = "<ContentPage xmlns=\"http://xamarin.com/schemas/2014/forms\" xmlns:x=\"http://schemas.microsoft.com/winfx/2009/xaml\" xmlns:local=\"clr-namespace:Buerger_App.Renderer;assembly=Buerger_App\" x:Class=\"Buerger_App.Views.Framework\"><ContentPage.Content>";
        public static readonly string POST_FRAMEWORK_XAML = "</ContentPage.Content></ContentPage>";

        /// <summary>
        /// This Dictionary is used for caching all of the generated CententPages in the memory.
        /// This is needed due to the Page-XAML-Code needs to be compiled JIT and this can cause noticable delay.
        /// The Caching automatically jumps in after a page was loaded once.
        /// </summary>
        private static Dictionary<string, ContentPage> CachedContentPages = new Dictionary<string, ContentPage>();

        /// <summary>
        /// Loads the given XAML string as a new ContentPage and binds the Framework Binding-Context
        /// </summary>
        /// <param name="XAML_Content">A string representing the XAML formatted code</param>
        /// <returns>The newly created ContentPage</returns>
        public static ContentPage LoadFramework(string XAML_Content, string Title = "")
        {
            string pre_XAML = PRE_FRAMEWORK_XAML;
            if (Title != "")
            {
                pre_XAML = pre_XAML.Replace("<ContentPage ", "<ContentPage Title=\""   Title   "\" ");
            }
            string FinalXAML = pre_XAML   XAML_Content   POST_FRAMEWORK_XAML;
            ContentPage ContentPage = null;

            // Use caching and read the XAML live from the phone and/or cache
            string hash = SHA256Hash.GetHash(FinalXAML);
            if (CachedContentPages.ContainsKey(hash))
                ContentPage = CachedContentPages[hash];
            else
            {
                ContentPage = new ContentPage().LoadFromXaml(FinalXAML);
                CachedContentPages.Add(hash, ContentPage);
            }

            ContentPage.BindingContext = new FrameworkViewModel();
            return ContentPage;
        }
        // More code ...
    }

I tried to load following XAML-Code (the full XAML-Code gets built using the LoadFramework-function):

<StackLayout>
    <Image Source="logo_wide.jpg" Margin="0,0,0,25"/>
    <local:GeoWebView x:Name="WebView" Margin="10,0,10,0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Source="https://google.de" />
</StackLayout>

The LoadFromXAML-extension runs without any error (no exception thrown). It only skips to create my custom GeoWebView (which I can verify in the childs of the newly created ContentPage in the debugger (the ContentPage only has 1 child and this is the image created before my custom WebView)).

CodePudding user response:

Ok, so i fiddled around a bit and found my solution. The problem I had was also described here. The answer mentioned to add the assembly-key to the namespace in the XAML-code which should get dynamically loaded was generally correct but I did it wrong in my original question (I added the assembly-key but with an incrorrect assembly-name).

The problem on my tests was, that I've entered Buerger_App as the assembly which is my default namespace and not my assembly-name. My assembly-name is Buerger App.

After correcting the XAML-code it started to work like expected.

  • Related