Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从Api的列表中填充服务器列表并在WPF.NETCore3.1的ListView中显示_C#_Wpf_Api_Web Services_.net Core 3.1 - Fatal编程技术网

C# 如何从Api的列表中填充服务器列表并在WPF.NETCore3.1的ListView中显示

C# 如何从Api的列表中填充服务器列表并在WPF.NETCore3.1的ListView中显示,c#,wpf,api,web-services,.net-core-3.1,C#,Wpf,Api,Web Services,.net Core 3.1,我正在创建VPN WPF软件,我拥有所有API,比如说serverlist它的链接是 现在我有了服务器列表的模型类 using System; using System.Collections.Generic; using System.Text; namespace VirtualPN.Model { class ServerModel { public string error { get; set; } public string message { get; set;

我正在创建VPN WPF软件,我拥有所有API,比如说serverlist它的链接是

现在我有了服务器列表的模型类

using System;
using System.Collections.Generic;
using System.Text;

namespace VirtualPN.Model
{
class ServerModel
{
    public string error { get; set; }
    public string message { get; set; }
    public Server[] servers { get; set; }
}

public class Server
{
    public string SID{ get; set; }
    public string Scountry { get; set; }
    public string Sdns { get; set; }
    public string Sport { get; set; }
    public string Spsk { get; set; }
    public string pptp { get; set; }
    public string l2tp { get; set; }
    public string tcp { get; set; }
    public string udp { get; set; }
    public string openconnect { get; set; }
    public string ikev2 { get; set; }
    public string sstp { get; set; }
    public string p2p { get; set; }
    public string videostreaming { get; set; }
    public string security { get; set; }
    public string voip { get; set; }
    public string enable { get; set; }
    public string maintmode { get; set; }
    public string iso { get; set; }
    public string free { get; set; }
    public string recent { get; set; }
    public string time { get; set; }
    public string fav { get; set; }
    public int Pingrate { get; set; }
    public string IsFavorite { get; set; }

    public string FavProtocol { get; set; }
}
}
现在我有多个国家/地区的服务器,并希望在窗口中显示为ListView,其中每个服务器都可以选择连接VPN服务。我怎样才能做到这一点。 国家名称、城市DNS很重要

using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
using Microsoft.VisualBasic;
using System.Diagnostics;
using System.Collections;
using VirtualPN.Model;
namespace VirtualPN.Classes
{
class Global
{
    public static ServerModel sm = new ServerModel();
    public static Web_Service service = new Web_Service();
    static ArrayList nic_arr = new ArrayList();
    static bool IsdnsStatic = false;




    public Global()
    {


    }


    public class ServerType
    {
        public static string type
        {
            get; set;
        }
    }

    public static ServerModel getModel()
    {
        return sm;
    }

    public static Web_Service GetService()
    {
        return service;
    }

    public class _Server : Server
    {

        public string SId { get; set; }
        public string ServerCountry { get; set; }
        public string SDNS { get; set; }

        public string Siso { get; set; }
        public string SKey { get; set; }
        public string Sport { get; set; }
        public string Spptp { get; set; }
        public string Sl2tp { get; set; }
        public string Stcp { get; set; }
        public string Sudp { get; set; }
        public string SopenConnect { get; set; }
        public string Sikev2 { get; set; }
        public string Sstp { get; set; }
        public string Sp2p { get; set; }
        public string Svideostreaming { get; set; }
        public string Svoip { get; set; }
        public string Ssecurity { get; set; }
        public int Spingrate { get; set; }
        public string GroupName { get; set; }
        public string SIsFavorite { get; set; }
        public string IsFree { get; set; }
        public string IsRecent { get; set; }
        public string SIsTime { get; set; }
        public string Smaintmode { get; set; }


    }


    public ServerModel getServerModel
    {
        get; set;

    }


    public static string Account_Person_Name
    {
        get; set;

    }

    public static double RemainingDays
    {
        get; set;

    }

    public static DateTime DueDate
    {
        get; set;
    }

    public static bool IsfreeAccount
    {
        get; set;
    }

    public static string AccountType
    {
        get; set;
    }

    public static double TotalSubscriptionDays
    {
        get; set;
    }


