Another common mistake using Xamarin Forms (and XAML): Property Content is null or is not IEnumerable

It’s the second or third time in a few days that in the forum or in StackOverflow some guys has problem with XAML files. I don’t use XAML (I believe to be in error, but I am a “old school” programmer and write ALL using code). I don’t like it very much and I think it’s another layer that can give complications.

For example, in this post a guy say that a “Sequence contains no elements” error appear. The code is very simple and the error is misleading. I have tried to help him and I reproduced his sample. It’s compile, but when I run on the emulator, I have this error:

Screenshot 2017-03-11 08.42.38

This error appears in

[System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
private void InitializeComponent() {
        this.LoadFromXaml(typeof(MyPage1));
        FeedbackList = this.FindByName <global::Xamarin.Forms.ListView>("FeedbackList");
    }
}

So, the problem is when the “Parser” try to figure out what is in the XML file. It’s not very clear: I don’t understand why the problem is raised. I have “row” and “cols” where the problem happens, so I open the XML file, and it seems all OK.

 <?xml version="1.0" encoding="UTF-8"?>
 <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
     x:Class="TestRelativeLayout.MyPage1">
     <ContentPage.Content>
         <Label>I am Testimonials Page</Label>
         <ListView x:Name="FeedbackList" />    
     </ContentPage.Content>
 </ContentPage>

A “ContentPage”, a “Label”, a “ListView”. A very simple layout.

But… OMG. It has not a “container”. I can’t add 2 controls to a “Content”property. I can add only a control at a time!!! Usually a “Layout”!

If he used C# to create his UI, he has not have this problem because C# don’t allow to add more than one control to ContentPage.Content:

Label l = new Label();
Label l2 = new Label();
//this.Content = ... // I have not a "Add" method
this.Content = new StackLayout { Children = { l, l2 } };

Changed the XAML

 <?xml version="1.0" encoding="UTF-8"?>
 <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
     x:Class="TestRelativeLayout.MyPage1">
     <ContentPage.Content>
         <StackLayout>
             <Label>I am Testimonials Page</Label>
             <ListView x:Name="FeedbackList" />    
         </StackLayout>
     </ContentPage.Content>
 </ContentPage>

And the error has gone:

Screenshot_1489219249

A Label with an empty ListView

The conclusion? In this case, XAML does not help the programmer in highlighting an error that appears only during debugging, with an error message does not particularly clear.

XAML “0” – C# “1”

 

Annunci

4 pensieri su “Another common mistake using Xamarin Forms (and XAML): Property Content is null or is not IEnumerable

  1. Just because XAMLC (the XAML compiler) unfortunately missed to see an error, doesn’t mean XAML is bad and shouldn’t be used.

    The fact that you like writing UI by code behind is your choice. I’d say this is a bad practice. You are blaming XAML for being another layer, but you’re creating another layer with your own code, which is harder to maintain and it’s more verbose.

    Liked by 1 persona

    • It’s not a war between XAML or C#. I think that XAML can be better than code to create the UI. In this particular case, XAMLC does not help the programmer to understand the problem and, in this case, C# does not allow to have this kind of error.

      Mi piace

      • I was wrong in my statement earlier. It’s not that XAMLC does not see the error, it actually does see it, and it does pretty well.
        It even tells you the error line number right there: it’s line 4, column 10.

        The only issue is that exception message could be better.
        Instead of “Property Content is null or is not IEnumerable” it could have said something like:
        “You’re trying to set the Property Content of a Page instance to something which is not null neither IEnumerable.”
        But again, it already gives you the line and column number. I don’t remember right now if double clicking on it or clicking on details, doesn’t give you more information or if it moves the cursor in the right position in XAML.

        Liked by 1 persona

  2. Pingback: Dew Drop - March 13, 2017 (#2438) - Morning Dew

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...