Picker in Xamarin.Forms how to use in ViewModel

Cdn_EuroCdn_Euro Member ✭✭✭
edited August 2018 in Xamarin.Forms

This is my code but I am sure something is not right and I am getting an "Index Out Of Bounds" error:

public class RegisterViewModel: INotifyPropertyChanged
    {
        public RegisterViewModel()
        {
            Regions = new List<string>()
            {
                "Vancouver", "Seattle", "All"
            };
        }

        int regionsSelectedIndex=0;

        public int RegionsSelectedIndex
        {
            get
            {
                return regionsSelectedIndex;
            }
            set
            {
                if (regionsSelectedIndex != value)
                {
                    regionsSelectedIndex = value;

                    // trigger some action to take such as updating other labels or fields
                    OnPropertyChanged(nameof(RegionsSelectedIndex));
                    Region = Regions[regionsSelectedIndex];
                }
            }
        }
        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string Region { get; set; }

        public string Email { get; set; }

        public string Password { get; set; }

        public string ConfirmPassword { get; set; }      

        public List<string> Regions { get; set; }

        public ICommand RegisterCommand
        {
            get
            {
                return new Command(() =>
                {

                });

            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                       new PropertyChangedEventArgs(propertyName));
            }
        }
    }

And the view:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local1="clr-namespace:SAApp.CustomControls"
             x:Class="SAApp.RegisterPage"
             NavigationPage.HasNavigationBar="False"
             BackgroundColor="{StaticResource pagesBackgroundColor}"
             xmlns:viewModels="clr-namespace:SAApp.ViewModels">


    <ContentPage.BindingContext>
        <viewModels:RegisterViewModel></viewModels:RegisterViewModel>
    </ContentPage.BindingContext>

    <local1:GradientColorStack>
        <StackLayout VerticalOptions="CenterAndExpand"
                     Margin="20">

            <Entry Text="{Binding FirstName}"
                   Placeholder="First name"
                   TextColor= "{StaticResource entryTextColor}"
                   PlaceholderColor= "{StaticResource placeholderTextColor}">
            </Entry>

            <Entry Text="{Binding LastName}"
                   Placeholder="Last name"
                   TextColor= "{StaticResource entryTextColor}"
                   PlaceholderColor= "{StaticResource placeholderTextColor}">
            </Entry>

            <Picker SelectedIndex="{Binding RegionsSelectedIndex}"
                    ItemsSource="{Binding Regions}"
                    Title="Select a region">
            </Picker>

            <Entry Text="{Binding Email}"
                   Placeholder="Email address"
                   Keyboard="Email"
                   TextColor= "{StaticResource entryTextColor}"
                   PlaceholderColor= "{StaticResource placeholderTextColor}">
            </Entry>

            <Entry Text="{Binding Password}"
                   Placeholder="Password"
                   IsPassword="True"
                   TextColor= "{StaticResource entryTextColor}"
                   PlaceholderColor="{StaticResource placeholderTextColor}">
            </Entry>

            <Entry Text="{Binding ConfirmPassword}"
                   Placeholder="Password"
                   IsPassword="True"
                   TextColor= "{StaticResource entryTextColor}"
                   PlaceholderColor="{StaticResource placeholderTextColor}">
            </Entry>

            <Button Command="{Binding RegisterCommand}"
                    Text="Register">
            </Button>

        </StackLayout>

    </local1:GradientColorStack>

</ContentPage>

Best Answer

Answers

Sign In or Register to comment.