【问题标题】:Listview in xaml doesn't fill up through local REST API callxaml 中的 Listview 不会通过本地 REST API 调用填充
【发布时间】:2015-12-05 16:12:02
【问题描述】:

我正在尝试在 Azure 移动服务 + 随附数据库的帮助下构建通用 Windows 应用程序。我在ToDoList.xaml中有以下代码

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ScrollViewer>
        <ListView ItemsSource="{Binding}">
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapGrid Orientation="Vertical" ItemWidth="800" ItemHeight="50">
                    </WrapGrid>
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="20">
                        <!--<CheckBox IsChecked="{Binding Complete}" Content="{Binding Text}"/>-->
                        <TextBlock Text="{Binding Text}"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ScrollViewer>

以此作为类背后的代码:

public sealed partial class ToDoList : Page
{
    ToDoListVM currentVM = new ToDoListVM();

    public ToDoList()
    {
        this.InitializeComponent();

        currentVM.getToDos();
        this.DataContext = currentVM.ToDoItems;
    }
}

这是他们为 Azure 移动服务调用所指的类:

class ToDoListVM
{
    public ObservableCollection<ToDoItem> ToDoItems { get; set; }

    public async void getToDos()
    {
        ToDoItems = await App.MobileService.GetTable<ToDoItem>().ToCollectionAsync();
    }
}

现在的问题是,我无法理解为什么列表没有被填满。我在本地启动服务的调试实例,然后启动 UWP 应用程序,尽管屏幕上没有显示任何内容,即使启动屏幕是 ToDoList.xaml

我注意到的另一个异常:在WebApiConfig 文件中,我将基本文本从 ToDoItem 更改为“first todo”,而不是“first item”。但是当我尝试使用 Postman 测试 API 调用时,我仍然得到“第一项”

【问题讨论】:

    标签: c# .net xaml binding uwp


    【解决方案1】:

    您需要更改为使用 ItemsSource 属性填充列表视图:

        <ListView Name="lst">
           ...
        </ListView>
    

    然后

    public sealed partial class ToDoList : Page
    {
        ToDoListVM currentVM = new ToDoListVM();
    
        public ToDoList()
        {
            this.InitializeComponent();
    
            currentVM.getToDos();
            lst.ItemsSource = currentVM.ToDoItems;
        }
    }
    

    【讨论】:

    • 感谢您的评论,但到目前为止它没有帮助。我按照你说的修改了代码,但还是什么都没看到。
    猜你喜欢
    • 2020-11-28
    • 2016-03-21
    • 2012-11-21
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多