0

I have an int of the numbers of players and of this number I need to generate the numbers of entries for the player's name. I created 24 entries and made them visible and not visible but I think this is not "clever".

I tested a little bit like:

var layout = new Stacklayout();
var playerentry = new Entry {Text = "Test"};
layout.children.Add(playerentry);

combining this with a for loop with i = number of players it doesn't work.

Maybe someone can help this easy question. UPDATE:

This is the complete code:

  • 1
    could you add all code? – Hamed Moghadasi Feb 10 '20 at 12:30
  • 1
    "doesnt work" - what do you mean? Doing this in a loop should work. Can you post the actual code that you tried? – Jason Feb 10 '20 at 12:38
  • As @Jason, suggested using for loop should work, but I guess you want to update the player field in the DB or somewhere else according to the user input., for this I would suggest using a ListView with List as ItemsSource and bind Text of Entry to the Name property of the Player object. I'll try to add an answer to this. – Nikhileshwar Feb 10 '20 at 12:53
  • @FelixGebert As Nikhileshwar's code, there is ObservableCollection, you can add number items according to user input value, if you want to loop entry, you can loop this ObservableCollection. – Cherry Bu - MSFT Feb 11 '20 at 08:18

1 Answers1

0

Use ListView or BindableLayouts where number of items or views or unknown.

Xaml:

    <ListView x:Name="playerList">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout Orientation="Horizontal">
                        <Label Text="Enter player name"/>
                        <Entry Text="{Binding Name}"/>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

Code behind

var playerCollection = new ObservableCollection<Player>();
for(int i=0;i<8;i++)
{
    playerCollection.Add(new Player());
}
playerList.ItemsSource=playerCollection;

Player class

public class Player : INotifyPropertyChanged
{
    private string name = "Unknown";

    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
            OnPropertyChanged();
        }
    }

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

Hope this helps!! Comment for any queries.

Nikhileshwar
  • 1,666
  • 1
  • 11
  • 26
  • ObservableCollection could not be found. And is new Player(), not dynamically? – Felix Gebert Feb 10 '20 at 14:33
  • The Number of Entries is an input by user as an int and this number for example 8 should generate 8 entries of players. for my newbie talent i hoped i can do a for loop 8 times and put in the code i posted and then the entries are generated 8 times and added 8 times to my stacklayout. – Felix Gebert Feb 10 '20 at 14:43
  • then use a loop to create 8 Player objects. `ObservableCollection` is in `System.Collections.ObjectModel` – Jason Feb 10 '20 at 14:44
  • I have added the addition of item to collection in a for loop as you had asked. Please do check. – Nikhileshwar Feb 10 '20 at 18:09
  • @Nikhileshwar: Thanks a lot for the answer. works fine. all my names of players are safed in playercollection now, or? can i handle this collection like a list? and for the xaml file: i have an button on the bottom of my page and i want to have it fixed on the bottom of my page and over there should be my entries. can you please help me again? – Felix Gebert Feb 11 '20 at 10:55
  • 1. can i handle this collection like a list? ==> Sorry, I don't get your requirement properly. 2. i want to have it fixed on the bottom of my page ==> Are you looking for this [SO link](https://stackoverflow.com/questions/27709788/stick-layout-in-xamarin-forms-to-bottom) – Nikhileshwar Feb 11 '20 at 11:25