简体   繁体   中英

Can't Access x:Name of the list from the code behind in Xamarin

I don't know why I can't access the x:Name of the list in xaml file from the code behind any help plz?

I'm new to Xamarin and Im trying to build a basic project ,I tried to google some solutions but I found that my code is correct so I don't what to do now .

here the xml file Posts.Xaml

        <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="FavPosts.Posts">
    
        <ListView x:Name="listview" HasUnevenRows="True">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal" 
                                     Padding="5">
                            <Label  HorizontalOptions="StartAndExpand"/>
                            <Button Text="Favorite"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    
    </ContentPage>

and the code behind is Posts.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace FavPosts
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Posts : ContentPage
    {
        public Posts()
        {
            InitializeComponent();
        }

        public class Post
        {
            public string Status { get; set; }
            public string Details { get; set; }
        }


        List<Post> Posty = new List<Post> { 
            new Post { Status="f1", Details="D1" },
            new Post { Status="f2", Details="D2"}
            };


        listview.ItemsSource = Posty;
            
    }
}

and here are the errors这是错误

the statement "listview.ItemsSource=Posty1;" has a wrong location. it should be in a method.

public Posts() {
    InitializeComponent();
    listview.ItemsSource=Posty1;
}

You should place class Post outside from Posts! To create List "automatically" you should place it in Constructor. But your main issue ist that class Post need to be outside

namespace FavPosts
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Posts : ContentPage
    {
        public Posts()
        {
            InitializeComponent();
            List<Post> Posty = new List<Post> { 
               new Post { Status="f1", Details="D1" },
               new Post { Status="f2", Details="D2"}
            };


            listview.ItemsSource = Posty;
        }
     


        
            
    }
    public class Post
    {
            public string Status { get; set; }
            public string Details { get; set; }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM