【发布时间】: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;
}
}
【问题讨论】: