【问题标题】:Xamarin ListView not appearingXamarin ListView 没有出现
【发布时间】:2017-03-24 17:02:12
【问题描述】:

我正在尝试使用数据绑定在 xamarin 表单中创建一个简单的列表应用程序,但数据没有出现。 这是我的 MainPage.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"
             xmlns:local="clr-namespace:Practice3"
             x:Class="Practice3.MainPage">
    <StackLayout>
        <ListView ItemsSource="{Binding Films}">
        <ListView.ItemTemplate>
            <DataTemplate>
                    <ImageCell
                    Text="{Binding Title}" 
                    TextColor="Black"
                    Detail="{Binding Resume}" 
                    DetailColor="Gray"
                />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    </StackLayout>
</ContentPage>

这里是 MainPage.xaml.cs

namespace Practice3
{
    public partial class MainPage : ContentPage
    {
        public ObservableCollection<Film> Films { get; set; }

        public MainPage()
        {
            InitializeComponent();

            Films = new ObservableCollection<Film>{
                new Film{
                    Title = "X-Men (2000)",
                    Resume = "Two mutants come to a private academy for their kind whose resident superhero team must oppose a terrorist organization with similar powers."
                },
                new Film{
                    Title = "X-Men 2 (2003)",
                    Resume = "The X-Men band together to find a mutant assassin who has made an attempt on the President's life, while the Mutant Academy is attacked by military forces."
                },
                new Film{
                    Title = "X-Men: The last stand (2006)",
                    Resume = "When a cure is found to treat mutations, lines are drawn amongst the X-Men, led by Professor Charles Xavier, and the Brotherhood, a band of powerful mutants organized under Xavier's former ally, Magneto."
                }
            };
        }
    }
}

和 Films.cs

namespace Practice3
{
    public class Film
    {
        public string Title { get; set; }
        public string Resume { get; set; }
    }
}

我尝试在 StackLayout 中放一个 Label,它看起来很清晰,但 ListView 似乎是空的或不可见。

【问题讨论】:

  • 你们实施INPC吗?你应该创建一个单独的 MainPageViewModel.cs...
  • 你需要设置你的BindingContext:this.BindingContext = this;
  • @Jason 这不是暗示吗?
  • 不,通常你会绑定到一个虚拟机,而页面无法知道你的虚拟机叫什么

标签: c# xaml listview xamarin


【解决方案1】:

我终于这样解决了: MainPage.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"
             xmlns:local="clr-namespace:Practice3"
             x:Class="Practice3.MainPage">
    <StackLayout>
        <ListView x:Name="FilmList">
        <ListView.ItemTemplate>
            <DataTemplate>
                    <ImageCell
                    Text="{Binding Title}" 
                    TextColor="Black"
                    Detail="{Binding Resume}" 
                    DetailColor="Gray"
                />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    </StackLayout>
</ContentPage>

还有 MainPage.xaml.cs

namespace Practice3
{
    public partial class MainPage : ContentPage
    {
        public ObservableCollection<Film> Films { get; set; }

        public MainPage()
        {
            InitializeComponent();

            Films = new ObservableCollection<Film>{
                new Film{
                    Title = "X-Men (2000)",
                    Resume = "Two mutants come to a private academy for their kind whose resident superhero team must oppose a terrorist organization with similar powers."
                },
                new Film{
                    Title = "X-Men 2 (2003)",
                    Resume = "The X-Men band together to find a mutant assassin who has made an attempt on the President's life, while the Mutant Academy is attacked by military forces."
                },
                new Film{
                    Title = "X-Men: The last stand (2006)",
                    Resume = "When a cure is found to treat mutations, lines are drawn amongst the X-Men, led by Professor Charles Xavier, and the Brotherhood, a band of powerful mutants organized under Xavier's former ally, Magneto."
                }
            };
            FilmList.ItemsSource = Films;
        }
    }
}

【讨论】:

  • 可行,但正如我上面提到的,只需为您的页面分配 BindingContext 也应该解决它
  • 没错,我只是在解决之前没有看到您的答案。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多