How to populate ListVIew from textfile in Xamrin Fomrs

24 Views Asked by At

I want to populate my ListView with 16 names of districts from text file. But when I open page it shows 16 rows without content in ListView. Is there also any other option to display content of txt file line by line? I listed below those 16 names. The output: enter image description here

My Code:

   ObservableCollection<Wojewodschaften> listModel = new 
    ObservableCollection<Wojewodschaften>();
  string content;
  string[] Wojewodztwa = new string[] { "" };
  private async void LoadWoj()
  {
   using (var stream = await FileSystem.OpenAppPackageFileAsync("wojewodschaften.txt"))
   {
       using (var reader = new StreamReader(stream))
      {
          content = await reader.ReadToEndAsync();
      }
      Wojewodztwa = content.Split('\n'); //Split the content after a new row.
    
      foreach (var item in Wojewodztwa)
      {
        Console.WriteLine(item);

        Wojewodschaften wojewodztwa = new Wojewodschaften()
        {
            NameOfWojewodschaften = item
        };
        listModel.Add(wojewodztwa); 
        ListOfWojewodschaftenList.ItemsSource = listModel;
     } 
    }
  }

Xaml Code

<ListView
  x:Name="ListOfWojewodschaftenList">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell
            Height="20"
            >
            <Label
                Text="{Binding NamWojewodschaften}"
                TextColor="Black" 
                FontSize="20"
                VerticalOptions="CenterAndExpand"
                HorizontalOptions="CenterAndExpand"
                />
             </ViewCell>
          </DataTemplate>
      </ListView.ItemTemplate>
  </ListView>

Txt file 16 names:

Dolnośląskie
Kujawsko-pomorskie
Lubelskie
Lubuskie
Łódzkie
Małopolskie
Mazowieckie
Opolskie
Podkarpackie
Podlaskie
Pomorskie
Śląskie
Świętokrzyskie
Warmińsko-mazurskie
Wielkopolskie
Zachodniopomorskie
0

There are 0 best solutions below