2016-05-30 8 views
0

ASP.NET Webアプリケーションで作成されたすべてのレコードを、自分のモバイルアプリケーションのXamarin.Formsに表示します。私のプログラムで起こっていることは、Webアプリケーションでレコードを作成して保存することができたが、Xamarin.Forms Mobileアプリに表示させることができなかったことだ。私はメインページにバインドしたWebアプリケーションからレコードを取得するMainViewModelを作成しました。 これらは私のコードです:ASP.NET WebアプリケーションからXamarin.Formsにデータを表示するには?

<?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 MainPageMain.xaml

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)); 

     } 
    } 
} 

答えて

1

あなたはJSONオブジェクトとしてにEmployeeListを呼び出すために使用できる別のAPIコントローラを作ります。それはこの種のことをするのに好ましい方法です。例:

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

    } 
} 
+0

このようなSir? http://pastie.org/10857684 –

+0

はい、一種です。私はSingle Reponsability Patternを破るように注意し、データベース内のすべてのものをコントローラの中で直接行いますが、基本的にはそうです。さらに、コードを複製するのではなく、同じAPIをウェブサイトに登録する必要があります。完了したら、あなたはXamarinアプリのように、ApiControllerからデータを取得します。 –

+0

それは私がやったことです。私が提供したリンクの内容は私の実際のコードですが、それでも動作しませんでした。私はちょうどここの初心者ですので多分あなたは私のコードを見ることができます。申し訳ありませんでした。 –

関連する問題