【问题标题】:ListView not bindingListView 不绑定
【发布时间】:2023-04-10 06:42:01
【问题描述】:

当我运行应用程序时,我得到两个标签(我使用的是标签页),但它们是空白的。

我有一个有点复杂的 ViewModel:

        public partial class NowPlayingView
{
    const string NowPlayingUrl = "http://api.myserver.com";
    public static List<MoviesItem> MoviesLst { get; set; }

    public NowPlayingView()
    {
        InitializeComponent();
        BindingContext = new MoviesViewModel();
    }
    public class MoviesViewModel
    {
        public MoviesViewModel()
        {
            Action<Dictionary<string, string>> initAction = initialize;
            initAction(new Dictionary<string, string>()
                {
                    {"$format", "json"},
                    {"AccessKey", "f54tg5gf54g-fgs3452-324asdf4"},
                    {"CineplexLanguage", "en-us"}
                });
        }

        public async void initialize(Dictionary<string,string> parameters)
        {
            var data = await (new ApiUtilities().CallGetData<MoviesNowPlaying>(NowPlayingUrl, "/api.svc/MoviesNowPlaying", parameters));
            MoviesLst = data.d.results.Select(x => new MoviesItem() {Header = x.Title, Text = x.MediumPosterImageURL}).ToList();
        }
    }
    public class MoviesItem
    {
        public string Header { get; set; }
        public string Text { get; set; }
    }
}

我的 XAML 文件如下所示:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                       x:Class="XamPlex.MainCategories.NowPlayingView"
             Title="Now Playing">
    <ListView x:Name="MoviesListView" RowHeight="80" BackgroundColor="Transparent" ItemsSource="{Binding MoviesLst}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <ViewCell.View>
                        <StackLayout Orientation="Vertical" Spacing="0" Padding="10">
                            <Label Font="Bold,20" Text="{Binding Header}" TextColor="Indigo"/>
                            <Label Font="16" Text="{Binding Text}" TextColor="Indigo"/>
                        </StackLayout>
                    </ViewCell.View>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage>

我检查了 MoviesLst 的内容,它包含大量数据,有什么想法可能是错误的吗?

【问题讨论】:

  • BindingSource 听起来像是这里的问题,请查看此链接msdn.microsoft.com/en-us/library/ms750972(v=vs.110).aspx
  • 您正在异步填充 ViewModel,因此在创建绑定时其中没有数据。将您的 List 更改为 ObservableCollection - 这将允许它在填充数据时通知 View 进行更新。

标签: c# xaml xamarin


【解决方案1】:

我没有做很多MVC,但我相信View应该有这种形式:

public ActionResult Index() {
    return View();
}

我认为你的视图应该是这样的结构......

public ActionResult Index() {
    var data = await (new ApiUtilities().CallGetData<MoviesNowPlaying>(NowPlayingUrl, "/api.svc/MoviesNowPlaying", parameters));
    var list = data.d.results.Select(x => new MoviesItem() {Header = x.Title, Text = x.MediumPosterImageURL}).ToList();
    return View(list);
}

不过,我也只完成了 MVC 的基本教程。我可能误解了你的模型。

我看不到您在哪里发布您的视图。我看到的似乎是模型。

【讨论】:

    猜你喜欢
    • 2012-05-07
    • 2011-08-30
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 2013-07-17
    • 1970-01-01
    相关资源
    最近更新 更多