Hallo zusammen,
ich versuche die SSID aller gefundenen WIFI's als Text in der comboBox comboSSID anzuzeigen, ohne Erfolg.
Ich sehe, dass in der Combobox Elemente enthalten sind aber mit leerem Text?
Ich komme einfach nicht dahinter, hat evtl. jemand eine Idee?
Danke schonmal
using System;
using System.Net.NetworkInformation;
using System.Windows;
using System.Windows.Controls;
using System.Linq;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Text;
using Microsoft.WindowsAPICodePack.Net;
using NativeWifi;
namespace WLAN2
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MyWifi wifi1 = new MyWifi();
wifi1.ScanSSID();
List<string> ssidList = new List<string>();
foreach (WIFISSID ssid in wifi1.ssids)
{
ssidList.Add(ssid.SSID);
}
comboSSID.ItemsSource = wifi1.ssids;
comboSSID.DisplayMemberPath = "SSID"; // Das sollte eigentlich dazu führen, dass der Text angezeigt wird???
//comboSSID.SelectedValuePath = "SSID";
//comboSSID.SelectedValue = ssid;
}
class MyWifi
{
public List<WIFISSID> ssids = new List<WIFISSID>();
public MyWifi()
{
ssids.Clear();
}
static string GetStringForSSID(Wlan.Dot11Ssid ssid)
{
return Encoding.UTF8.GetString(ssid.SSID, 0, (int)ssid.SSIDLength);
}
/// Enumerate the SSIDs received by all wireless devices
public void ScanSSID()
{
WlanClient client = new WlanClient();
foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
{
// Lists all networks with WEP security
Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
foreach (Wlan.WlanAvailableNetwork network in networks)
{
WIFISSID targetSSID = new WIFISSID();
targetSSID.wlanInterface = wlanIface;
targetSSID.wlanSignalQuality = (int)network.wlanSignalQuality;
targetSSID.SSID = GetStringForSSID(network.dot11Ssid);
targetSSID.dot11DefaultAuthAlgorithm = network.dot11DefaultAuthAlgorithm.ToString();
targetSSID.dot11DefaultCipherAlgorithm = network.dot11DefaultCipherAlgorithm.ToString();
ssids.Add(targetSSID);
}
}
} // EnumSSID
/// Connect to an unencrypted SSID
public void ConnectToSSID(WIFISSID ssid)
{
// Connects to a known network with WEP security
string profileName = ssid.SSID; // this is also the SSID
string mac = StringToHex(profileName);
string myProfileXML = string.Format("<? xml version =\"1.0\"?> < WLANProfile xmlns =\"http://www.microsoft.com/networking/WLAN/profile/v1 Jump \"> <name>{0}</ name > <SSIDConfig> <SSID>< hex >{1}</ hex > <name>{0}</name> </SSID> </SSIDConfig> < connectionType > ESS </ connectionType > < connectionMode > manual </ connectionMode > < MSM > < security > < authEncryption > < authentication > open </ authentication > < encryption > none </ encryption > < useOneX > false </ useOneX > </ authEncryption > </ security > </ MSM > </ WLANProfile > ", profileName, mac);
ssid.wlanInterface.SetProfile(Wlan.WlanProfileFlags.AllUser, myProfileXML, true);
ssid.wlanInterface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName);
}
/// String to Hex
public static string StringToHex(string str)
{
StringBuilder sb = new StringBuilder();
byte[] byStr = System.Text.Encoding.Default.GetBytes(str);
for (int i = 0; i < byStr.Length; i++)
{
sb.Append(Convert.ToString(byStr[i], 16));
}
return (sb.ToString().ToUpper());
}
}
class WIFISSID
{
public string SSID = "NONE";
public string dot11DefaultAuthAlgorithm = "";
public string dot11DefaultCipherAlgorithm = "";
public bool networkConnectable = true;
public string wlanNotConnectableReason = "";
public int wlanSignalQuality = 0;
public WlanClient.WlanInterface wlanInterface = null;
}
/*
private void ComboSSID_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// WLAN2.MainWindow + WIFISSID
var selectedItem = comboSSID.SelectedItem;
var ssid = ((WIFISSID)selectedItem).SSID;
Console.WriteLine(ssid);
}
*/
}
}
<Window x:Class="WLAN2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="800">
<ComboBox Name="comboSSID" DisplayMemberPath="SSID" HorizontalAlignment="Left" VerticalAlignment="Top" Width="300" SelectionChanged="ComboSSID_SelectionChanged"/>
</Window>
Zur Info, der Teil muss wieder aktiviert werden, damit das Skript läuft.
Das Problem mit der Combo besteht jedoch weiterhin.
/*
private void ComboSSID_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// WLAN2.MainWindow + WIFISSID
var selectedItem = comboSSID.SelectedItem;
var ssid = ((WIFISSID)selectedItem).SSID;
Console.WriteLine(ssid);
}
*/
Bei DisplayMemberPath
muss eine Eigenschaft verwendet werden, kein reines Feld, also
public string SSID { get; set; } = "NONE";
(am besten auch die anderen Felder zu Eigenschaften umwandeln)
Du solltest bisher auch im VS-Ausgabefenster eine Binding-Warnung sehen (ansonsten in den Optionen höher einstellen).
Hallo
schau dir mal das MVVM-Entwurfsmuster an.
[Artikel] MVVM und DataBinding
Das war die Lösung, vielen Dank für die Hilfe!
public string SSID { get; set; } = "NONE";