如何设置ListView的ItemsSource?

时间:2015-02-26 08:59:10

标签: 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 ());
    }
}

比我设置ItemsSource

的XAML
<ListView x:Name="listView"
                IsVisible="false"
                ItemsSource="{x:Static local:App.myListOfEmployeeObjects}"
                ItemSelected="EmployeeListOnItemSelected">

这应该有用吗?因为我得到了

  

Xamarin.Forms.Xaml.XamlParseException:在xmlns中找不到类型的应用程序

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");
    }
}

如何设置我的XAML的ItemsSource

修改

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

  1. xmlns:local="clr-namespace:HelloXamarinFormsWorld;assembly=HelloXamarinFormsWorld"添加到我的XAML文件
  2. 现在看起来像以下

    <?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>
    

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

    1. 显示列表视图
    2. 我删除了IsVisibleItemSelected

      <ListView ItemsSource="{x:Static local:App.myListOfEmployeeObjects}">
      
      1. 让一切变得静止
      2. 它必须是静态的,否则你得到

          

        找不到本地的静态成员: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 ());
        }
        

3 个答案:

答案 0 :(得分:3)

所以我实际上并没有自己这样做,但是从阅读文档中我得到了一个值得你尝试的建议。

ItemsSource="{x:Static local:App.myListOfEmployeeObjects}"

在您的xaml中,您说来源是静态的但是查看您的.cs文件并不是。请尝试以下方法:

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

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

static App() {
    myListOfEmployeeObjects = something;
}

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

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

Xamarin documentation on data-binding

Example cs code

Example xaml code

希望有所帮助。

答案 1 :(得分:1)

我想我有解决你问题的方法。我有同样的问题,我在EmployeeListPage.xaml中添加了这一行:

xmlns:local="clr-namespace:YourNamespace;assembly=YourAssembly"

您将在项目的属性和任何page.cs中的命名空间中获取程序集的名称

答案 2 :(得分:0)

您可以使用ObservableCollections。这种类型的集合会在对其进行更改时通知。

在您的代码中:

<强>的.cs:

public class YourClass : INotifyPropertyChanged //Note this INotifyPropertyChanged
{

    private ObservableCollection<Employee> _myListOfEmployeeObjects;
    public ObservableCollection<Employee> ObservableEmployees
    {
        get
        {
            if (_myListOfEmployeeObjects == null) LoadEmployees();
            return _myListOfEmployeeObjects;
        }
        set
        {
            _myListOfEmployeeObjects = value;
            OnPropertyChanged("ObservableEmployees");
        }
    }

     private void LoadEmployees()
    {
        // Necessary stuff to load employees
        ObservableEmployees = new ObservableCollection<Employees>();
        ....
    }

您必须在默认构造函数中添加DataContext = this

public YourClass()
{
    InitializeComponent();
    DataContext = this;
}

然后,将必要的方法添加到NotifyOnPropertyChanged

 protected virtual void OnPropertyChanged(string propertyName)
 {
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
 }

在.xaml中:

<ListView x:Name="listView"
    IsVisible="false"
    ItemsSource="{Binding Path=ObservableEmployees, ElementName=EmployeesWindow, NotifyOnSourceUpdated=True}"
    ItemSelected="EmployeeListOnItemSelected">

ElementName=EmployeesWindow中,EmployeesWindow是您的主窗口x:Name

<Window ...
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"                      
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Name="EmployeesWindow" >