大家好,在這個博客中,我們將學習如何從我們的 .net Maui 應用程序發送 WhatsApp 消息。
MainPage.xaml
這是我們應用程序的 UI 代碼,有 2 個條目,一個用于手機號碼,一個用于消息,以及一個用于發送消息的按鈕。
<?xml version="1.0" encoding="utf-8" ?><ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MyApp.MainPage"> <VerticalStackLayout HorizontalOptions="CenterAndExpand" Spacing="20" VerticalOptions="CenterAndExpand"> <Frame BorderColor="Black" Padding="15,5,15,5"> <Entry x:Name="numberEntry" ReturnType="Next" MaxLength="10" PlaceholderColor="Gray" Keyboard="Numeric" Placeholder="Enter Mobile Number"/> </Frame> <Frame BorderColor="Black" Padding="15,5,15,5"> <Entry x:Name="messageEntry" ReturnType="Done" PlaceholderColor="Gray" Placeholder="Enter Your Message"/> </Frame> <Button Text="Send Whatsapp Message" BorderColor="Black" BorderWidth="1" FontAttributes="Bold" Padding="25,15,25,15" BackgroundColor="#25D366" Clicked="SendMessage"/> </VerticalStackLayout></ContentPage>
MainPage.xaml.cs
namespace MyApp;public partial class MainPage : ContentPage{ public MainPage() { InitializeComponent(); } /// <summary> /// Sends provided message on whatsapp number /// </summary> /// <param name="phoneNumber">Number on which message will be nt</param> /// <param name="message">Message to nd</param> private async void SendWhatsapp(string phoneNumber,string message) { bool supportsUri=await Launcher.Default.CanOpenAsync($"whatsapp://nd?phone=+{phoneNumber}&text={message}"); if (supportsUri) await Launcher.Default.OpenAsync($"whatsapp://nd?phone=+{phoneNumber}&text={message}"); el await App.Current.MainPage.DisplayAlert("Error", "Unable to open WhatsApp.", "OK"); } /// <summary> /// Send message button clicked /// </summary> /// <param name="nder">object</param> /// <param name="e">EventArgs</param> private void SendMessage(object nder, EventArgs e) { string number="+91 "+numberEntry.Text; SendWhatsapp(number, messageEntry.Text); }}
注意:我在發送 WhatsApp 消息之前添加了國家代碼。 您可以根據需要提供國家/地區代碼的下拉列表。
使用 Maui 的 Launcher 啟動 WhatsApp 以向特定的手機號碼發送消息。
感謝您閱讀我的博客直到這里。 快樂編碼。
關注七爪網,獲取更多APP/小程序/網站源碼資源!
本文發布于:2023-02-28 21:09:00,感謝您對本站的認可!
本文鏈接:http://m.newhan.cn/zhishi/a/1677731514101077.html
版權聲明:本站內容均來自互聯網,僅供演示用,請勿用于商業和其他非法用途。如果侵犯了您的權益請與我們聯系,我們將在24小時內刪除。
本文word下載地址:net send(net send命令).doc
本文 PDF 下載地址:net send(net send命令).pdf
| 留言與評論(共有 0 條評論) |