Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Why does this Bindable.Layout work in UWP, but not in Android?

Hello Campers
I have this program which uses a Bindable.Layout which works fine in UWP, but not in Android. If you run the program and press "Drink me!" a few times then the beverage list appears and disappears consistently under UWP, but it only appears just the ONCE under the Android emulator. Can any wise owl give me any guidance as to why this should be the case? Many thanks in advance for any light you can shine.

Here is the XAML MainPage.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="Wonderland.MainPage">

    <Frame  CornerRadius="25"
                x:Name="framestack"
                HasShadow="False"
                Padding="0"
                BorderColor="Black"
                Margin="5,0,5,0">
        <StackLayout Spacing="0" 
                     Padding="5,0,5,0" 
                     Margin="10,0,10,0" 
                     BackgroundColor="Transparent">
            <Grid >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="250"/>
                </Grid.ColumnDefinitions>
                <Frame  Grid.Column="0"
                        CornerRadius="5"
                        HasShadow="False"
                        Padding="0"
                        BorderColor="Red"
                        Margin="0,0,0,0">
                    <StackLayout Spacing="0" 
                        Padding="5,0,5,0" 
                        Margin="10,0,10,0" 
                        BackgroundColor="Transparent">
                        <Grid >
                            <Grid.RowDefinitions>
                                <RowDefinition Height="50"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <StackLayout 
                                    Grid.Row="0"
                                    HeightRequest="50">
                                <Label Text="Drink me!"
                                    Margin="10,0,0,0"/>
                                <StackLayout.GestureRecognizers>
                                    <TapGestureRecognizer 
                                          NumberOfTapsRequired="1" 
                                          Tapped="Submit"/>
                                </StackLayout.GestureRecognizers>
                            </StackLayout>
                            <StackLayout
                                    Grid.Row="1"
                                    BindableLayout.ItemsSource="{Binding Selections}"
                                    IsVisible="{Binding ListVisible}">
                                <BindableLayout.ItemTemplate>
                                    <DataTemplate>
                                        <StackLayout>
                                            <Label Text="{Binding Content}"/>
                                        </StackLayout>
                                    </DataTemplate>
                                </BindableLayout.ItemTemplate>
                            </StackLayout>
                        </Grid>
                    </StackLayout>
                </Frame>
            </Grid>
        </StackLayout>
    </Frame>
</ContentPage>

Here is the MainPage.xaml.cs:

using System;
using System.Collections.Generic;
using Xamarin.Forms;

namespace Wonderland
{
    public partial class MainPage : ContentPage
    {
        public static MainViewModel mainviewmodel;

        public MainPage()
        {
            InitializeComponent();
            mainviewmodel = new MainViewModel();
            BindingContext = mainviewmodel;

            MainViewModel.Brand brand = new MainViewModel.Brand()
            {
                Content = "Beer"
            };
            mainviewmodel.BrandSelect.Add(brand);
            brand = new MainViewModel.Brand()
            {
                Content = "Whisky"
            };
            mainviewmodel.BrandSelect.Add(brand);
            brand = new MainViewModel.Brand()
            {
                Content = "Vodka"
            };
            mainviewmodel.BrandSelect.Add(brand);
            brand = new MainViewModel.Brand()
            {
                Content = "Water"
            };
            mainviewmodel.BrandSelect.Add(brand);
        }

        public void Submit(object sender, EventArgs e)
        {
            if (!mainviewmodel.ListVisible)
            {
                mainviewmodel.Selections = mainviewmodel.BrandSelect.ToArray() as dynamic[];
                mainviewmodel.ListVisible = true;
            }
            else
            {
                mainviewmodel.ListVisible = false;
            }
        }
    }
}

and here is MainViewModel.cs:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace Wonderland
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }

        private bool _listvisible = false;
        public bool ListVisible
        {
            get
            {
                return _listvisible;
            }
            set
            {
                _listvisible = value;
                this.NotifyPropertyChanged(nameof(ListVisible));
            }
        }

        public class Brand
        {
            public string Content { get; set; }
        }

        private List<Brand> _brands_select = new List<Brand>();
        public List<Brand> BrandSelect
        {
            get
            {
                return _brands_select;
            }
            set
            {
                _brands_select = value;
                this.NotifyPropertyChanged(nameof(BrandSelect));
            }
        }

        private dynamic[] _selections;
        public dynamic[] Selections
        {
            get
            {
                return _selections;
            }
            set
            {
                _selections = value;
                this.NotifyPropertyChanged(nameof(Selections));
            }
        }
    }
}

There are three NuGet packages used:
NetStandard.Library V2.0.3
Xamarin Essentials V1.5.3.2
Xamarin Forms V4.8.0.1451

and the Android target version is Android 9.0 (Pie)

Best Answer

Answers

  • princepotemkinprincepotemkin Member ✭✭

    Works a treat! Many thanks. Spent 2 days on this .. was banjaxed by the fact it worked on UWP and not Android. Beers are on me whenever we meet :)

Sign In or Register to comment.