【问题标题】:Can't Access x:Name of the list from the code behind in Xamarin无法从 Xamarin 中的代码访问 x:列表的名称
【发布时间】:2021-01-09 08:18:00
【问题描述】:

我不知道为什么我不能从任何帮助后面的代码访问 xaml 文件中列表的 x:Name 吗?

我是 Xamarin 的新手,我正在尝试构建一个基本项目,我尝试用谷歌搜索一些解决方案,但我发现我的代码是正确的,所以我现在不知道该怎么做。

这里是 xml 文件 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>

背后的代码是 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;
            
    }
}

这里是错误

【问题讨论】:

    标签: c# android .net xamarin


    【解决方案1】:

    您应该将 Post 类放在 Posts 之外! 要“自动”创建列表,您应该将其放在构造函数中。但你的主要问题是 Post 类需要在外面

    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; }
        }
    }
    

    【讨论】:

    • 非常感谢!但请你能告诉我为什么是listview。 Itemsources 应该在构造函数中?
    • 每次创建 Posts 类时都会调用构造函数。在这种情况下,它会自动使用帖子创建您的列表并填充您的列表视图
    【解决方案2】:

    语句“listview.ItemsSource=Posty1;”位置错误。它应该在一个方法中。

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

    【讨论】:

    • 非常感谢兄弟!但是你能告诉我为什么 itemssource 应该在一个方法中吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多