    public class UrlItems
    {
        public int id { get; set; }
        public string url { get; set; }
    }



    public static void Window_Closing()
    {
        Process.GetCurrentProcess().Kill();
    }


    public static string UserName { get; set; }
    public static string password { get; set; }

    public static string GetActiveIp()
    {
        var data = Task.Run(() => Web_Service.GetIPData()).Result;
        var model = Web_Service.Map_IPModel(data.ToString());

        var ip = model.ip;

        return ip;
    }

    public static string GetActiveLocation()
    {
        var data = Task.Run(() => Web_Service.GetIPData()).Result;
        var model = Web_Service.Map_IPModel(data.ToString());

        var ip = model.country;

        return ip;
    }

    public static DateTime GetLiveDate()
    {

        var data = Task.Run(() => Web_Service.GetLiveDate()).Result;

        DateTime reg = DateTime.Parse(data);

        return reg.Date;
    }

    public static bool IsValidIpAddress(string paddress)
    {
        if (string.IsNullOrWhiteSpace(paddress))
            return false;

        var splitValues = paddress.Split('.');
        if (splitValues.Length != 4)
            return false;

        byte tempForParsing;

        return splitValues.All(r => byte.TryParse(r, out tempForParsing));
    }
    public static void Disable_Network()
    {
        // Interaction.Shell("cmd.exe /c" + value);
        string targetURL = "netsh advfirewall firewall add rule name = Block All Traffic dir =in action = allow port enable = yes remoteip = 5, 46, 48 - 1722, 1724 - 65535,LocalSubnet profile = public";

        var psi = new ProcessStartInfo
        {
            FileName = targetURL,
            UseShellExecute = true
        };
        Process.Start(psi);




    }


    public static void Activate_KillSwitch()
    {
        foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
        {


            if (ni.OperationalStatus == OperationalStatus.Up && (ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet || ni.NetworkInterfaceType == NetworkInterfaceType.FastEthernetFx || ni.NetworkInterfaceType == NetworkInterfaceType.FastEthernetT || ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) && ni.NetworkInterfaceType != NetworkInterfaceType.Loopback && ni.NetworkInterfaceType != NetworkInterfaceType.Tunnel)
            {
                if (ni.GetIPProperties().GatewayAddresses.FirstOrDefault() != null)
                {
                    string targetURL = "netsh interface set interface " + ni.Name + " DISABLED";

                    var psi = new ProcessStartInfo
                    {
                        FileName = targetURL,
                        UseShellExecute = true
                    };
                    Process.Start(psi);
                    nic_arr.Add(ni.Name);
                    // Interaction.Shell("cmd.exe /c" + vpnKillString, Constants.vbHide;

                }


            }
        }
    }

    public static void EnableKillSwitch()
    {
        string targetURL = "netsh advfirewall firewall del rule name = 'BlockOutbound'";

        var psi = new ProcessStartInfo
        {
            FileName = targetURL,
            UseShellExecute = true
        };
        Process.Start(psi);
    }

    public static List<_Server> ServerListGlobal { get; set; }
    public static List<_Server> FavoriteServerListGlobal { get; set; }
    public static List<_Server> RecentServerListGlobal { get; set; }

    public static string ethernetIp
    {
        get; set;
    }

    public static DateTime CurrentDate
    {
        get; set;
    }

    public static string ethernetFXIp
    {
        get; set;
    }

    public static string ethernetTIp
    {
        get; set;
    }


    public static string wirelessIP
    {
        get; set;
    }

