Home > Back-end >  .net Maui ViewList Binding with an ObservableCollection
.net Maui ViewList Binding with an ObservableCollection

Time:09-08

I have a ObservableCollection: tags

and for that the Property: Tags

They should be the ItemsSource for my List View: tagList

Until now I got a List from another method and iterated them into tags the whole time but now that takes too long.

But now I found a simpler way to fill the list without iterating it from one to another but the problem is that. But it doesn't display the tags in the Listview so I tried it to bind it per the xml code but it doesn't seem to work.

Here is the XML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TagEditor.TagPage"
             Title="TagPage">
    <VerticalStackLayout
        Spacing="5"
        HorizontalOptions="Center">

        <RefreshView
            x:Name="RefreshScroll">
            <ScrollView
                    HeightRequest="500"
                    WidthRequest="500"
                    VerticalOptions="StartAndExpand"    
                    VerticalScrollBarVisibility="Always"
                >
                <VerticalStackLayout>
                    <ListView
                        x:Name="tagList"
                        ItemsSource="{Binding Tags}"
                        BackgroundColor="LightGray"
                    >
                    </ListView>
                </VerticalStackLayout>
            </ScrollView>
        </RefreshView>


    </VerticalStackLayout>
</ContentPage>

and here is the c# code

    private ObservableCollection<Tag> tags = new();

    public ObservableCollection<Tag> Tags { get { return tags; }  set { tags = value; } }

    public TagPage(TagFinder tagFinder)
    {
        MessagingCenter.Subscribe<MainPage>(this, "DISCONNECT", (sender) =>
        {
            MainThread.BeginInvokeOnMainThread(() =>
            {
                Application.Current?.CloseWindow(this.Window);
            });
        });
        InitializeComponent();
        tagList.ItemsSource = Tags;
        try
        {
            UpdateSearchresults();
        }
        catch (FileNotFoundException e)
        {
            DisplayAlert("Error", e.Message, "Ok");
        }
    }

What would I have to do so the Tags are beeing used as the ItemsSource?

CodePudding user response:

Here is solution using Xml Linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication40
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
 
            XDocument doc = XDocument.Load(FILENAME);
            XNamespace ns = doc.Root.GetDefaultNamespace();
            XNamespace nsX = doc.Root.GetNamespaceOfPrefix("x");

            XElement listView = doc.Descendants(ns   "ListView").FirstOrDefault();
            string name = (string)listView.Attribute(nsX   "Name");
        }
    }

}

CodePudding user response:

Based on your code, I did a test ,and it works on my side.

You can add Cell label to display each item of ListView.

Please refer to the following code:

  <ListView x:Name="tagList"
            ItemsSource="{Binding Tags}"
            BackgroundColor="LightGray">
        <ListView.ItemTemplate>
                  <DataTemplate>
                       <TextCell Text="{Binding Name}"
                                 Detail="{Binding MyProperty}"/>
                   </DataTemplate>
         </ListView.ItemTemplate>
  </ListView>
  • Related