简体   繁体   English

如何显示从ASP.NET Web应用程序到Xamarin.Forms的数据?

[英]How to show data from ASP.NET Web Application to Xamarin.Forms?

I want all the records created in ASP.NET Web Application to be shown in my Mobile App Xamarin.Forms. 我希望在ASP.NET Web应用程序中创建的所有记录都可以在我的移动应用程序Xamarin.Forms中显示。 What's happening to my program is that I was able to create records in my Web Application and save it, but I wasn't able to make it appear in my Xamarin.Forms Mobile app. 我的程序正在发生的事情是,我能够在Web应用程序中创建记录并将其保存,但无法使其显示在Xamarin.Forms Mobile应用程序中。 I have created a MainViewModel that will get the records from the Web Application which I have binded to my MainPage. 我创建了一个MainViewModel,它将从已绑定到MainPage的Web应用程序中获取记录。 These are my codes: 这些是我的代码:

MainPageMain.xaml 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 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));

        }
    }
}

Make a separate API controller that you can use to call EmployeeList as a JSON object. 制作一个单独的API控制器,可用于将EmployeeList作为JSON对象调用。 That is the preferred way to do this kind of thing. 那是做这种事情的首选方法。 Example: 例:

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

   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asp.net WebAPI 调用无法从 Xamarin.Forms - asp.net WebAPI call not working from Xamarin.Forms 如何将图像保存在 ASP.NET Core MVC 中的文件夹中,该 MVC 来自 Xamarin.Forms ZE84ZDB60B939084CDB69 - How to save an image in a folder in ASP.NET Core MVC that comes from Xamarin.Forms Android? 如何从我的 Xamarin.Forms 应用程序将数据发送到 Web ZDB9742387014CA8DE6434ACE7 - How can i send data from my Xamarin.Forms application to a Web API 我们可以使用可移植类库(Xamarin.Forms Portable)与ASP.Net应用程序共享UI吗? 怎么样? - Can we use the Portable class library (Xamarin.Forms Portable) to share UI with ASP.Net application? How? 无法执行从Xamarin表单应用程序到ASP.NET Web API的放置,删除操作 - Not able to perform put,delete operation from xamarin forms application to asp.net web api 如何从ASP.NET MVC中的Web表单清除数据? - How to clean data from web forms in asp.net mvc? 如何在Xamarin Forms PCL应用程序中为HttpClient接受ASP.NET Web API控制器文件 - How to accept ASP.NET web API controller file for HttpClient in Xamarin Forms PCL application 如何将ASP.NET Web窗体应用程序从成员身份迁移到ASP.NET Identity 2.0? - How to migrate ASP.NET Web Forms application from Membership to ASP.NET Identity 2.0? Xamarin表单和Asp.Net Web Api - Xamarin Forms and Asp.Net Web Api 如何在ASP.NET Web窗体中显示变量值? - How to show a variable value in ASP.NET Web Forms?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM