当我试图从一个教程创建一个REST API时得到CS1503

时间:2019-11-17 07:36:03

标签: c# api xamarin.forms

我正在尝试通过教程与我的Xamarin表单应用程序中的Node js后端进行通信,并收到以下错误消息

Severity Code    Description  Project File    Line    Suppression State
Error  CS1503  Argument 1: cannot convert from 'Kula.MainPage' to 'Android.Content.Context'   Kula    C:\Users\1243g\Desktop\Kula\Kula\Kula\Kula\MainPage.xaml.cs 28  Active

该错误在维护活动中的this

我想知道自己在做什么错,因为这是我第一次尝试在下面创建rest API代码。我想我接近了。因此,有关如何使用正确的变量并对其进行修复的任何实施方式都将非常有用

主要

using Kula.API;
using Refit;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

 namespace Kula
{
// Learn more about making custom code visible in the Xamarin.Forms previewer
// by visiting https://aka.ms/xamarinforms-previewer
[DesignTimeVisible(false)]
public partial class MainPage : ContentPage
{
    // set variables 
    Entry RinputEmail;
    Entry RinputPassword;
    IMyAPI myAPI;
    APIRequestHelper apiRequestHelper;
    public MainPage()
    {
        InitializeComponent();

        myAPI = RestService.For<IMyAPI>("");
        apiRequestHelper = new APIRequestHelper(this , myAPI);

        RinputEmail = this.FindByName<Entry>("Remail");
        RinputPassword = this.FindByName<Entry>("Rpassword");

    }

    async private void GTLogin_Clicked(object sender, EventArgs e)
    {
        //navigate to Login page
        await Navigation.PushAsync (new Login());
    }

    async private void registerUser_Clicked(object sender, EventArgs e)
    {

     await apiRequestHelper.RequestRegisterUserAsync((RinputEmail.Text).ToString(),   (RinputPassword.Text).ToString());

    }

 }

}

API请求助手(一个用于登录,一个用于注册)

using Android.Content;
using Android.Widget;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

 namespace Kula.API
 {
  public class APIRequestHelper
 {
    Context context;
    IMyAPI myAPI;

    public APIRequestHelper(Context context, IMyAPI myAPI)
    {
        this.context = context;
        this.myAPI = myAPI;      
    }
    public async Task RequestRegisterUserAsync(string email, string password) 
    {
        if (string.IsNullOrEmpty(email))
        {
            Toast.MakeText(context, "no email detected", ToastLength.Short).Show();
        }
        if (string.IsNullOrEmpty(password))
        {
            Toast.MakeText(context, "no password detected", ToastLength.Short).Show();
        }

        //create params for request 
        Dictionary<string, object> data = new Dictionary<string, object>();
        data.Add("email", email);
        data.Add("password", password);

        string result = await myAPI.RegisterUser(data);
    }
    public async Task RequestLoginUserAsync(string email, string password)
    {
        if (string.IsNullOrEmpty(email))
        {
            Toast.MakeText(context, "no email detected", ToastLength.Short).Show();
        }

        if (string.IsNullOrEmpty(password))
        {
            Toast.MakeText(context, "no password detected", ToastLength.Short).Show();
        }

        //create params for request 
        Dictionary<string, object> data = new Dictionary<string, object>();
        data.Add("email", email);
        data.Add("password", password);

        string result = await myAPI.LoginUser(data);
    }
 }
 }

MyAPI

using Refit;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Kula.API
{

public interface IMyAPI
{
    [Post("/register")]
    Task<string> RegisterUser([Body(BodySerializationMethod.UrlEncoded)] Dictionary<string, object>  data);

    [Post("/login")]
    Task<string> LoginUser([Body(BodySerializationMethod.UrlEncoded)] Dictionary<string, object> data);
}    
}

0 个答案:

没有答案