Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi, trying to attach class constructor to basic listview. according to my logic this should work but somehow how it is not attaching to list view despite code runs ok;


What I have tried:

C#
using System.Collections.ObjectModel;
using System.Reflection.Metadata.Ecma335;
using System.Security.Cryptography.X509Certificates;

namespace MauiApp12;
public partial class MainPage : ContentPage
{
    public class MyVariables
    {
        string myst { get; set; }
                public MyVariables(string a)
        {
        myst = a;
        }
    }
    public ObservableCollection<MyVariables> variables { get; set; } = new ObservableCollection<MyVariables>() {new MyVariables("hello sir how are you")};
    //public ObservableCollection<MyVariables> variables { get; set; } = new ObservableCollection<MyVariables>() { new MyVariables { name = "adfdlkjlkjlksdf"; } };
    //ListView lv1 = new ListView();
    public MainPage()
    {
        InitializeComponent();
    }
    private void OnCounterClicked(object sender, EventArgs e)
    {
        lv.ItemsSource = variables;
        //lv.SetBinding(ItemsView.items, "variables");
    }
    }



XAML
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="MauiApp12.MainPage">

    <ScrollView>
        <VerticalStackLayout
            Spacing="25"
            Padding="30,0"
            VerticalOptions="Center">

            <ListView x:Name="lv" ItemsSource="{Binding variables}" />
            <Image
                Source="dotnet_bot.png"
                SemanticProperties.Description="Cute dot net bot waving hi to you!"
                HeightRequest="200"
                HorizontalOptions="Center" />

            <Label
                Text="Hello, World!"
                SemanticProperties.HeadingLevel="Level1"
                FontSize="32"
                HorizontalOptions="Center" />

            <Label
                Text="Welcome to .NET Multi-platform App UI"
                SemanticProperties.HeadingLevel="Level2"
                SemanticProperties.Description="Welcome to dot net Multi platform App U I"
                FontSize="18"
                HorizontalOptions="Center" />

            <Button
                x:Name="CounterBtn"
                Text="Click me"
                SemanticProperties.Hint="Counts the number of times you click"
                Clicked="OnCounterClicked"
                HorizontalOptions="Center" />

        </VerticalStackLayout>
    </ScrollView>

</ContentPage>
Posted
Updated 4-Jun-23 19:44pm
v2
Comments
Graeme_Grant 5-Jun-23 1:42am    
Do not post duplicate questions. Always update the original question.
suhail malik 2023 5-Jun-23 1:52am    
this is not duplicate question last time this was not about class constructors. i want to learn maui i dont know what i am doing wrong in this beahtiful forum. i will deeply appreciate yoour answer. i am sitting in a remote area of this world and i do not have any teacher and only way to learn this forum.
Graeme_Grant 5-Jun-23 2:01am    
Yes it is. Here is the old question: MAUI basic listview items are not showing on screen[^]

Here you ask "this should work but somehow how it is not attaching to list view despite code runs ok" and the other question was "trying to create a basic listview in code behind, code runs ok but nothing is showing on screen" ... have the same issue but asked differently. The only difference this time is that you listened and posted the xaml.

I can see your issue. Delete this question, update the original question and I'll post my observation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900