【问题标题】:Connecting Xamarin.Forms to Web Services将 Xamarin.Forms 连接到 Web 服务
【发布时间】:2016-09-28 21:25:31
【问题描述】:

大家好。我希望在 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 MainPage"/>

 </StackLayout>

MainViewModel.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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));
        }
    }
}

【问题讨论】:

  • 您至少问过这个问题的一些变体 4 次。请不要重复发布相同的问题。
  • @Jason 对不起,先生。只是这个问题我还没有答案。也许你可以帮助我。
  • 您的 MainPageMain 代码在哪里?您是否能够检索数据但不能显示它?还是没有检索到数据?
  • 这是我的 MainPageMain.xaml.cs (pastie.org/10868664) 背后的代码。我可以检索,但无法在手机中显示。
  • @Christine 这是我的 MainPageMain.xaml.cs (pastie.org/10868664) 背后的代码。我可以检索,但无法在手机中显示。

标签: asp.net mvvm asp.net-web-api xamarin.android xamarin.forms


【解决方案1】:

你应该使用ObservableCollections,像这样:

    private ObservableCollection<Employee> _employeesList;
    public ObservableCollection<Employee> EmployeesList
    {
        get { return _employeesList; }
        set { Set(() => EmployeesList, ref _employeesList, value); }
    }

然后

    private async Task InitializeDataAsync()
    {
        var employeesServices = new EmployeesServices();
        var employees = await employeesServices.GetEmployeesAsync();
        EmployeesList = new ObservableCollection<Employee>(employees);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 2019-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    • 1970-01-01
    相关资源
    最近更新 更多