前言
.NET 多平台应用 UI (.NET MAUI) 是一个跨平台框架,用于使用 C# 和 XAML 创建本机移动和桌面应用。
使用 .NET MAUI,可从单个共享代码库开发可在 Android、iOS、macOS 和 Windows 上运行的应用。
.NET MAUI 是一款开放源代码应用,是 Xamarin.Forms 的进化版,从移动场景扩展到了桌面场景,并从头重新生成了 UI 控件,以提高性能和可扩展性。 如果以前使用过 Xamarin.Forms 来生成跨平台用户界面,那么你会注意到它与 .NET MAUI 有许多相似之处。 但也有一些差异。 通过使用 .NET MAUI,可使用单个项目创建多平台应用,但如果有必要,可以添加特定于平台的源代码和资源。 .NET MAUI 的主要目的之一是使你能够在单个代码库中实现尽可能多的应用逻辑和 UI 布局。
安装环境
安装功能
VS2022安装 【ASP.NET和Web开发】、【.NET Multi-platform App UI开发】、【.NET桌面开发】
配置程序源
【工具】–>【选项】–>【NuGet包管理器】–>【程序包源】,添加如下:
名称:MES_APP
源:http://172.:8081/repository/mes/index.json
安装虚拟机
Android设备管理器
- 还原解决方案
执行包还原
dotnet restore
- 网络配置
协议 http
地址 172
端口号 5500
- 登录账户
账户
密码
添加模块功能
原MES系统,模块管理,移动端添加模块功能
GlueNet.Mobile.Operation.Pages.MO1001, GlueNet.Mobile.Operation
项目结构
App.Mes.Core.Operation 核心操作
App.Mes.Mobile.Operation 移动端操作
App.Mes.Mobile.Quality 移动端质量
GlueNet.Mobile 移动端界面
GlueNet.Mobile.Core 移动端硬件传感器交互(IP、Mac地址)
GlueNet.Mobile.Service 移动端服务
GlueNet.Mobile.Core
IDeviceInfoService.cs 设备接口
public interface IDeviceInfoService : ISingletonDependency
{
string GetIpAddress();
string GetMacAddress();
}
DeviceInfoService.cs 接口实现
Android端,依赖注入,读取设备信息
Global.cs 全局变量
初始化的网络连接参数,登录状态的信息。
ApplicationInformation serverInformation = IocManager.Instance.Resolve<ApplicationInformation>(); List<TsKeyValueDto> keyValues = KeyValueService.GetSystemSettings(); serverInformation.ApplicationName = string.Concat(keyValues.FirstOrDefault(x => x.CCode == "ProductName")?.CValue, "-", keyValues.FirstOrDefault(x => x.CCode == "ProductVersion")?.CValue); serverInformation.TimeoutOfLogin = int.Parse(keyValues.FirstOrDefault(x => x.CCode == "TimeoutOfLogin")?.CValue ?? "240"); AppContext.Current.Token = serverInformation.Token;
IUpgradeService.cs 检查升级的接口
在 GlueNet.Mobile\Platforms\Android\UpgradeService.cs进行实现。
Navigator.cs 导航类
路由跳转相关的重载方法
Toaster.cs 消息框
GlueNet.Mobile
App.xaml 注册绑定路由
使用Navigator.NavigateToAsync("MO1002");
方法,根据路由名MO1002
,跳转MO1002Page
页面。
Shell.Current.Items.Add(new ShellContent() { Route = "MO1002", Content = new MO1002Page() });
MainPage.xaml 主页面
菜单页面,Clicked
单击事件,x:Name
参数名,与路由相关联。
<StackLayout Padding="10">
<Button Text="MO1002" x:Name="MO1002" Clicked="togo_Clicked"></Button>
<Button Text="注销" x:Name="logout" Clicked="logout_Clicked" VerticalOptions="EndAndExpand" Margin="0,10"></Button>
</StackLayout>
Pages前端页面
MO1002Page.xaml 前端设计界面
<VerticalStackLayout>
<Label
Text="你已打开MO1002"
VerticalOptions="Center"
HorizontalOptions="Center" />
<Button Text="返回" Margin="0,60" Clicked="OnReturnHomeClicked" />
</VerticalStackLayout>
MO1002Page.xaml.cs 后端代码,事件方法(需要手动绑定事件)