【问题标题】:How to show data from ASP.NET Web Application to Xamarin.Forms?如何将 ASP.NET Web 应用程序中的数据显示到 Xamarin.Forms?
【发布时间】:2016-05-30 14:34:28
【问题描述】:

我希望在 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));

        }
    }
}

【问题讨论】:

    标签: c# asp.net xaml xamarin xamarin.forms


    【解决方案1】:

    制作一个单独的 API 控制器,您可以使用它以 JSON 对象的形式调用 EmployeeList。这是做这种事情的首选方式。示例:

    public class EmployeeApiController : ApiController
    {
       [HttpGet]
       public async Task<List<Employee>> Get()
       {
          return await employeesServices.GetEmployeesAsync();
    
       }
    }
    

    【讨论】:

    • 是的,有点。我会小心地打破单一责任模式并直接在控制器内完成所有与数据库相关的工作,但是是的,基本上就是这样。此外,您应该让您的网站订阅相同的 API,而不是复制代码。完成后,您可以从 ApiController 获取数据,就像您的 Xamarin 应用一样。
    • 我就是这么做的。我提供的链接内容是我的实际代码,但仍然没有用。我只是这里的新手,所以也许你可以看看我的代码。对不起先生。
    • 你能把你的Employee类包括进来吗?是否标记为可序列化?
    • 你指的是这个类吗?这是 Employee.cs pastie.org/10857695#9 或者您正在寻找我的EmployeesServices 课程? pastie.org/10857698
    • 就是那个,它没有标记[Serializable]或[DataContract],所以本质上Visual STudio不知道如何将它序列化为JSON,因此您可以在另一端使用它。只需将其中一个(和相应的命名空间)添加到类的顶部,你就应该是金色的
    猜你喜欢
    • 1970-01-01
    • 2013-07-23
    • 1970-01-01
    • 2011-06-14
    • 2014-05-16
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多