C# 当我试图从教程中创建RESTAPI时获得CS1503
我正试图从教程中的Xamarin forms应用程序与我的node js后端通信,结果出现以下错误 该错误在Mainactivity中的C# 当我试图从教程中创建RESTAPI时获得CS1503,c#,api,xamarin.forms,C#,Api,Xamarin.forms,我正试图从教程中的Xamarin forms应用程序与我的node js后端通信,结果出现以下错误 该错误在Mainactivity中的下 我想知道我做错了什么,因为这是我第一次尝试在下面创建RESTAPI代码。我想我很接近了。因此,任何关于如何使用正确变量并修复它的实现都将非常有用 Main using Kula.API; using Refit; using System; using System.Collections.Generic; using System.ComponentMod
下
我想知道我做错了什么,因为这是我第一次尝试在下面创建RESTAPI代码。我想我很接近了。因此,任何关于如何使用正确变量并修复它的实现都将非常有用
Main
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());
}
}
}
使用Kula.API;
使用改装;
使用制度;
使用System.Collections.Generic;
使用系统组件模型;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用Xamarin.Forms;
库拉
{
//了解有关使自定义代码在Xamarin.Forms预览器中可见的更多信息
//参观https://aka.ms/xamarinforms-previewer
[设计时间可见(错误)]
公共部分类主页:ContentPage
{
//设置变量
输入电子邮件;
输入密码;
IMyAPI myAPI;
APIRequestHelper APIRequestHelper;
公共主页()
{
初始化组件();
myAPI=RestService.For(“”);
apiRequestHelper=新的apiRequestHelper(这个,myAPI);
RinputEmail=this.FindByName(“Remail”);
RinputPassword=this.FindByName(“Rpassword”);
}
异步私有void GTLogin_已单击(对象发送方,事件参数e)
{
//导航到登录页面
等待Navigation.PushAsync(新登录());
}
异步私有无效注册表用户已单击(对象发送方,事件参数e)
{
等待apiRequestHelper.RequestRegisterUserAsync((rinputeEmail.Text).ToString(),(RinputPassword.Text).ToString());
}
}
}
API请求帮助程序(一个用于登录,一个用于注册)
使用Android.Content;
使用Android.Widget;
使用制度;
使用System.Collections.Generic;
使用系统文本;
使用System.Threading.Tasks;
名称空间Kula.API
{
公共类APIRestHelper
{
语境;
IMyAPI myAPI;
公共APIRestHelper(上下文,IMyAPI myAPI)
{
this.context=上下文;
this.myAPI=myAPI;
}
公共异步任务请求注册表同步(字符串电子邮件、字符串密码)
{
if(string.IsNullOrEmpty(电子邮件))
{
Toast.MakeText(上下文,“未检测到电子邮件”,ToastLength.Short).Show();
}
if(string.IsNullOrEmpty(密码))
{
Toast.MakeText(上下文,“未检测到密码”,ToastLength.Short).Show();
}
//为请求创建参数
字典数据=新字典();
数据。添加(“电子邮件”,电子邮件);
数据。添加(“密码”,密码);
字符串结果=等待myAPI.RegisterUser(数据);
}
公共异步任务RequestLoginUserAsync(字符串电子邮件、字符串密码)
{
if(string.IsNullOrEmpty(电子邮件))
{
Toast.MakeText(上下文,“未检测到电子邮件”,ToastLength.Short).Show();
}
if(string.IsNullOrEmpty(密码))
{
Toast.MakeText(上下文,“未检测到密码”,ToastLength.Short).Show();
}
//为请求创建参数
字典数据=新字典();
数据。添加(“电子邮件”,电子邮件);
数据。添加(“密码”,密码);
字符串结果=等待myAPI.LoginUser(数据);
}
}
}
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);
}
}
使用Refit;
使用制度;
使用System.Collections.Generic;
使用系统文本;
使用System.Threading.Tasks;
名称空间Kula.API
{
公共接口IMyAPI
{
[邮寄(“/登记册”)]
任务注册器([Body(BodySerializationMethod.UrlEncoded)]字典数据);
[发布(“/登录”)]
任务登录用户([Body(BodySerializationMethod.UrlEncoded)]字典数据);
}
}
和。。。这在哪一行happen@TheGeneral对不起,我应该说得更具体些。当我初始化APIRestHelper值时,它位于“APIRestHelper=new APIRestHelper(this,myAPI);”行的“public MainPage()”中。youtuber说我应该尝试使用“上下文”而不是“这个”,但实际上仍然没有用。如果这有帮助的话,我想你是在传递你的页面,它期望你的申请/表格在哪里context@TheGeneral我将如何通过应用程序上下文而不是页面,因为在视频中,他只是“这个”。或者我需要实现一个新的api吗?@TheGeneral我不知道我在做RESTAPI的时候是做错了什么,还是只是做错了一点。任何信息或实现都会很棒
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);
}
}
}
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);
}
}