树莓派开发扩展十二 -C#编写客户端控制小车 Xamarin.Forms

1、效果

2、前台代码

XML 复制代码
<StackLayout>
    <Frame
        Padding="24"
        BackgroundColor="#2196F3"
        CornerRadius="0">
        <Label
            FontSize="30"
            HorizontalTextAlignment="Center"
            Text="树霉派小车控制器"
            TextColor="White" />
    </Frame>
 
    <Label
        x:Name="lbInfo"
        Padding="30,10,30,10"
        FontSize="18"
        Text="Wait Info!" />
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition Height="50" />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <StackLayout Grid.ColumnSpan="3" Orientation="Horizontal">
            <Label
                Margin="15,0,0,0"
                Text="IP地址:"
                VerticalTextAlignment="Center" />
            <Editor x:Name="tbIP" WidthRequest="200" />
        </StackLayout>

        <StackLayout
            Grid.Row="1"
            Grid.ColumnSpan="3"
            Orientation="Horizontal">
            <Label Text="通讯端口:" VerticalTextAlignment="Center" />
            <Editor x:Name="tbPort" WidthRequest="200" />
        </StackLayout>

        <Button
            Grid.Row="0"
            Grid.Column="3"
            Clicked="Button_Click"
            Text="连接服务" />
        <Button
            Grid.Row="1"
            Grid.Column="3"
            Clicked="Button_Click"
            Text="断开服务" />

        <Button
            Grid.Row="2"
            Grid.Column="0"
            Clicked="Button_Click"
            Text="手动模式" />
        <Button
            Grid.Row="2"
            Grid.Column="1"
            Clicked="Button_Click"
            Text="自动模式" />
        <Button
            Grid.Row="2"
            Grid.Column="2"
            Clicked="Button_Click"
            Text="智能模式" />


        <Button
            Grid.Row="3"
            Grid.Column="0"
            Clicked="Button_Click"
            Text="加速" />
        <Button
            Grid.Row="3"
            Grid.Column="1"
            Clicked="Button_Click"
            Text="减速" />

        <Button
            x:Name="butFornt"
            Grid.Row="4"
            Grid.Column="1"
            Pressed="ButPressed_Click"
            Released="ButReleased_Click"
            Text="前" />
        <Button
            x:Name="butLeft"
            Grid.Row="5"
            Grid.Column="0"
            Pressed="ButPressed_Click"
            Released="ButReleased_Click"
            Text="左" />
        <Button
            x:Name="butRight"
            Grid.Row="5"
            Grid.Column="2"
            Pressed="ButPressed_Click"
            Released="ButReleased_Click"
            Text="右" />
        <Button
            x:Name="butBack"
            Grid.Row="6"
            Grid.Column="1"
            Pressed="ButPressed_Click"
            Released="ButReleased_Click"
            Text="后" />
    </Grid>
</StackLayout>

3、后台代码

cs 复制代码
 public partial class MainPage : ContentPage
 {
     TcpClient client = null;
     public MainPage()
     {
         InitializeComponent();
          
     }

     private void Button_Click(object sender, EventArgs e)
     {
         Button but = sender as Button;
         if (but != null)
         {
             string tag = but.Text;
             switch (tag)
             {
                 case "连接服务":
                     try
                     {
                         IPAddress address = IPAddress.Parse(tbIP.Text.Trim());
                         int port = Convert.ToInt32(tbPort.Text.Trim());
                         IPEndPoint endPoint = new IPEndPoint(address, port);
                         client = new TcpClient();
                         client.Connect(endPoint);
                         SetInfo("连接服务器成功!");
                     }
                     catch (Exception ex)
                     {
                         SetInfo("连接服务器异常:" + ex.Message);

                     }
                     break;
                 case "断开服务":
                     try
                     {
                         client.Close();
                         SetInfo("断开服务器成功!");


                     }
                     catch (Exception ex)
                     {
                         SetInfo("断开服务器异常:" + ex.Message);

                     }
                     break;
                 case "手动模式":
                     SendMsgToServer("s");
                     break;
                 case "自动模式":
                     SendMsgToServer("a");
                     break;
                 case "智能模式":
                     SendMsgToServer("mu");
                     break;

                 case "加速":
                     SendMsgToServer("up");
                     break;
                 case "减速":
                     SendMsgToServer("down");
                     break;
                 default:
                     SetInfo("按钮关联事件失败");
                     break;
             }
          
         }

     }

     private void SetInfo(string msg)
     {
         lbInfo.Text = msg;
     }



     private void ButReleased_Click(object sender, EventArgs e)
     {
         Button but = sender as Button;
         if (but != null)
         {
             string tag = but.Text;
             switch (tag)
             {
                 case "前":
                 case "后":
                 case "左":
                 case "右":
                     SendMsgToServer("s");
                     break;
                 default:
                     SetInfo("按钮关联事件失败");
                     break;
             }
         }
     }

     private void SendMsgToServer(string msg)
     {
         if (client == null)
         {
             return;
         }
         try
         {
             byte[] data = System.Text.Encoding.ASCII.GetBytes(msg);
             client.GetStream().Write(data, 0, data.Length);
             SetInfo("发送信息至服务器:" + msg);
         }
         catch (Exception ex)
         {
             SetInfo("发送信息至服务器异常:" + ex.Message);

         }


     }

     private void ButPressed_Click(object sender, EventArgs e)
     {
         Button but = sender as Button;
         if (but != null)
         {
             string tag = but.Text;
             switch (tag)
             {
                 case "前":
                     SendMsgToServer("f");
                     break;
                 case "后":
                     SendMsgToServer("b");
                     break;
                 case "左":
                     SendMsgToServer("l");
                     break;
                 case "右":
                     SendMsgToServer("r");
                     break;
                 default:
                     SetInfo("按钮关联事件失败");
                     break;
             }
         }
     }
 }
相关推荐
合作小小程序员小小店1 小时前
桌面开发,拼车管理系统开发,基于C#,winform,sql server数据库
开发语言·数据库·sql·microsoft·c#
wangnaisheng3 小时前
【C#】Newtonsoft.Json、System.Text.Json 解析Json串的对比
c#
自由的好好干活16 小时前
使用Qoder编写ztdaq的C#跨平台示例总结
linux·windows·c#·qoder
FuckPatience17 小时前
C# 实现元素索引由1开始的链表
开发语言·链表·c#
我是唐青枫21 小时前
C#.NET 范围与索引(Range、Index)完全解析:语法、用法与最佳实践
c#·.net
烛阴1 天前
从`new()`到`.DoSomething()`:一篇讲透C#方法与构造函数的终极指南
前端·c#
深海潜水员1 天前
【MonoGame游戏开发】| 牧场物语实现 第一卷 : 农场基础实现 (下)
vscode·游戏·c#·.net·monogame
合作小小程序员小小店1 天前
图书管理系统,基于winform+sql sever,开发语言c#,数据库mysql
开发语言·数据库·sql·microsoft·c#
大侠课堂1 天前
C#经典面试题100道
开发语言·c#
时光追逐者2 天前
Visual Studio 2026 现已正式发布,更快、更智能!
ide·c#·.net·visual studio