如何使用从 Web Api 接收的数据加载 Xamarin 表单中的选择器? [英] How to load a Picker in Xamarin forms with data received from the Web Api?

查看:22
本文介绍了如何使用从 Web Api 接收的数据加载 Xamarin 表单中的选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,这是我的模型类Usuario"

So, this is my Model Class "Usuario"

此类具有外键IdTipoUsuario";这是从 SQL Server Management Studio 在我的数据库中声明的.所以我把它和IList"放在一起.属性:

This class has the Foreign key "IdTipoUsuario" that was declared in my database from SQL Server Management Studio. So I placed it here with the "IList" property:

namespace APPiGarbage.Models
{
    public class Usuario
    {
        public int IdUsuario { get; set; }
        public string Nome { get; set; }
        public string Foto { get; set; }
        public string Descricao { get; set; }
        public string Email { get; set; }
        public string Senha { get; set; }
        public IList<TipoUsuario> TiposUsuarios { get; set; }
        
    }
}

我有另一个班级 TipoUsuario.这个类有Nome"应该在选择器中绑定的属性.

I have this other class TipoUsuario. This class has the "Nome" property that is supposed to be Binding in the picker.

namespace APPiGarbage.Models
{
    public class TipoUsuario
    {
        public int IdTipoUsuario { get; set; }
        public string Nome { get; set; }
    }
}

对于我的 MVVM 模型,我尝试了很多代码,现在我什至不知道该放什么.说实话.

For my MVVM Model I tried whole lot codes and now I don't even know what to put in it. To be honest.

namespace APPiGarbage.ViewModel
{
    public class UsuarioViewModel 
    {
        public List<TipoUsuario> tipos { get; set; }

        public List<Usuario> GetTipos()
        {
            var tipos = new List<Usuario>()
            {

            };
            return tipos;
        }
    }

对于我的 ApiService 类,我有以下代码:

For my ApiService class I have this code:

namespace APPiGarbage.API
{
    public class ApiService
    {
        public const string Url = "http://10.0.2.2:44342/";
        public static async Task<List<TipoUsuario>> ObterTipoUsuarios()
        {
            try
            {
                HttpClient client = new HttpClient();
                string url = Url + "/api/TipoUsuario";
                string response = await client.GetStringAsync(url);
                List<TipoUsuario> tipos = JsonConvert.DeserializeObject<List<TipoUsuario>>(response);
                return tipos;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}

这是我的 UsuarioPage.xaml 中的选择器:

This is the Picker in my UsuarioPage.xaml:

<Picker Title="Selecione o Tipo de Usuario"
             ItemsSource="{Binding TipoUsuario}"
             ItemDisplayBinding="{Binding Nome}"/>

最后是 UsuarioPage.xaml.cs:

And finally this is the UsuarioPage.xaml.cs:

namespace APPiGarbage.Pages.UsuarioPage
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class UsuarioPage : ContentPage
    {
        public UsuarioPage()
        {
            InitializeComponent();
            BindingContext = new UsuarioViewModel();
        }
    }
}

感谢您抽出宝贵时间帮助我.

Thank you for taking your time to help me.

推荐答案

首先,在您的 xaml 中,您的 Picker 的 ItemsSource 应该绑定到您的 ViewModel 中的技巧.

First, in your xaml, your Picker's ItemsSource should bind to the tipos in your ViewModel.

<Picker Title="Selecione o Tipo de Usuario"
     ItemsSource="{Binding tipos}"
     ItemDisplayBinding="{Binding Nome}"/>

其次,您应该从 ViewModel 中的 Api 获取数据:

Second, you should get data from Api in ViewModel:

public class UsuarioViewModel
{
    public ObservableCollection<TipoUsuario> tipos { get; set; }

    public UsuarioViewModel() {

        Task<List<TipoUsuario>> task = ApiService.ObterTipoUsuarios();

        tipos = new ObservableCollection<TipoUsuario>(task.Result);

    }
}

然后绑定应该可以工作,如果您有任何问题,请随时问我.

Then the binding should work and feel free to ask me any question if you have.

这篇关于如何使用从 Web Api 接收的数据加载 Xamarin 表单中的选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