Xamarin.Forms中的数据绑定

时间:2016-05-27 08:18:36

标签: asp.net mvvm xamarin xamarin.forms xamarin-studio

我希望在ASP.NET Web应用程序中创建的所有记录都显示在我的Mobile App Xamarin.Forms中。我的程序发生了什么,我能够在我的Web应用程序中创建记录,但我无法在我的Xamarin.Forms Mobile应用程序中将其绑定。我创建了一个MainViewModel,它将从我在MainPage中绑定的Web应用程序中获取记录。 我该怎么做才能显示我创建的所有记录? 这些是我的代码。任何帮助将受到高度赞赏。非常感谢你。

MainPageMain.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:XamarinDemoApp"
         x:Class="XamarinDemoApp.MainPageMain"
         xmlns:ViewModels="clr-namespace:XamarinDemoApp.ViewModels;assembly=XamarinDemoApp"
         BackgroundColor="Teal"
         Title=" Title Bar">



 <ContentPage.BindingContext>
  <ViewModels:MainViewModel/>
   </ContentPage.BindingContext>


  <StackLayout Orientation="Vertical">

    <ListView ItemsSource="{Binding EmployeesList}"
          HasUnevenRows="True">
     <ListView.ItemTemplate>
      <DataTemplate>
      <ViewCell>
        <StackLayout Orientation="Horizontal">
          <Label Text="{Binding Name}"
                  FontSize="24"/>
          <Label Text="{Binding Department}"
                  FontSize="24"/>

        </StackLayout>

      </ViewCell>

     </DataTemplate>

     </ListView.ItemTemplate>



  </ListView>

   <Label Text="This is the Main Page"/>

   </StackLayout>
 </ContentPage>

MainViewModel.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using XamarinDemoApp.Models;
using XamarinDemoApp.Services;

namespace XamarinDemoApp.ViewModels
    {
public class MainViewModel : INotifyPropertyChanged
{    


    private List<Employee> _employeesList;

    public List<Employee> EmployeesList
    {
        get {return _employeesList;}
        set 
        {
            _employeesList = value;
            OnPropertyChanged();
        }
    }



    public MainViewModel()
    {

        InitializeDataAsync();
    }

    private async Task InitializeDataAsync()
    {
        var employeesServices = new EmployeesServices();

        EmployeesList = await employeesServices.GetEmployeesAsync();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
           PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }


    }
}

1 个答案:

答案 0 :(得分:2)

在您的代码中,您使用List<Employee>作为列表视图的来源。

请参阅此link。它说:

  

如果希望ListView在基础列表中添加,删除和更改项目时自动更新,则需要使用ObservableCollection。 ObservableCollection在System.Collections.ObjectModel中定义,就像List一样,只是它可以通知ListView任何更改

更新您的代码以使用ObservableCollection。

ObservableCollection<Employee> employees= new ObservableCollection<Employee>();

// Convert/Add your list in observable collection
foreach (Employee e in EmployeesList )
{ 
    employees.Add(new Employee { Name=e.Name,Department=e.Department } )
}

之后,您可以将其设置为ListView的源。

<强>更新

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using XamarinDemoApp.Models;
using XamarinDemoApp.Services;

namespace XamarinDemoApp.ViewModels
    {
public class MainViewModel : INotifyPropertyChanged
{    


    private ObservableCollection<Employee> _employeesList;

    public ObservableCollection<Employee> EmployeesList
    {
        get {return _employeesList;}
        set 
        {
            _employeesList = value;
            OnPropertyChanged();
        }
    }



    public MainViewModel()
    {

        InitializeDataAsync();
    }

    private async Task InitializeDataAsync()
    {
        var employeesServices = new EmployeesServices();

        var EmployeesListNew = await employeesServices.GetEmployeesAsync();

        foreach (Employee e in EmployeesListNew )
        { 
            EmployeesList.Add(new Employee { Name=e.Name,Department=e.Department } )
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
           PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }


    }
}