Home > Mobile >  How do I check if a variable is less than a selected number in Xamarin Forms?
How do I check if a variable is less than a selected number in Xamarin Forms?

Time:12-19

I'm trying to create an app which has the following pages:

  • MainPage.xaml ( MainPage.xaml.cs)
  • Name and suggestions.xaml ( Name and suggestions.xaml.cs)
  • MainPageViewModel.cs

I want to check if the "participantsturn" is less than what the user chose from the picker on the main page. If it is then I want to add 1 to it until it is equal and then move on with the else. But now when I test it and pick more than 1 from the picker - it still skips the if and moves on to the else. Does anyone know how I can prevent this from happening/what part of the code I need to change? Thanks!

For the main page I have a picker from which the user can choose a number from 1-30:

<Picker x:Name="mypicker"
               Title="Choose number of participants" 
               ItemsSource="{Binding ItemCollection}"
               SelectedItem="{Binding SelectedItem}"
               SelectedIndex="{Binding SelectedItemIndex}"
               />

In the "MainPageViewModel" I have the following code:


public int SelectedItem { get; set; } 

        public ObservableCollection<int> ItemCollection { get; set; }
        public MainPageViewModel()
        { 
            ItemCollection = new ObservableCollection<int>();
            CreateCollection();
        }

        private void CreateCollection ()
        {
            var numList = Enumerable.Range(1, 30).ToList();
            ItemCollection = new ObservableCollection<int>(numList);
        }

In the "Name and suggestions.cs" page I have this code:

public partial class Name_and_suggestions : ContentPage
{
        public Name_and_suggestions()
        {
            InitializeComponent();

            this.BindingContext = new MainPageViewModel();
        }

        static int participantsturn = 1; 
}

And later:

if (participantsturn <= ((MainPageViewModel)this.BindingContext).SelectedItem)
{
   participantsturn  ;

}

I've tried something with the index, but I'm not sure how to do it right for this.

Here is updated code for how I navigate between the pages - on my main page I have a button which takes the user to the Name and Suggestions page when clicked:

<StackLayout Padding="50,50,50,0">
     <Button Text="Start" 
                x:Name="StartButton"
                Clicked="StartButton_Clicked"
                HeightRequest="50" 
                />
</StackLayout>

Here is the MainPage.xaml.cs code behind:

     private void StartButton_Clicked(object sender, EventArgs e)
        {

            if (mypicker.SelectedIndex >= 0)
            {
                Navigation.PushAsync(new Name_and_suggestions());
            }
            else
            {
                Console.WriteLine("Please choose the number of participants first");
            }
                
           
        }

CodePudding user response:

you need to pass the value when you navigate

var item = ((MainPageViewModel)BindingContext).SelectedItem
Navigation.PushAsync(new Name_and_suggestions(item));
    

then in the 2nd page, you need to modify it's constructor to accept the parameter

int player; 

public Name_and_suggestions(int player)
{
    InitializeComponent();

    this.player = player;

    ...
}

now the 2nd page has a class level variable player that contains the value chosen on the first page

CodePudding user response:

If you want to store the value, you could use Preferences in Xamarin.Essentials.

When you select a value in Picker, you could try the following code:

public int SelectedItem
{
    get
    {
        return selectedItem;
    }
    set
    {
        selectedItem = value;
        references.Set("PickerSelectedValue", value);
    }
}

And in another page, if you want to use this value:

int value = Preferences.Get("PickerSelectedValue", 1); // the second parameter is default value you want to set

For more info, you could refer to Xamarin.Essentials: Preferences

Hope it works for you.

  • Related