Xamarin.forms: [Bug] XAML x:Name + Binding reference doesn't work on nested layouts binding

Created on 15 May 2019  路  5Comments  路  Source: xamarin/Xamarin.Forms

Description

I have a Page with a StackLayout using BindableLayout.ItemsSource, inside each item I have a ListView, for each item in this nested ListView I need to do a Binding to a property on the Page's ViewModel. I'm trying to use the Source+Path approach but the app crashes as soon as I open the page that contains this structure.

MainPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
  x:Class="BindableLayoutReferenceBug.ListViewPage"
  xmlns="http://xamarin.com/schemas/2014/forms"
  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  xmlns:local="clr-namespace:BindableLayoutReferenceBug">
  <StackLayout BindableLayout.ItemsSource="{Binding Items}">
    <BindableLayout.ItemTemplate>
      <DataTemplate>
        <local:MessageListViewTemplate />
      </DataTemplate>
    </BindableLayout.ItemTemplate>
  </StackLayout>
</ContentPage>

MessageListViewTemplate.xaml:

<?xml version="1.0" encoding="UTF-8" ?>
<ContentView
  x:Class="BindableLayoutReferenceBug.MessageListViewTemplate"
  xmlns="http://xamarin.com/schemas/2014/forms"
  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  x:Name="listView">
  <ListView ItemsSource="{Binding Options}">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
        <StackLayout>
          <Label Text="{Binding .}" />
          <Button
            BackgroundColor="Blue"
            Command="{Binding Source={x:Reference Name=listView}, Path=Parent.BindingContext.ShowMessageCommand}"
            CommandParameter="{Binding .}"
            Text="Show Message" />
        </StackLayout>
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>
</ContentView>

The exception shows that there is a problem finding the reference to the x:Name I used: Can not find the object referenced by listView.

image

This only happens when I have a nested structure as I mentioned (StackLayout with BindableLayout > ListView). At first I thought this was a problem only with BindableLayout, but my repro shows that his happens on ListView and BindableLayout

I tried using different Paths for the binding, but since this is a problem parsing the XAML not finding the x:Name referenced, I don't think it even starts evaluating my Path.

As shown in this answer in SO, it works with C#, so this look like a XAML bug.

Steps to Reproduce

  1. Clone https://github.com/akamud/BindableLayoutReferenceBug
  2. Run the app and click any of the buttons

Expected Behavior

The app should render a nested list with a frame that fires a command on the parent ViewModel.

Actual Behavior

The app crashes as soon as the page's XAML is parsed.

Basic Information

  • Version with issue: Xamarin.Forms 3.6.0.293080
  • Platform Target Frameworks:

    • iOS: 12.2

    • Android: 9.0

Reproduction Link

https://github.com/akamud/BindableLayoutReferenceBug

Xaml </> 4 bug

Most helpful comment

I am trying to reference the ViewModel bound to a ContentView also. I can confirm it works after turning off XamlCompilation.

[XamlCompilation(XamlCompilationOptions.Skip)]

All 5 comments

In order to bind to ShowMessageCommand, it needs to be a property, not a field. Once that's fixed, I can reproduce your error when XamlC is on, but it works fine when XamlC is off

My bad, I'm creating a repro for this issue from a real project that I cannot share. I pushed some changes to the repro code.

Thanks for the heads-up :)

I'm also seeing this bug. And can confirm turning XAMLC off for that view fixes it.

Not ideal but a temporary workaround

I tried working on this bug, but couldn't figure out the reason why the name is getting lost. I see the code that sets the name being called, but somehow it is not present again when needed.

I am trying to reference the ViewModel bound to a ContentView also. I can confirm it works after turning off XamlCompilation.

[XamlCompilation(XamlCompilationOptions.Skip)]

Was this page helpful?
0 / 5 - 0 ratings