If your purpose is not to block the UI while scanning the networks, you can simply use async/await
. Consider the following simplified code example:
class WifiNetworkViewModel : ViewModelBase
{
public WifiNetworkViewModel()
{
Networks = new ObservableCollection<WifiNetwork>();
}
public ObservableCollection<WifiNetwork> Networks { get; }
public async Task ScanWifiNetworks()
{
while (true)
{
// Scanning logic here (use await)
// When found one, add it to the Networks collection
Networks.Add(new WifiNetwork { Name = "Wifi Network 1" });
// Break the while loop with timeout etc.
}
}
}
class WifiNetwork
{
public string Name { get; set; }
}
To use this view model, I assume you have a list control such as ListView
and bind it to the Networks
property like
<ListView ItemsSource="{Binding Networks}"/>
When a found network is added to the Networks
property then the ListView
will populate automatically. To start scanning, call the ScanWifiNetworks()
method from a Button
for example.