您好,
会员登录 快速注册
退出 ( 条未读消息)
关于本站 意见反馈 首页

公告:小宅博客网可以开发票了,需要发票的,去群里找群主哈!!
全部文章分类
  • 人工智能 >

  • 编程语言 >

  • WPF系列 >

  • ASP.NET系列 >

  • Linux >

  • 数据库 >

  • 嵌入式 >

  • WEB技术 >

  • PLC系列 >

  • 微服务与框架 >

  • 小宅DIY >

  • 学习资料 >

OpenCv基础 ANN车牌识别 yolov5车牌识别 指针式仪表识别 ROS系列 YOLO Halcon Detectron2 昇腾AI ChatGPT在线体验 英伟达JETSON ChatGLM ChatTTS FunASR 地平线 ByteTrack 魔搭社区 LangChain
C C# C++ Python Java Go
WPF
ASP.NET小功能 GPS定位系统-MVC GPS定位系统-VUE ASP.NET WebRTC
Linux Linux内核 Shell MakeFile
MySql SqlServer Oracle
STM8 STM32 51单片机
VUE入门 HTML JavaScript CSS layui镜像网站 ElementUi中文官网 element-plus 图标
三菱 欧姆龙 西门子 施耐德 松下 台达
IOTSharp IOTGateway ABP FRAMEWORK Docker
亚克力音响 编程仙途:智驭万法
面试题与技巧 Python入门技能树 微软C#教程
首页 编程之美 工具下载 全国就业 流量地图 文心一言
GPS定位系统-MVC
.NET6.0 GPS定位系统介绍(物联网) 系列源码下载 1、新建.net core web工程 2、添加自定义登录页面 3、添加百度地图页面 4、添加后台日志系统 5、添加mysql数据存储 6、添加SqlServer数据存储(额外内容) 7、用户注册与登录功能实现 8、添加坐标报表页面(用于遍历地图坐标) 9、前后端数据交互与报表数据展示 10、报表数据的编辑与修改 11、用户登录与退出 12、自定义GPRS通讯协议 13、添加TCP通讯功能(接收) 14、添加TCP通讯功能(发送) 15、模拟GPRS数据通讯 16、设备链接状态检测与提示 17、新建Windows Server虚拟机 18、服务器IIS运行环境配置 19、服务器.net程序发布 20、关于如何配置.net3.1框架 21、关于如何配置.net5.0框架 22、WinForm版地图上位机(带数据库和TCP功能) 23、WPF版地图上位机(带数据库和TCP功能) 24、公网映射与外网通讯 25、GPS协议与AT命令流程说明 26、GPRS协议与AT命令流程说明 27、STM32、A9G硬件连接图 28、STM32开发环境搭建 29、STM32 GPS/GPRS通讯功能实现 30、STM32 GPS定位数据上报服务器 31、室外最终效果演示 32、结束语
22、WinForm版地图上位机(带数据库和TCP功能)
24、公网映射与外网通讯
激萌の小宅 小宅博客网 GPS定位系统-MVC

文章作者:激萌の小宅

促销:¥0

价格:¥0

配送方式: 购买后立即生效(如购买异常,请联系站长)
付款之后一定要等待自动跳转结束,否则购买可能会失败
  • 0 天

    有效期

  • 0

    总销量

  • 2

    累计评价

WPF版地图上位机 - (第二十三讲)

视频讲解如下:


工程源码下载:GPS定位系统系列教程源码下载


        WPF版本的上位机使用了.net4.8的框架,同时也集成前面的DbEntity和Link两个类库,开发环境还是VS2022。参考文章:

        《百度地图上显示GPS坐标》

        《C#环境下使用EF操作MySql》

        《如何实现TCP通信》


1、集成事项

集成后的代码如下:

QQ截图20220927090359.jpg


界面布局图如下:

QQ截图20220927093609.jpg

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="480" Width="950">
    <Grid ShowGridLines="False" Background="#FF0BC3F3">
        <Grid.RowDefinitions>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"></ColumnDefinition>
            <ColumnDefinition Width="250"></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Label Grid.Row="0" Grid.Column="0" Margin="5,5,5,0" Content="本机主机地址:" FontSize="18" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <ComboBox Grid.Row="1" Grid.Column="0" Margin="5,0,5,5" x:Name="ComboBoxList" FontSize="18"/>
        <Label Grid.Row="2" Grid.Column="0" Margin="5,5,5,0" Content="本机主机端口:" FontSize="18" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <TextBox Grid.Row="3" Grid.Column="0" Margin="5,5,5,5" TextWrapping="Wrap" FontSize="18" x:Name="MyPort" Text="2233" />

        <Button Grid.Row="4" Grid.Column="0" Margin="5,5,5,5" FontSize="18" Content="打开网络" x:Name="OpenTcp" Click="OpenTcp_Click"/>
        <Button Grid.Row="5" Grid.Column="0" Margin="5,5,5,5" FontSize="18" Content="清空日志" x:Name="ClearLog" Click="ClearLog_Click"/>
        <Button Grid.Row="6" Grid.Column="0" Margin="5,5,5,5" FontSize="18" Content="主动请求" x:Name="ReadData" Click="ReadData_Click" />
        <Button Grid.Row="7" Grid.Column="0" Margin="5,5,5,5" FontSize="18" Content="清除地图" x:Name="ClearMap" Click="ClearMap_Click"/>

        <ListBox Grid.Row="0" Grid.Column="1" Grid.RowSpan="9" Margin="5,5,5,5" x:Name="ListBoxLog" FontSize="8" BorderThickness="10"
                 ScrollViewer.HorizontalScrollBarVisibility="Disabled" Background="Black"></ListBox>

        <WebBrowser Grid.Row="0" Grid.Column="2" Grid.RowSpan="9" Margin="5,5,5,5" x:Name="MyBrowser" VerticalAlignment="Stretch"
                  HorizontalAlignment="Center" />
    </Grid>
</Window>


MainWindow.xaml.cs文件代码如下:

using DbEntity;
using DbEntity.Tables;
using Link;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfApp
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        // <summary>
        /// 窗体日志信息
        /// </summary>
        public static ObservableCollection<TextBlock> ListBoxLogs { get; set; } = new ObservableCollection<TextBlock>();

        public MainWindow()
        {
            InitializeComponent();

            // 关联到数据
            ListBoxLog.ItemsSource = ListBoxLogs;

            // 绑定IP数据
            ComboBoxList.Items.Add("127.0.0.1");
            ComboBoxList.Items.Add(GetLocalIP());
            ComboBoxList.SelectedIndex = 0;

            // 启动数据库
            DbInit();
            MyBrowser.Navigate(new Uri(Directory.GetCurrentDirectory() + @"\html\index.html"));
        }

        #region 数据库
        /// <summary>
        /// 启动数据库
        /// </summary>
        private void DbInit()
        {
            try
            {
                wRes.MyDb = new MyDbContext() { config = wRes.SqlStr };
                wRes.MyDb.Database.EnsureCreated();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
        #endregion

        #region TCP网络
        /// <summary>
        /// 启动TCP Server
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenTcp_Click(object sender, RoutedEventArgs e)
        {
            if (Link.Res.TSC == null || !Link.Res.TSC.IsOpen())
            {
                // 定义一个设备
                Link.Res.Socs.Add(new Link.Equipment.GpsModel() { equi = 0x01, equino = 1.IntToByte() });

                // 启动Socket Server,端口是2233
                Link.Res.TSC = new Link.Sockets.TcpSocketServer(ComboBoxList.Text, int.Parse(MyPort.Text));
                Link.Res.TSC.HandleRecMsg = new Link.Equipment.Server().Calculation;
                Link.Res.TSC.HandleClientClose = new Link.Equipment.Server().ClientDisconnect;
                Link.Res.TSC.StartServer();

                // 初始化接收事件
                EventInit();
                OpenTcp.Content = "关闭网络";
                OpenTcp.Background = Brushes.Red;
            }
            else
            {
                Link.Res.TSC.StopServer();
                OpenTcp.Content = "打开网络";
                OpenTcp.Background = Brushes.White;
            }
        }

        /// <summary>
        /// 事件初始化
        /// </summary>
        private void EventInit()
        {
            int color = 0;
            // 保存GPS数据
            Link.Res.SenseDataMsg = (dev, a, b, c) =>
            {
                System.Diagnostics.Debug.WriteLine($"SenseDataMsg 接收到数据:{a},{b},{c}");
                // 如果用户不存在,属于非法数据,可以忽略
                User user = wRes.MyDb.Tb_User.FirstOrDefault(x => x.ID == a);
                if (user == null) return;

                // 显示地图
                this.Dispatcher.BeginInvoke((Action)delegate ()
                {
                    List<Brush> colors = new List<Brush>() { Brushes.Yellow, Brushes.SpringGreen };

                    MyBrowser.InvokeScript("WriteLine", new Object[] { b, c });
                    TextBlock item = new TextBlock();
                    item.Text = $"{ListBoxLogs.Count}:{b},{c}";
                    item.FontSize = 14;
                    item.TextWrapping = TextWrapping.Wrap;
                    item.Foreground = colors[color];
                    ListBoxLogs.Insert(0, item);
                    color = color == 0 ? 1 : 0;

                    //控制只显示20条
                    if (ListBoxLogs.Count > 20)
                    {
                        for (int i = 10; i < ListBoxLogs.Count; i++)
                        {
                            ListBoxLogs.RemoveAt(i);
                        }
                    }
                });

                // 将接收到的数据保存到数据库
                using (MyDbContext db = new MyDbContext() { config = wRes.SqlStr })
                {
                    GpsData data = new GpsData()
                    {
                        CreateTime = DateTime.Now,
                        UpdateTime = DateTime.Now,
                        UserId = user.UserId,
                        gps = $"{b},{c}"
                    };
                    db.Tb_GpsData.Add(data);
                    db.SaveChanges();
                }
            };

            // 后面可以扩展
            /* ... */
        }
        #endregion

        /// <summary>
        /// 清除地图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClearMap_Click(object sender, RoutedEventArgs e)
        {
            MyBrowser.InvokeScript("clearAll");
        }

        /// <summary>
        /// 清空日志
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClearLog_Click(object sender, RoutedEventArgs e)
        {
            ListBoxLogs.Clear();
        }

        /// <summary>
        /// 主动请求数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReadData_Click(object sender, RoutedEventArgs e)
        {
            // 由于初始化的时候,设置的设备类型是1,设备号也是1,所以这边查询的时候也要按这个参数查询
            Link.Equipment.GpsModel sense = Link.Res.Socs.FirstOrDefault(v => v.equi == 1 && v.equino.ByteToInt() == 1) as Link.Equipment.GpsModel;
            if (sense != null)
            {
                if (sense.socketstate == 1)
                {
                    // 下发命令,可以带参数,这里用0
                    int ret = sense.SetState(0);
                    if (ret == 0) MessageBox.Show("命令发送成功");
                    if (ret == 1) MessageBox.Show("命令发送失败");
                    if (ret == -1) MessageBox.Show("网络错误");
                }
                else MessageBox.Show("设备链接已断开");
            }
            else MessageBox.Show("未查询到设备");
        }

        public string GetLocalIP()
        {
            try
            {
                string HostName = Dns.GetHostName(); //得到主机名
                IPHostEntry IpEntry = Dns.GetHostEntry(HostName);
                for (int i = 0; i < IpEntry.AddressList.Length; i++)
                {
                    //从IP地址列表中筛选出IPv4类型的IP地址
                    //AddressFamily.InterNetwork表示此IP为IPv4,
                    //AddressFamily.InterNetworkV6表示此地址为IPv6类型
                    if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
                    {
                        string strip = IpEntry.AddressList[i].ToString();

                        string[] sArray = strip.Split('.');
                        if (sArray[3] != "1")
                            return IpEntry.AddressList[i].ToString();
                    }
                }
                return "";
            }
            catch (Exception ex)
            {
                return "";
            }
        }
    }
}


2、最终演示效果

wpfmap.gif


注意:如果是直接运行生成的exe文件,会提示下面这个信息,直接允许运行即可!

QQ截图20220927100307.jpg

22、WinForm版地图上位机(带数据库和TCP功能)
24、公网映射与外网通讯

友情链接: CSDN激萌の小宅 95知识库 自考题库 罗分明个人网络博客 精益编程leanboot

小宅博客  www.bilibili996.com All Rights Reserved. 备案号: 闽ICP备2024034575号

网站经营许可证  福建省福州市 Copyright©2021-2025 版权所有

小宅博客
首页 智能家居 地图定位
公告:小宅博客网可以开发票了,需要发票的,去群里找群主哈!!

文章作者:激萌の小宅

促销:¥0

价格:¥0

配送方式: 购买后立即生效(如购买异常,请联系站长)
付款之后一定要等待自动跳转结束,否则购买可能会失败
  • 0 天

    有效期

  • 0

    总销量

  • 2

    累计评价

WPF版地图上位机 - (第二十三讲)

视频讲解如下:


