背景介绍
因实验室出差项目需求,需要基于.net和C#的基础上开发一个APP
框架介绍
调研之后,决定前端框架选用xamarin,后端选用基于ASP.net的Web Api
选用xamarin原因:框架较完善,一套代码可以发布两套平台,包括Android端和IOS端,且生态算是比较完善,微软有较完善的开发文档,UI展示也有比较完善的xamarin.forms可以用。
选用ASP.Net原因:基于MVC架构,微软的web Api服务可以比较好的在移动端、网页端进行迁移
,且可以进行基于http协议的前后端通信,因为之前在做小程序的时候也是用的这一套,因此就直接迁移过来移动端了
实现展示
以实现简单的登录界面为例,登录界面有涉及到:
http post:
将用户名和密码以参数的形式传到后端
http get:
请求后端,后端返回数据
先进入注册界面,然后按下确定按钮,跳转到欢迎页面
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210716144104111.gif
核心代码
注册界面代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using AppDevelop.Models;
using Newtonsoft.Json;
using Xamarin.Forms;
namespace AppDevelop.Views
{
public partial class SignInPage : ContentPage
{
private string name, passwd;
public SignInPage()
{
InitializeComponent();
}
async void OnSaveButtonClicked(object sender, EventArgs e)
{
//将输入的用户名和密码插入后台数据库
var uri = new Uri(string.Format(Constants.RestUrl + "signInto ", string.Empty));
using (var http = new HttpClient())
{
var data = new
{
name,
passwd
};
string json = JsonConvert.SerializeObject(data);
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = await http.PostAsync(uri, content);
if (response.IsSuccessStatusCode)
{
// Navigate backwards 导航返回上一页
//await Shell.Current.GoToAsync(nameof(WelcomePage));
await Navigation.PushAsync(new WelcomePage());
}
}
}
async void OnDeleteButtonClicked(object sender, EventArgs e)
{
// Navigate backwards
await Shell.Current.GoToAsync("..");
}
void OnEditorCompleted1(object sender, EventArgs e)
{
this.name = ((Editor)sender).Text;
}
void OnEditorCompleted2(object sender, EventArgs e)
{
this.passwd = ((Editor)sender).Text;
}
}
}
欢迎页面代码
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace AppDevelop.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class WelcomePage : ContentPage
{
private string welcomeNum;
public string Welcome
{
get
{
return "欢迎,你是第 " + welcomeNum + " 位来访者!";
}
set
{
welcomeNum = value;
}
}
public WelcomePage()
{
BindingContext = this;
//创建一个子线程,暂时阻塞主线程,等待异步函数执行完,再向下执行代码,这里的等待函数返回类型不能为空
Task.Run(async () => { await GetWelcomeNum(); }).Wait();
InitializeComponent();
}
public async Task GetWelcomeNum()
{
var uri = new Uri(string.Format(Constants.RestUrl + "common ", string.Empty));
using (var http = new HttpClient())
{
var response = await http.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
JObject con = JObject.Parse(content);
Welcome = con["num"].ToString();
}
else
{
throw new Exception();
}
}
}
}
}
注册插入数据库后端代码
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MiniprogramApi.Models;
using Microsoft.EntityFrameworkCore;
namespace AppApi.Controllers
{
[ApiController]
[Route("api/signInto")]
public class SingIntoController : ControllerBase
{
private readonly DBCenterContext _context;
public SingIntoController(DBCenterContext context)
{
_context = context;
}
[HttpPost]
public ActionResult SignInto([FromBody] SignInto user)
{
using var conn = _context.Database.GetDbConnection();
using var command = conn.CreateCommand();
try
{
string name = user.Name;
string passwd = user.Passwd;
conn.Open();
command.CommandText = string.Format("Insert into SYS_User (name,psw,createtime) " +
"Values('{0}','{1}',GETDATE())", name, passwd);
command.ExecuteReader();
}
catch (Exception err)
{
Console.WriteLine(err.ToString());
}
return Ok(new
{
isSucess = true
}) ;
}
}
}
后续
自己只做了登录这么一个简单的功能,希望后面有机会再拓展。
涉及到一些网络编程的知识是重点,例如异步请求、多线程等,在做的过程中的笔记参考xamarin学习记录