    public static void getAdaptersIP()
    {
        foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
        {


            if (ni.OperationalStatus == OperationalStatus.Up && (ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet || ni.NetworkInterfaceType == NetworkInterfaceType.FastEthernetFx || ni.NetworkInterfaceType == NetworkInterfaceType.FastEthernetT || ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) && ni.NetworkInterfaceType != NetworkInterfaceType.Loopback && ni.NetworkInterfaceType != NetworkInterfaceType.Tunnel)
            {
                if (ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                {
                    if (ni.GetIPProperties().GatewayAddresses.FirstOrDefault() != null)

                    {
                        ethernetIp = ni.GetIPProperties().UnicastAddresses[1].Address.ToString();

                    }


                }
                else if (ni.NetworkInterfaceType == NetworkInterfaceType.FastEthernetFx)
                {
                    if (ni.GetIPProperties().GatewayAddresses.FirstOrDefault() != null)

                    {
                        ethernetFXIp = ni.GetIPProperties().UnicastAddresses[1].Address.ToString();
                    }
                }

                else if (ni.NetworkInterfaceType == NetworkInterfaceType.FastEthernetT)
                {
                    if (ni.GetIPProperties().GatewayAddresses.FirstOrDefault() != null)

                    {
                        {
                            ethernetTIp = ni.GetIPProperties().UnicastAddresses[1].Address.ToString();
                        }
                    }
                }

                else if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
                {
                    if (ni.GetIPProperties().GatewayAddresses.FirstOrDefault() != null)

                    {
                        wirelessIP = ni.GetIPProperties().UnicastAddresses[1].Address.ToString();
                        if (ni.GetIPProperties().GetIPv4Properties().IsDhcpEnabled)
                        {
                            if (ni.GetIPProperties().DhcpServerAddresses.FirstOrDefault().ToString() != ni.GetIPProperties().DnsAddresses.FirstOrDefault().ToString())
                            {

                                dnsStatic = ni.GetIPProperties().DnsAddresses.FirstOrDefault().ToString();
                                IsdnsStatic = true;
                            }
                        }
                        else
                        {
                            dnsStatic = ni.GetIPProperties().DnsAddresses.FirstOrDefault().ToString();
                            IsdnsStatic = true;
                        }



                    }


                }

            }


        }
    }

    public static string dnsStatic
    { get; set; }

    public static string vpndnsServer
    { get; set; }

    public static void dnsLeakFix()
    {
        IsdnsStatic = false;

        foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
        {

            if (ni.OperationalStatus == OperationalStatus.Up && (ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet || ni.NetworkInterfaceType == NetworkInterfaceType.FastEthernetFx || ni.NetworkInterfaceType == NetworkInterfaceType.FastEthernetT || ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) && ni.NetworkInterfaceType != NetworkInterfaceType.Loopback && ni.NetworkInterfaceType != NetworkInterfaceType.Tunnel)
            {
                string targetURL = "ipconfig /flushdns";

                var psi = new ProcessStartInfo
                {
                    FileName = targetURL,
                    UseShellExecute = true
                };
                Process.Start(psi);


                if (ni.GetIPProperties().GatewayAddresses.FirstOrDefault() != null)
                {
                    if (ni.GetIPProperties().GetIPv4Properties().IsDhcpEnabled)
                    {
                        if (ni.GetIPProperties().DhcpServerAddresses.FirstOrDefault().ToString() != ni.GetIPProperties().DnsAddresses.FirstOrDefault().ToString())
                        {

                            dnsStatic = ni.GetIPProperties().DnsAddresses.FirstOrDefault().ToString();
                            IsdnsStatic = true;
                        }
                    }
                    else
                    {
                        dnsStatic = ni.GetIPProperties().DnsAddresses.FirstOrDefault().ToString();
                        IsdnsStatic = true;
                    }



                    string dnsFix = "netsh interface IPv4 set dnsserver \"" + ni.Name + "\" static 8.8.8.8 both";
                    Process.Start(dnsFix);
                    //Interaction.Shell("cmd.exe /c" + dnsFix, AppWinStyle.Hide);
                }


            }


        }


        Messagebox box = new Messagebox("DNS Leak has been fixed!. Enjoy your browsing...");
        box.ShowInTaskbar = false;
        box.ShowDialog();
    }

    public static void dnsLeak_Release()
    {
        foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
        {

            if (ni.OperationalStatus == OperationalStatus.Up && (ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet || ni.NetworkInterfaceType == NetworkInterfaceType.FastEthernetFx || ni.NetworkInterfaceType == NetworkInterfaceType.FastEthernetT || ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) && ni.NetworkInterfaceType != NetworkInterfaceType.Loopback && ni.NetworkInterfaceType != NetworkInterfaceType.Tunnel)
            {
                string targetURL = "ipconfig /flushdns";

                var psi = new ProcessStartInfo
                {
                    FileName = targetURL,
                    UseShellExecute = true
                };
                Process.Start(psi);

                if (ni.GetIPProperties().GatewayAddresses.FirstOrDefault() != null)
                {

                    if (IsdnsStatic == false)
                    {
                        string dnsFix = "netsh interface IPv4 set dnsserver \"" + ni.Name + "\" dhcp";
                        Process.Start(dnsFix);


                   ////     Interaction.Shell("cmd.exe /c" + dnsFix, AppWinStyle.Hide);
                    }
                    else if (IsdnsStatic == true)
                    {
                        string dnsFix = "netsh interface IPv4 set dnsserver \"" + ni.Name + "\" dhcp";
                        Process.Start(dnsFix);

                       //Interaction.Shell("cmd.exe /c" + dnsFix, AppWinStyle.Hide);


                    }


                   Usercredentials.Default.IsDNSLeakEnabled = false;
                   Usercredentials.Default.Save();

                }

            }
        }
        Messagebox box = new Messagebox("DNS settings has been set to default.");
        box.ShowInTaskbar = false;
        box.ShowDialog();
    }



}
使用系统;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Net.NetworkInformation;
使用System.Threading.Tasks;
使用Microsoft.VisualBasic;
使用系统诊断;
使用系统集合;
使用虚拟模型;
命名空间VirtualPN.Classes
{
类全局
{
public static ServerModel sm=new ServerModel();
公共静态Web_服务=新Web_服务();
静态ArrayList nic_arr=新ArrayList();
静态bool isdnstatic=false;
公共全球()
{
}
公共类服务器类型
{
公共静态字符串类型
{
获得;设置;
}
}
公共静态服务器模型getModel()
{
返回sm;
}
公共静态Web_服务GetService()
{
回程服务;
}
公共类\u服务器:服务器
{
公共字符串SId{get;set;}
公共字符串ServerCountry{get;set;}
公共字符串SDN{get;set;}
公共字符串Siso{get;set;}
公共字符串SKey{get;set;}
公共字符串Sport{get;set;}
公共字符串Spptp{get;set;}
公共字符串Sl2tp{get;set;}
公共字符串Stcp{get;set;}
公共字符串Sudp{get;set;}
公共字符串SopenConnect{get;set;}
公共字符串Sikev2{get;set;}
公共字符串Sstp{get;set;}
公共字符串Sp2p{get;set;}
公共字符串Svideostreaming{get;set;}
公共字符串Svoip{get;set;}
公共字符串安全性{get;set;}
公共整数Spingrate{get;set;}
公共字符串组名{get;set;}
公共字符串{get;set;}
公共字符串IsFree{get;set;}
公共字符串是最近的{get;set;}
公共字符串SIsTime{get;set;}
公共字符串smainmode{get;set;}
}
公共服务器模型getServerModel
{
获得;设置;
}
公共静态字符串帐户\人员\姓名
{
获得;设置;
}
公共静态双剩余天数
{
获得;设置;
}
公共静态日期时间DueDate
{
获得;设置;
}
公共静态bool IsfreeAccount
{
获得;设置;
}
公共静态字符串AccountType
{
获得;设置;
}
公共静态双总订阅日
{
获得;设置;
}
公共类项目
{
公共int id{get;set;}
公共字符串url{get;set;}
}
公共静态无效窗口_关闭()
{
Process.GetCurrentProcess().Kill();
}
公共静态字符串用户名{get;set;}
公共静态字符串密码{get;set;}
公共静态字符串GetActiveIp()
{
var data=Task.Run(()=>Web_服务.GetIPData()).Result;
var model=Web_Service.Map_IPModel(data.ToString());
var ip=model.ip;
返回ip;
}
公共静态字符串GetActiveLocation()
{
var data=Task.Run(()=>Web_服务.GetIPData()).Result;
var model=Web_Service.Map_IPModel(data.ToString());
var ip=模型国;
返回ip;
}
公共静态日期时间GetLiveDate()
{
var data=Task.Run(()=>webservice.GetLiveDate()).Result;
DateTime reg=DateTime.Parse(数据);
返回注册日期;
}
公共静态bool IsValidIpAddress(字符串填充符)
{
if(string.IsNullOrWhiteSpace(paddress))
返回false;
var splitValues=paddress.Split('.');
if(splitValues.Length!=4)
返回false;
字节分析;
返回splitValues.All(r=>byte.TryParse(r,out tempForParsing));
}
公共静态无效禁用_网络()
{
//Interaction.Shell(“cmd.exe/c”+值);
string targetURL=“netsh advfirewall firewall add rule name=Block All Traffic dir=in action=allow port enable=yes remoteip=5,46,48-1722,1724-65535,LocalSubnet profile=public”;
var psi=新流程开始信息
{
FileName=targetURL,
UseShellExecute=true
};
过程启动(psi);
}
公共静态无效激活\u KillSwitch()
{
foreach(NetworkInterface.GetAllNetworkInterfaces()中的NetworkInterface)
{
如果(ni.OperationalStatus==OperationalStatus.Up&(ni.NetworkInterfaceType==NetworkInterfaceType.Ethernet | | | ni.NetworkInterfaceType==NetworkInterfaceType.fastehernetfx | | ni.NetworkInterfaceType==NetworkInterfaceType.Wireless80211)&&ni.NetworkInterfaceType!=NetworkInterfaceType.Loopback&&ni.NetworkInterfaceType!=NetworkInterfaceType.Tunnel)
{
if(ni.GetIPProperties().GatewayAddresses.FirstOrDefault()!=null)
{
字符串targetURL=“netsh接口集接口”+ni.Name+“已禁用”;
var psi=新流程开始信息
{
FileName=targetURL,
UseShellExecute=true
};
过程启动(psi);
nic_arr.Add(ni.Name);
//Interaction.Shell(“cmd.exe/c”+vpnKillString,Constants.vbHide;
}
}
}
}
公共静态void EnableKillSwitch()
{
string targetURL=“netsh advfirewall del rule name=”BlockOutbo
<Window x:Class="ExampleApp.ServerView"
        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"
        mc:Ignorable="d"
        Title="ServerView" Height="450" Width="800">
    <Grid>
        <ListView ItemsSource="{Binding Servers}" SelectedItem="{Binding SelectedServer}" SelectionMode="Single">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Border Padding="5">
                        <StackPanel>
                            <TextBlock Text="{Binding CountryName, StringFormat='Country: {0}'}"  />
                            <TextBlock Text="{Binding CityDns, StringFormat='DNS: {0}'}" />
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <Button Content="Refresh" Click="BtnRefresh_Click" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="10" Padding="10"/>
    </Grid>
</Window>
using System.Windows;

namespace ExampleApp
{
    public partial class ServerView : Window
    {
        private readonly ServerViewModel _model;
        public ServerView()
        {
            InitializeComponent();
            _model = new ServerViewModel();
            DataContext = _model;
        }

        private void BtnRefresh_Click(object sender, RoutedEventArgs e)
        {
            _model.ReloadServers();
        }
    }
}
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace ExampleApp
{
    public class ServerViewModel : INotifyPropertyChanged
    {
        public ObservableCollection<Server> Servers { get; set; } = new ObservableCollection<Server>();

        private Server _selectedServer;
        public Server SelectedServer
        {
            get => _selectedServer;
            set
            {
                if(_selectedServer != value)
                {
                    _selectedServer = value;
                    OnPropertyChanged();
                    SelectedServerChanged();
                }
            }
        }

        public void SelectedServerChanged()
        {
            //do whatever you need to do when the selected server changes here...
            System.Console.WriteLine($"The selected server has changed: {SelectedServer.CountryName}");
        }

        public void ReloadServers()
        {
            var servers = DownloadServers();

            Servers.Clear();
            foreach (var server in servers)
            {
                Servers.Add(server);
            }
        }

        private List<Server> DownloadServers()
        {
            //pretend we're downloading the list of servers here...
            return new List<Server>
            {
                new Server { CityDns = "Some DNS", CountryName = "England" },
                new Server { CityDns = "Some other DNS", CountryName = "China" },
                new Server { CityDns = "Another DNS", CountryName = "United States" }
            };
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class Server
    {
        public string CountryName { get; set; }
        public string CityDns { get; set; }
    }
}