工程源码下载:GPS定位系统系列教程源码下载


        WPF版本的上位机使用了.net4.8的框架,同时也集成前面的DbEntity和Link两个类库,开发环境还是VS2022。参考文章:

        《百度地图上显示GPS坐标》

        《C#环境下使用EF操作MySql》

        《如何实现TCP通信》


1、集成事项

集成后的代码如下:

QQ截图20220927090359.jpg


界面布局图如下:

QQ截图20220927093609.jpg

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="480" Width="950">
    <Grid ShowGridLines="False" Background="#FF0BC3F3">
        <Grid.RowDefinitions>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"></ColumnDefinition>
            <ColumnDefinition Width="250"></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Label Grid.Row="0" Grid.Column="0" Margin="5,5,5,0" Content="本机主机地址:" FontSize="18" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <ComboBox Grid.Row="1" Grid.Column="0" Margin="5,0,5,5" x:Name="ComboBoxList" FontSize="18"/>
        <Label Grid.Row="2" Grid.Column="0" Margin="5,5,5,0" Content="本机主机端口:" FontSize="18" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <TextBox Grid.Row="3" Grid.Column="0" Margin="5,5,5,5" TextWrapping="Wrap" FontSize="18" x:Name="MyPort" Text="2233" />

        <Button Grid.Row="4" Grid.Column="0" Margin="5,5,5,5" FontSize="18" Content="打开网络" x:Name="OpenTcp" Click="OpenTcp_Click"/>
        <Button Grid.Row="5" Grid.Column="0" Margin="5,5,5,5" FontSize="18" Content="清空日志" x:Name="ClearLog" Click="ClearLog_Click"/>
        <Button Grid.Row="6" Grid.Column="0" Margin="5,5,5,5" FontSize="18" Content="主动请求" x:Name="ReadData" Click="ReadData_Click" />
        <Button Grid.Row="7" Grid.Column="0" Margin="5,5,5,5" FontSize="18" Content="清除地图" x:Name="ClearMap" Click="ClearMap_Click"/>

        <ListBox Grid.Row="0" Grid.Column="1" Grid.RowSpan="9" Margin="5,5,5,5" x:Name="ListBoxLog" FontSize="8" BorderThickness="10"
                 ScrollViewer.HorizontalScrollBarVisibility="Disabled" Background="Black"></ListBox>

        <WebBrowser Grid.Row="0" Grid.Column="2" Grid.RowSpan="9" Margin="5,5,5,5" x:Name="MyBrowser" VerticalAlignment="Stretch"
                  HorizontalAlignment="Center" />
    </Grid>
</Window>


MainWindow.xaml.cs文件代码如下:

