簡體   English   中英

如何設置ListView的ItemsSource?

[英]How to set ItemsSource of ListView?

這里我定義了我的數據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中找不到類型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");
    }
}

如何設置XAML的ItemsSource

編輯:

現在我嘗試了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>

當然,您必須更改名稱,以使其適合您的項目。

  1. 顯示列表視圖

我刪除了IsVisibleItemSelected

<ListView ItemsSource="{x:Static local:App.myListOfEmployeeObjects}">
  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 ());
}

所以我自己實際上並沒有這樣做,但是從閱讀文檔中我得到了一個值得你嘗試的建議。

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

在您的xaml中,您已經說過源是靜態的,但是查看您的.cs文件卻不是。 請嘗試以下方法:

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

然后嘗試使用靜態函數設置對象,例如:

static App() {
    myListOfEmployeeObjects = something;
}

然后列表應該在頁面上可見。

我使用了以下您可能會覺得有用的鏈接:

關於數據綁定的Xamarin文檔

示例cs代碼

示例xaml代碼

希望有所幫助。

我想我有解決你的問題的方法。 我有同樣的問題,我在EmployeeListPage.xaml中添加了這一行:

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

您將在項目的屬性和任何page.cs中的命名空間中獲得程序集的名稱

您可以使用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=EmployeesWindowEmployeesWindow是您的主窗口x:Name

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM