Home > Software design >  C# .NET Maui, System.Reflection.TargetInvocation exception. Every Time I try to open a site of my pr
C# .NET Maui, System.Reflection.TargetInvocation exception. Every Time I try to open a site of my pr

Time:01-16

I try to load Data of the type "User" in a ListView Template. It recognises everything. But for some reason I get this exception when I try to open the site that contains the ListView: System.Reflection.TargetInvocation exception. I get 0 errors just this exception.

C# Code looks like this (I am really new to C#, and this is probably not best practice)

using System.Collections.ObjectModel;

namespace GoogleMessenger.Pages;

public partial class ContactPage : ContentPage
{
    public ObservableCollection<User> Contact { get; set; } = new ObservableCollection<User>();
    public List<User> UserList { get; set; }
    public ContactPage()
    {
        InitializeComponent();
        BindingContext = this;

    }

    protected override void OnAppearing()
    {
        UserList = new List<User>()
        {
            new User() {Username = "something", Email = "[email protected]", LastMessage = "something! Lorem Ipsum...", ImageSource = "dotnet_bot.jpg"},
            new User() {Username = "something", Email = "[email protected]", LastMessage = "Lorem 10min something...", ImageSource = "dotnet_bot.jpg.jpg"},

        };


        if (!UserList.Any())
        {
            foreach (var user in UserList)
            {
                var userCopy = user;
                Contact.Add(userCopy);
            }
        }
        base.OnAppearing();
    }
      
}

And my XAML Code looks like this

<?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"
             xmlns:pages="clr-namespace:GoogleMessenger.Pages"
             x:Class="GoogleMessenger.Pages.ContactPage"
             Title="ContactPage"
             >
    <ContentPage.BindingContext>
        <pages:ContactPage></pages:ContactPage>
    </ContentPage.BindingContext>
 
    <ListView ItemsSource="{Binding Contact}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Grid Padding="10">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Image Grid.RowSpan="2"
                           Source="{Binding ImageSource}"
                           Aspect="AspectFill"
                           HeightRequest="60"
                           WidthRequest="60" />
                    <Label Grid.Column="1"
                           Text="{Binding Username}"
                           FontAttributes="Bold" />
                    <Label Grid.Row="1"
                           Grid.Column="1"
                           Text="{Binding LastMessage}"
                           FontAttributes="Italic"
                           VerticalOptions="End" />
                </Grid>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
</ContentPage>

CodePudding user response:

get rid of this

<ContentPage.BindingContext>
    <pages:ContactPage></pages:ContactPage>
</ContentPage.BindingContext>

you are already assigning the BindingContext in the codebehind so this is redundant, and not the proper syntax to use when your page is it's own BindingContext

CodePudding user response:

To begin with, as Jason suggested, don't set multiple BindingContext. You either set the BindingContext in Xaml or Code-behind.

In addiction, you can try to set the item source of ListView like below:

Code-behind:

public partial class MainPage : ContentPage
{
    readonly IList<User> source;
    public ObservableCollection<User> Contact { get; private set; } 

    public MainPage()
    {
       InitializeComponent();
       source= new List<User>();
       CreateUserCollection();

       BindingContext = this;   
   }

    private void CreateUserCollection()
    {
        source.Add(new User() { Username = "something", Email = "[email protected]", LastMessage = "something! Lorem Ipsum...", ImageSource = "dotnet_bot.png" });
        source.Add(new User() { Username = "something", Email = "[email protected]", LastMessage = "Lorem 10min something...", ImageSource = "dotnet_bot.png" });

        Contact = new ObservableCollection<User>(source);
    }

}
  • Related