简体   繁体   English

在WPF MainWindow.xaml中显示IP地址

[英]Display IP address in WPF MainWindow.xaml

How can I display my Ip address on MainWindow.xaml?( I have a console application that can display IP address. How can I combine the console application with the XAML file? Need some guidance here and advice. Thanks! 我如何在MainWindow.xaml上显示我的IP地址?(我有一个可以显示IP地址的控制台应用程序。如何将控制台应用程序与XAML文件结合在一起?在这里需要一些指导和建议。谢谢!

XAML file: XAML文件:

<Window x:Class="DisplayIP.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:DisplayIP"
mc:Ignorable="d"
Title="Display IP Address" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
    <RowDefinition/>
    <RowDefinition/>
    <RowDefinition/>
</Grid.RowDefinitions>
</Grid>
</Window>

C# Code for the console app: 控制台应用程序的C#代码:

class Program
{
    static void Main(string[] args)
    {
        NetworkInterface[] IF = NetworkInterface.GetAllNetworkInterfaces();

        foreach (NetworkInterface Interface in IF)
        {
            if (Interface.NetworkInterfaceType == NetworkInterfaceType.Loopback) continue;

            Console.WriteLine(" ");
            Console.WriteLine(Interface.Description);


            UnicastIPAddressInformationCollection UnicastIPInfoCol = Interface.GetIPProperties().UnicastAddresses;
            foreach (UnicastIPAddressInformation UnicatIPInfo in UnicastIPInfoCol)
            {
                Console.WriteLine("The IP address is:{0}", UnicatIPInfo.Address);
                Console.WriteLine("The Sub net is:{0}", UnicatIPInfo.IPv4Mask);
            }

        }
        Console.ReadKey();
        }


    }

Generally you would like to have all "business logic" in your case the piece of code which gets the IP address in a separate method (or even class). 通常情况下,您希望所有“业务逻辑”都包含在单独的方法(甚至类)中获取IP地址的代码段。 Your console application should only be communicating with the user, and so is your WPF application. 您的控制台应用程序应仅与用户通信,WPF应用程序也应与用户通信。

So, you will have 2 "front-end" applications (WPF and Console) which a user can use to get the business logic from a back-end piece of code (method, class, namespace or in bigger applications - library). 因此,您将拥有2个“前端”应用程序(WPF和控制台),用户可以使用这些应用程序从后端代码段(方法,类,名称空间或更大的应用程序-库)中获取业务逻辑。

So if I were you, I would have created a class (eg NetworkInfo) where a method(s) would return the information about the address and the mask (focus on the word return here - the method will return data to the caller, and not print it on the console). 因此,如果我是您,我将创建一个类(例如NetworkInfo),其中一个(或多个)方法将返回有关地址和掩码的信息(将重点放在return这个词上-该方法会将数据返回给调用方,并且不能在控制台上打印它)。 Then both applications - the console one and the WPF one will call this method from this class, to get the needed data and present it accordingly - one on the console with Console.WriteLine, and the other one in a Form or a Control with the XAML file. 然后,两个应用程序-控制台一个和WPF一个都将从此类中调用此方法,以获取所需的数据并相应地呈现-一个在控制台上使用Console.WriteLine,另一个在Form或Control中使用。 XAML文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM