如何设置ListView的ItemsSource?

tes*_*ing 7 c# xaml xamarin.ios xamarin xamarin.forms

这里我定义了我的数据myListOfEmployeeObjects:

public class App : Application
{
    public List<Employee> myListOfEmployeeObjects;

    public App ()
    {
        Employee emp1 = new Employee () {
            FirstName = "Max",
            LastName = "Mustermann",
            Twitter = "@fake1"
        };
        Employee emp2 = new Employee () {
            FirstName = "Evy",
            LastName = "Mustermann",
            Twitter = "@fake2"
        };
        myListOfEmployeeObjects = new List<Employee> {
            emp1, emp2
        };
        MainPage = new NavigationPage (new EmployeeListPage ());
    }
}
Run Code Online (Sandbox Code Playgroud)

比我设置的XAML ItemsSource:

<ListView x:Name="listView"
                IsVisible="false"
                ItemsSource="{x:Static local:App.myListOfEmployeeObjects}"
                ItemSelected="EmployeeListOnItemSelected">
Run Code Online (Sandbox Code Playgroud)

这有用吗?因为我明白了

Xamarin.Forms.Xaml.XamlParseException:在xmlns中找不到类型App

public partial class EmployeeListPage : ContentPage {

    private ListView listView;

    private void InitializeComponent() {
        this.LoadFromXaml(typeof(EmployeeListPage)); // here the exception is thrown
        listView = this.FindByName <ListView>("listView");
    }
}
Run Code Online (Sandbox Code Playgroud)

如何设置ItemsSource我的XAML?

编辑:

现在我尝试了user2425632的建议,如果我做了以下更改,它会起作用:

  1. 添加xmlns:local="clr-namespace:HelloXamarinFormsWorld;assembly=HelloXamarinFormsWorld"到我的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:HelloXamarinFormsWorld;assembly=HelloXamarinFormsWorld"
             x:Class="HelloXamarinFormsWorld.EmployeeListPage"
             Title="Employee List">
    <ContentPage.Content>
Run Code Online (Sandbox Code Playgroud)

当然,您必须更改名称,以使其适合您的项目.

  1. 显示列表视图

我删除了IsVisibleItemSelected.

<ListView ItemsSource="{x:Static local:App.myListOfEmployeeObjects}">
Run Code Online (Sandbox Code Playgroud)
  1. 让一切都静止

它必须是静态的,否则你会得到

找不到本地的静态成员:App.myListOfEmployeeObjects

public static List<Employee> myListOfEmployeeObjects { private set; get; }

public static void GetAllEmployees(){
    Employee emp1 = new Employee () {
        FirstName = "Max",
        LastName = "Mustermann",
        Twitter = "@fake1"
    };
    Employee emp2 = new Employee () {
        FirstName = "Eva",
        LastName = "Mustermann",
        Twitter = "@fake2"
    };
    myListOfEmployeeObjects = new List<Employee> {
        emp1, emp2
    };
}

public App ()
{
    GetAllEmployees ();
    MainPage = new NavigationPage (new EmployeeListPage ());
}
Run Code Online (Sandbox Code Playgroud)

I_K*_*age 3

所以我自己实际上还没有抽出时间来做这件事,但是通过阅读文档,我有一个可能值得您尝试的建议。

ItemsSource="{x:Static local:App.myListOfEmployeeObjects}"
Run Code Online (Sandbox Code Playgroud)

在您的 xaml 中,您说过源是静态的,但查看您的 .cs 文件,它不是静态的。请尝试以下操作:

public static List<Employee> myListOfEmployeeObjects { private set; get; }
Run Code Online (Sandbox Code Playgroud)

然后尝试使用静态函数设置对象,例如:

static App() {
    myListOfEmployeeObjects = something;
}
Run Code Online (Sandbox Code Playgroud)

然后该列表应该可以在页面上查看。

我使用了以下链接,您可能会觉得有用:

有关数据绑定的 Xamarin 文档

示例cs代码

示例 xaml 代码

希望有帮助。