MAUI 初体验 联合 WinForm让家里废弃的手机当做电脑副品用起来( 二 )

MAUI 发布 APK 供手机下载安装使用 右键 MAUI 项目>使用终端 输入如下命令

keytool -genkey -v -keystore myapp.keystore -alias key -keyalg RSA -keysize 2048 -validity 10000
项目文件中增加如下配置
<PropertyGroup Condition="$(TargetFramework.Contains('-android')) and '$(Configuration)' == 'Release'"><AndroidKeyStore>True</AndroidKeyStore><AndroidSigningKeyStore>myapp.keystore</AndroidSigningKeyStore><AndroidSigningKeyAlias>key</AndroidSigningKeyAlias><AndroidSigningKeyPass></AndroidSigningKeyPass><AndroidSigningStorePass></AndroidSigningStorePass></PropertyGroup>发布
dotnet publish -f:net6.0-android -c:Release /p:AndroidSigningKeyPass=youpwd /p:AndroidSigningStorePass=youpwd替换命令中的 youpwd
参考官方 发布 文章 https://learn.microsoft.com/zh-cn/dotnet/maui/android/deployment/publish-cli
8:创建 winform 程序 使用 .net framework 4.7 主要使用全局快捷方式 需要用到 user32.dll 主程序代码【MAUI 初体验 联合 WinForm让家里废弃的手机当做电脑副品用起来】 public partial class MainForm : Form{int crtlSpace;//定义快捷键public static IMqttClient _mqttClient;public MainForm(){InitializeComponent();crtlSpace = "CtrlSpace".GetHashCode();//组合键模式 None = 0,Alt = 1,Ctrl = 2,Shift = 4,WindowsKey = 8Win32Api.RegisterHotKey(this.Handle, crtlSpace, 2, (int)Keys.Space);}/// <summary>/// 热键/// </summary>/// <param name="m"></param>protected override void WndProc(ref Message m){const int WM_HOTKEY = 0x0312;int wParam = (int)m.WParam;switch (m.Msg){case WM_HOTKEY:if (wParam == crtlSpace){var text = Clipboard.GetText();string result= YouDao.GetFanYiResult(text);Publish(result);AppentTextLog("触发:【" + text + "】->" + System.DateTime.Now);}break;}base.WndProc(ref m);}/// <summary>/// 隐藏窗体/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button_closed_Click(object sender, EventArgs e){this.Visible = false;}private void MainForm_Load(object sender, EventArgs e){MqttInit();}/// <summary>/// 窗口拖拽/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void MainForm_MouseDown(object sender, MouseEventArgs e){Win32Api.ReleaseCapture();Win32Api.SendMessage(this.Handle, Win32Api.WM_SYSCOMMAND, Win32Api.SC_MOVE + Win32Api.HTCAPTION, 0);}/// <summary>/// 双击右下角图标显示隐藏/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e){if (e.Button == MouseButtons.Left){this.Visible = !this.Visible;}}/// <summary>/// 右键右下角图标退出程序/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void 退出ToolStripMenuItem_Click(object sender, EventArgs e){//卸载注册热键Win32Api.UnregisterHotKey(this.Handle, this.crtlSpace);//关闭窗口this.Close();}/// <summary>/// 初始化MQTT/// </summary>public void MqttInit(){string clientId=Guid.NewGuid().ToString();var optionsBuilder = new MqttClientOptionsBuilder().WithTcpServer("xxx.xxx.xxx.xxx", 1883) // 要访问的mqtt服务端的 ip 和 端口号//.WithCredentials("admin", "123456") // 要访问的mqtt服务端的用户名和密码.WithClientId(clientId) // 设置客户端id.WithCleanSession().WithTls(new MqttClientOptionsBuilderTlsParameters{UseTls = false// 是否使用 tls加密});var clientOptions = optionsBuilder.Build();_mqttClient = new MqttFactory().CreateMqttClient();_mqttClient.ConnectedAsync += _mqttClient_ConnectedAsync; // 客户端连接成功事件_mqttClient.DisconnectedAsync += _mqttClient_DisconnectedAsync; // 客户端连接关闭事件_mqttClient.ConnectAsync(clientOptions);}/// <summary>/// 客户端连接关闭事件/// </summary>/// <param name="arg"></param>/// <returns></returns>private Task _mqttClient_DisconnectedAsync(MqttClientDisconnectedEventArgs arg){AppentTextLog($"MQTT服务已关闭");return Task.CompletedTask;}/// <summary>/// 客户端连接成功事件/// </summary>/// <param name="arg"></param>/// <returns></returns>private Task _mqttClient_ConnectedAsync(MqttClientConnectedEventArgs arg){AppentTextLog($"MQTT服务已连接^v^");_mqttClient.SubscribeAsync("pc-helper", MqttQualityOfServiceLevel.AtLeastOnce);return Task.CompletedTask;}/// <summary>/// 发送MQTT消息/// </summary>/// <param name="data"></param>public void Publish(string data){var message = new MqttApplicationMessage{Topic = "pc-helper",Payload = Encoding.UTF8.GetBytes(data),QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,Retain = true// 服务端是否保留消息 。true为保留,如果有新的订阅者连接,就会立马收到该消息 。};_mqttClient.PublishAsync(message);}/// <summary>/// 更新窗体文本/// </summary>/// <param name="text"></param>void AppentTextLog(string text){Action act = delegate (){textBox_log.AppendText(Environment.NewLine + text + Environment.NewLine);textBox_log.ScrollToCaret();if (textBox_log.Text.Count() > 100000){textBox_log.Clear();}};this.Invoke(act);}}

推荐阅读