locked
XmlnsDefinitionAttribute: ArgumentNullException - Parameter name: clrNamespace RRS feed

  • Question

  • User37558 posted

    I am using the method LoadFromXaml in Xamarin.Forms.Xaml.Extensions. This method allows for dynamic creation of UI elements from Xaml strings at runtime. I'm using this via reflection because it is an internal method. The Xamarin Forms team have refused to make this method public for some reason, but we require this functionality so we are forced to use this reflection hack.

    We've been using this functionality for several months now without a problem. But, today, when I ran the latest code, I got an exception when parsing our dynamically loaded Xaml (which hasn't changed). This has stopped development dead in its tracks for us because I can't run our app at all now.

    System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentNullException: Value cannot be null. Parameter name: clrNamespace at Xamarin.Forms.Internals.XmlnsDefinitionAttribute..ctor(String xmlNamespace, String clrNamespace) at Xamarin.Forms.Xaml.XamlParser.GetElementType(XmlType xmlType, IXmlLineInfo xmlInfo, Assembly currentAssembly, XamlParseException& exception) at Xamarin.Forms.Xaml.Internals.XamlTypeResolver.Resolve(String qualifiedTypeName, IServiceProvider serviceProvider, XamlParseException& exception) at Xamarin.Forms.Xaml.Internals.XamlTypeResolver.Xamarin.Forms.Xaml.IXamlTypeResolver.TryResolve(String qualifiedTypeName, Type& type) at Xamarin.Forms.Xaml.ExpandMarkupsVisitor.MarkupExpansionParser.Parse(String match, String& remaining, IServiceProvider serviceProvider) at Xamarin.Forms.Xaml.ExpandMarkupsVisitor.ParseExpression(String& expression, IXmlNamespaceResolver nsResolver, IXmlLineInfo xmlLineInfo, INode node, INode parentNode) at Xamarin.Forms.Xaml.ExpandMarkupsVisitor.Visit(MarkupNode markupnode, INode parentNode) at Xamarin.Forms.Xaml.MarkupNode.Accept(IXamlNodeVisitor visitor, INode parentNode) at Xamarin.Forms.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode) at Xamarin.Forms.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode) at Xamarin.Forms.Xaml.RootNode.Accept(IXamlNodeVisitor visitor, INode parentNode) at Xamarin.Forms.Xaml.XamlLoader.Visit(RootNode rootnode, HydratationContext visitorContext) at Xamarin.Forms.Xaml.XamlLoader.Load(Object view, String xaml) at Xamarin.Forms.Xaml.Extensions.LoadFromXaml[TXaml](TXaml view, String xaml) --- End of inner exception stack trace --- at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) at Adapt.Presentation.XamarinForms.XamlReader.<>cDisplayClass10.<.cctor>b2(BindableObject view, String xaml) at Adapt.Presentation.XamarinForms.XamlReader.LoadFromXaml[TView](TView view, String xaml) at Adapt.Presentation.XamarinForms.XamlReader.LoadFromXaml[TView](String xaml) at Adapt.Presentation.Xivic.Navigation.TabbedPageUIController.CreateAndAddPanelFromContent(PagePanel pagePanel, Nullable`1 panelHeight) at Adapt.Presentation.Xivic.PanelPageBase.d_22.MoveNext()

    I thought it might be a problem with our XAML, but it's happening with all the XAML that has always worked in the past. Here is an example of our XAML:

    <ContentView xmlns:local="clr-namespace:Adapt.Presentation.XamarinForms.Controls;assembly=Adapt.Presentation.XamarinForms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
      <Grid>
    
        <Grid.RowDefinitions>
          <RowDefinition Height="60" />
          <RowDefinition  />
        </Grid.RowDefinitions>
    
        <local:BasicRecordProvider RecordTypeName="Adapt.Model.Custom.ContractorDef"  x:Name="ContractorDefs" />
    
        <local:AdaptPicker Title="Client" ItemsSource="{Binding Source={x:Reference Name=ContractorDefs}}" DisplayMemberPath="Code" SelectedItem="{Binding Contractor, Mode=TwoWay}" />
    
        <Editor Text="{Binding ProbNotes}" TextColor="#FF0000EE" Grid.Row="1" />
    
      </Grid>
    
    </ContentView>
    

    I sifted through the Xamarin Forms code in the Git Repo, and traced the error message to this spot. It's in the constructor of XmlnsDefinitionAttribute (https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Core/XmlnsDefinitionAttribute.cs).

        public XmlnsDefinitionAttribute(string xmlNamespace, string clrNamespace)
        {
            if (xmlNamespace == null)
                throw new ArgumentNullException(nameof(xmlNamespace));
            if (clrNamespace == null)
                throw new ArgumentNullException(nameof(clrNamespace));
    
            ClrNamespace = clrNamespace;
            XmlNamespace = xmlNamespace;
        }
    

    Note: I am using Xamarin Forms build 2.3.4.224. I have also tried upgrading to 2.3.4.231 (with a full Git Clean of my solution folders), and this made no difference.

    I presume the problem has something to do with the Xml namespace, but I can't see what the issue would be here. Does anyone here have any ideas?

    Monday, May 15, 2017 1:59 AM

All replies

  • User37558 posted

    After hunting around on Google, I found this ingenious answer:

    http://stackoverflow.com/questions/42638197/generated-code-behind-throws-system-argumentnullexception

    I was missing a default namespace. It turns out that Xamarin Forms sometimes requires a default namespace, and sometimes doesn't. This depends on the version of Xamarin Forms you are using, and the platform. But, at no time does the Xamarin Forms parser give you a useful error message.

    I have logged a bug with the Xamarin Forms team here so that the parser gives a more descript error message in future. Please take the time to comment on the bug so that the team realises that this is an issue: https://bugzilla.xamarin.com/show_bug.cgi?id=56433

    Monday, May 15, 2017 3:12 AM
  • User291231 posted

    Yesterday I was upgrading Xamarin Forms from 2.3.3.193 to 2.3.4.224 (baby steps), and got the same error.

    Fortunately, the error was only in one form and I got to the problem by commenting out code in my ContentView XAML file. This file contained

    <ContentView.Content />

    and I got the project to compile again using

    <ContentView.Content></ContentView.Content>

    ... yeah.

    Hope this helps. ;)

    Friday, February 9, 2018 9:24 AM
  • User62222 posted

    not an overly helpful error, or maybe I don't know programmer speak enough :-) I had a missing 's' for 'converters', my xaml should have been Converter={x:Static converters:HasProperty NilesvanKersteren's comment made me start looking at the xaml a bit more pedantically and I happened to notice the missing 's'.

    Thursday, February 22, 2018 11:11 PM