0

I create pin combination layout like

Image

Code:

<StackLayout>
                <Grid>
                    <Grid Margin="0">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <Button x:Name="btn7" Text="7" HeightRequest="160" WidthRequest="180" Margin="10" BackgroundColor="White" FontSize="50"  />
                    <Button x:Name="btn8" Text="8" HeightRequest="160" WidthRequest="180" Margin="10" Grid.Column="1" BackgroundColor="White" FontSize="50" />
                        <Button x:Name="btn9" Text="9"  HeightRequest="160" WidthRequest="180" Margin="10" Grid.Column="2" BackgroundColor="White" FontSize="50"/>
                        <Button Text="6"  HeightRequest="160" WidthRequest="180" Margin="10" Grid.Column="2" Grid.Row="1" BackgroundColor="White" FontSize="50"/>
                        <Button Text="5"  HeightRequest="160" WidthRequest="180" Margin="10" Grid.Column="1" Grid.Row="1" BackgroundColor="White" FontSize="50" />
                        <Button Text="4"  HeightRequest="160" WidthRequest="180" Margin="10" Grid.Row="1" BackgroundColor="White" FontSize="50" />
                        <Button Text="1"  HeightRequest="160" WidthRequest="180" Margin="10" Grid.Row="2" BackgroundColor="White" FontSize="50" />
                        <Button Text="2"  HeightRequest="160" WidthRequest="180" Margin="10" Grid.Row="2" Grid.Column="1" BackgroundColor="White" FontSize="50" />
                        <Button Text="3"  HeightRequest="160" WidthRequest="180" Margin="10" Grid.Row="2" Grid.Column="2" BackgroundColor="White" FontSize="50" />
                        <Button Text="0"  HeightRequest="160" WidthRequest="180" Margin="10" Grid.Row="3" Grid.Column="1" BackgroundColor="White" FontSize="50" />
                        <Button Text="Ok" HeightRequest="160" WidthRequest="180" Margin="10" Grid.Row="3" BackgroundColor="White" FontSize="50"/>
                        <Button Text="Clear"  HeightRequest="160" WidthRequest="180" Margin="10" Grid.Row="3" Grid.Column="2" BackgroundColor="White" FontSize="50" />
                    </Grid>
                </Grid>
            </StackLayout>

I want to do all number buttons random positions so I want to have 0,1,2,3,4,5,6,7,8,9 but in different position every time I access this view. I start with two binding class and I have structure like:

public class PinPageViewModel : BaseViewModel
    {

        public PinPageViewModel()
        {
            _ = LoadItems();
        }

        public PinPageViewModel(INavigation navigation)
        {
            _ = LoadItems();
            Navigation = navigation;
        }


        public INavigation Navigation { get; set; }
       

        private async Task LoadItems()
        {
            try
            {
              
            }
            catch (Exception ex)
            {
                return;
            }
          

        }

    }

So what I need to do to achieve that? I mean what I need to do in each button of stacklayout, then how can I receive in binding view model then set random text, and finally a function to know which button is pressed?

UPDATE

As comment bellow I have some questions, each button should have Text="{Binding Number}" (All buttons should have the same one)?

Then I change ViewModel as:

  public PinPageViewModel()
        {
            _ = LoadItems();
        }

        public PinPageViewModel(INavigation navigation)
        {
            _ = LoadItems();
            Navigation = navigation;
        }


        public INavigation Navigation { get; set; }

        private int _number;

        public int Number
        {
            get { return _number; }
            set { SetProperty(ref _number, value, nameof(Number)); }
        }
       

        private async Task LoadItems()
        {
            try
            {

                SetNumber();

            }
            catch (Exception ex)
            {
                return;
            }
        }


        private void SetNumber()
        {
            int Min = 0;
            int Max = 9;
            Random randNum = new Random();
            int[] test2 = Enumerable
                .Repeat(0, 5)
                .Select(i => randNum.Next(Min, Max))
                .ToArray();

          //How can I assign a number to each button?
        }
    }

How can I assign button value to each button after random numbers? Regards

Jesus
  • 79
  • 2
  • 9

1 Answers1

0

create a list of your numbers

public List<string> data { get; set; } = new List<string>{"0", "1", ... "9" };

randomize the order

in your XAML, bind each button to an element of the list

<Button Text="{Binding data[0]}" Clicked="ButtonClick" ... />
...
<Button Text="{Binding data[9]}" Clicked="ButtonClick" ... />

then in your click handler

protected void ButtonClick(object sender, EventArgs args)
{
  var button = (Button)sender;
  var number = button.Text;
}
Jason
  • 86,222
  • 15
  • 131
  • 146