【问题标题】:Data Binding in Xamarin.FormsXamarin.Forms 中的数据绑定
【发布时间】:2016-09-25 11:47:29
【问题描述】:

我希望在 ASP.NET Web 应用程序中创建的所有记录都显示在我的移动应用程序 Xamarin.Forms 中。我的程序发生的事情是我能够在我的 Web 应用程序中创建记录,但我无法将它绑定到我的 Xamarin.Forms 移动应用程序中。我创建了一个 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));
    }


    }
}

【问题讨论】:

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


    【解决方案1】:

    在您的代码中,您使用List&lt;Employee&gt; 作为列表视图的来源。

    请参阅此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));
        }
    
    
        }
    }
    

    【讨论】:

    • 我应该把代码放在哪里,先生?我文件的哪一部分?我很抱歉我只是这里的新手。它应该看起来像这样吗? pastie.org/10854692
    • 您可以在 InitializeDataAsync 方法中执行此操作。将 _employeesList 的数据类型更改为 ObservableCollection&lt;Employee&gt;
    • _employeesList 的数据类型不在“InitializeDataAsync”方法中。我很难理解,对不起,先生。您能否编辑我的代码并将其粘贴到此处pastie.org。单击“创建粘贴”按钮并复制其 URL。那你把链接发给我好吗?非常感谢先生。
    • 它不工作,先生。我创建的标签是唯一出现的东西。 ''
    • 了解如何在 Visual Studio 中调试代码。在你想在调试模式下查看程序执行或某些值的地方放置一个断点。msdn.microsoft.com/en-us/library/y740d9d3.aspx
    猜你喜欢
    • 1970-01-01
    • 2014-08-05
    • 2020-09-12
    • 2018-12-09
    • 2014-11-21
    • 2016-09-30
    • 2018-08-07
    • 1970-01-01
    • 2022-01-11
    相关资源
    最近更新 更多