using DbEntity;
using DbEntity.Tables;
using Link;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfApp
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        // <summary>
        /// 窗体日志信息
        /// </summary>
        public static ObservableCollection<TextBlock> ListBoxLogs { get; set; } = new ObservableCollection<TextBlock>();

        public MainWindow()
        {
            InitializeComponent();

            // 关联到数据
            ListBoxLog.ItemsSource = ListBoxLogs;

            // 绑定IP数据
            ComboBoxList.Items.Add("127.0.0.1");
            ComboBoxList.Items.Add(GetLocalIP());
            ComboBoxList.SelectedIndex = 0;

            // 启动数据库
            DbInit();
            MyBrowser.Navigate(new Uri(Directory.GetCurrentDirectory() + @"\html\index.html"));
        }

        #region 数据库
        /// <summary>
        /// 启动数据库
        /// </summary>
        private void DbInit()
        {
            try
            {
                wRes.MyDb = new MyDbContext() { config = wRes.SqlStr };
                wRes.MyDb.Database.EnsureCreated();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
        #endregion

        #region TCP网络
        /// <summary>
        /// 启动TCP Server
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenTcp_Click(object sender, RoutedEventArgs e)
        {
            if (Link.Res.TSC == null || !Link.Res.TSC.IsOpen())
            {
                // 定义一个设备
                Link.Res.Socs.Add(new Link.Equipment.GpsModel() { equi = 0x01, equino = 1.IntToByte() });

                // 启动Socket Server,端口是2233
                Link.Res.TSC = new Link.Sockets.TcpSocketServer(ComboBoxList.Text, int.Parse(MyPort.Text));
                Link.Res.TSC.HandleRecMsg = new Link.Equipment.Server().Calculation;
                Link.Res.TSC.HandleClientClose = new Link.Equipment.Server().ClientDisconnect;
                Link.Res.TSC.StartServer();

                // 初始化接收事件
                EventInit();
                OpenTcp.Content = "关闭网络";
                OpenTcp.Background = Brushes.Red;
            }
            else
            {
                Link.Res.TSC.StopServer();
                OpenTcp.Content = "打开网络";
                OpenTcp.Background = Brushes.White;
            }
        }

        /// <summary>
        /// 事件初始化
        /// </summary>
        private void EventInit()
        {
            int color = 0;
            // 保存GPS数据
            Link.Res.SenseDataMsg = (dev, a, b, c) =>
            {
                System.Diagnostics.Debug.WriteLine($"SenseDataMsg 接收到数据:{a},{b},{c}");
                // 如果用户不存在,属于非法数据,可以忽略
                User user = wRes.MyDb.Tb_User.FirstOrDefault(x => x.ID == a);
                if (user == null) return;

                // 显示地图
                this.Dispatcher.BeginInvoke((Action)delegate ()
                {
                    List<Brush> colors = new List<Brush>() { Brushes.Yellow, Brushes.SpringGreen };

                    MyBrowser.InvokeScript("WriteLine", new Object[] { b, c });
                    TextBlock item = new TextBlock();
                    item.Text = $"{ListBoxLogs.Count}:{b},{c}";
                    item.FontSize = 14;
                    item.TextWrapping = TextWrapping.Wrap;
                    item.Foreground = colors[color];
                    ListBoxLogs.Insert(0, item);
                    color = color == 0 ? 1 : 0;

                    //控制只显示20条
                    if (ListBoxLogs.Count > 20)
                    {
                        for (int i = 10; i < ListBoxLogs.Count; i++)
                        {
                            ListBoxLogs.RemoveAt(i);
                        }
                    }
                });

                // 将接收到的数据保存到数据库
                using (MyDbContext db = new MyDbContext() { config = wRes.SqlStr })
                {
                    GpsData data = new GpsData()
                    {
                        CreateTime = DateTime.Now,
                        UpdateTime = DateTime.Now,
                        UserId = user.UserId,
                        gps = $"{b},{c}"
                    };
                    db.Tb_GpsData.Add(data);
                    db.SaveChanges();
                }
            };

            // 后面可以扩展
            /* ... */
        }
        #endregion

        /// <summary>
        /// 清除地图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClearMap_Click(object sender, RoutedEventArgs e)
        {
            MyBrowser.InvokeScript("clearAll");
        }

        /// <summary>
        /// 清空日志
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClearLog_Click(object sender, RoutedEventArgs e)
        {
            ListBoxLogs.Clear();
        }

        /// <summary>
        /// 主动请求数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReadData_Click(object sender, RoutedEventArgs e)
        {
            // 由于初始化的时候,设置的设备类型是1,设备号也是1,所以这边查询的时候也要按这个参数查询
            Link.Equipment.GpsModel sense = Link.Res.Socs.FirstOrDefault(v => v.equi == 1 && v.equino.ByteToInt() == 1) as Link.Equipment.GpsModel;
            if (sense != null)
            {
                if (sense.socketstate == 1)
                {
                    // 下发命令,可以带参数,这里用0
                    int ret = sense.SetState(0);
                    if (ret == 0) MessageBox.Show("命令发送成功");
                    if (ret == 1) MessageBox.Show("命令发送失败");
                    if (ret == -1) MessageBox.Show("网络错误");
                }
                else MessageBox.Show("设备链接已断开");
            }
            else MessageBox.Show("未查询到设备");
        }

        public string GetLocalIP()
        {
            try
            {
                string HostName = Dns.GetHostName(); //得到主机名
                IPHostEntry IpEntry = Dns.GetHostEntry(HostName);
                for (int i = 0; i < IpEntry.AddressList.Length; i++)
                {
                    //从IP地址列表中筛选出IPv4类型的IP地址
                    //AddressFamily.InterNetwork表示此IP为IPv4,
                    //AddressFamily.InterNetworkV6表示此地址为IPv6类型
                    if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
                    {
                        string strip = IpEntry.AddressList[i].ToString();

                        string[] sArray = strip.Split('.');
                        if (sArray[3] != "1")
                            return IpEntry.AddressList[i].ToString();
                    }
                }
                return "";
            }
            catch (Exception ex)
            {
                return "";
            }
        }
    }
}


2、最终演示效果

wpfmap.gif


注意:如果是直接运行生成的exe文件,会提示下面这个信息,直接允许运行即可!

QQ截图20220927100307.jpg