@@ -31,9 +31,6 @@ namespace NETworkManager.ViewModels;
3131public class WiFiViewModel : ViewModelBase
3232{
3333 #region Variables
34-
35- private readonly IDialogCoordinator _dialogCoordinator ;
36-
3734 private static readonly ILog Log = LogManager . GetLogger ( typeof ( WiFiViewModel ) ) ;
3835
3936 private readonly bool _isLoading ;
@@ -440,12 +437,10 @@ private set
440437
441438 #region Constructor, load settings
442439
443- public WiFiViewModel ( IDialogCoordinator instance )
440+ public WiFiViewModel ( )
444441 {
445442 _isLoading = true ;
446443
447- _dialogCoordinator = instance ;
448-
449444 // Check if Microsoft.Windows.SDK.Contracts is available
450445 SdkContractAvailable = ApiInformation . IsTypePresent ( "Windows.Devices.WiFi.WiFiAdapter" ) ;
451446
@@ -792,124 +787,128 @@ private LineSeries GetSeriesCollection(WiFiNetworkInfo network)
792787 } ;
793788 }
794789
795- private async void Connect ( )
790+ private Task Connect ( )
796791 {
797792 var selectedAdapter = SelectedAdapter ;
798793 var selectedNetwork = SelectedNetwork ;
799794
800795 var connectMode = WiFi . GetConnectMode ( selectedNetwork . AvailableNetwork ) ;
801796
802- var customDialog = new CustomDialog
797+ var childWindow = new WiFiConnectChildWindow ( ) ;
798+
799+ var childWindowViewModel = new WiFiConnectViewModel ( async instance =>
803800 {
804- Title = selectedNetwork . IsHidden
805- ? Strings . HiddenNetwork
806- : string . Format ( Strings . ConnectToXXX , selectedNetwork . AvailableNetwork . Ssid )
807- } ;
801+ // Connect Open/PSK/EAP
802+ childWindow . IsOpen = false ;
803+ ConfigurationManager . Current . IsChildWindowOpen = false ;
804+
805+ var ssid = selectedNetwork . IsHidden ? instance . Ssid : selectedNetwork . AvailableNetwork . Ssid ;
806+
807+ // Show status message
808+ IsConnecting = true ;
809+ ConnectionStatusMessage = string . Format ( Strings . ConnectingToXXX , ssid ) ;
810+ IsConnectionStatusMessageDisplayed = true ;
811+
812+ // Connect to the network
813+ var reconnectionKind = instance . ConnectAutomatically
814+ ? WiFiReconnectionKind . Automatic
815+ : WiFiReconnectionKind . Manual ;
816+
817+ PasswordCredential credential = new ( ) ;
808818
809- var connectViewModel = new WiFiConnectViewModel ( async instance =>
819+ switch ( instance . ConnectMode )
810820 {
811- // Connect Open/PSK/EAP
812- await _dialogCoordinator . HideMetroDialogAsync ( this , customDialog ) ;
821+ case WiFiConnectMode . Psk :
822+ credential . Password = SecureStringHelper . ConvertToString ( instance . PreSharedKey ) ;
823+ break ;
824+ case WiFiConnectMode . Eap :
825+ credential . UserName = instance . Username ;
813826
814- var ssid = selectedNetwork . IsHidden ? instance . Ssid : selectedNetwork . AvailableNetwork . Ssid ;
827+ if ( ! string . IsNullOrEmpty ( instance . Domain ) )
828+ credential . Resource = instance . Domain ;
815829
816- // Show status message
817- IsConnecting = true ;
818- ConnectionStatusMessage = string . Format ( Strings . ConnectingToXXX , ssid ) ;
819- IsConnectionStatusMessageDisplayed = true ;
830+ credential . Password = SecureStringHelper . ConvertToString ( instance . Password ) ;
831+ break ;
832+ }
820833
821- // Connect to the network
822- var reconnectionKind = instance . ConnectAutomatically
823- ? WiFiReconnectionKind . Automatic
824- : WiFiReconnectionKind . Manual ;
834+ WiFiConnectionStatus connectionResult ;
825835
826- PasswordCredential credential = new ( ) ;
836+ if ( selectedNetwork . IsHidden )
837+ connectionResult = await WiFi . ConnectAsync ( instance . Options . AdapterInfo . WiFiAdapter ,
838+ instance . Options . NetworkInfo . AvailableNetwork , reconnectionKind , credential , instance . Ssid ) ;
839+ else
840+ connectionResult = await WiFi . ConnectAsync ( instance . Options . AdapterInfo . WiFiAdapter ,
841+ instance . Options . NetworkInfo . AvailableNetwork , reconnectionKind , credential ) ;
827842
828- switch ( instance . ConnectMode )
829- {
830- case WiFiConnectMode . Psk :
831- credential . Password = SecureStringHelper . ConvertToString ( instance . PreSharedKey ) ;
832- break ;
833- case WiFiConnectMode . Eap :
834- credential . UserName = instance . Username ;
843+ // Done connecting
844+ IsConnecting = false ;
835845
836- if ( ! string . IsNullOrEmpty ( instance . Domain ) )
837- credential . Resource = instance . Domain ;
846+ // Get result
847+ ConnectionStatusMessage = connectionResult == WiFiConnectionStatus . Success
848+ ? string . Format ( Strings . SuccessfullyConnectedToXXX , ssid )
849+ : string . Format ( Strings . CouldNotConnectToXXXReasonXXX , ssid ,
850+ ResourceTranslator . Translate ( ResourceIdentifier . WiFiConnectionStatus , connectionResult ) ) ;
838851
839- credential . Password = SecureStringHelper . ConvertToString ( instance . Password ) ;
840- break ;
841- }
852+ // Hide message automatically
853+ _hideConnectionStatusMessageTimer . Start ( ) ;
854+
855+ // Update the Wi-Fi networks.
856+ // Wait because an error may occur if a refresh is done directly after connecting.
857+ await ScanAsync ( SelectedAdapter , true , 5000 ) ;
858+ } , async instance =>
859+ {
860+ // Connect WPS
861+ childWindow . IsOpen = false ;
862+ ConfigurationManager . Current . IsChildWindowOpen = false ;
842863
843- WiFiConnectionStatus connectionResult ;
864+ var ssid = selectedNetwork . IsHidden ? instance . Ssid : selectedNetwork . AvailableNetwork . Ssid ;
844865
845- if ( selectedNetwork . IsHidden )
846- connectionResult = await WiFi . ConnectAsync ( instance . Options . AdapterInfo . WiFiAdapter ,
847- instance . Options . NetworkInfo . AvailableNetwork , reconnectionKind , credential , instance . Ssid ) ;
848- else
849- connectionResult = await WiFi . ConnectAsync ( instance . Options . AdapterInfo . WiFiAdapter ,
850- instance . Options . NetworkInfo . AvailableNetwork , reconnectionKind , credential ) ;
866+ // Show status message
867+ IsConnecting = true ;
868+ ConnectionStatusMessage = string . Format ( Strings . ConnectingToXXX , ssid ) ;
869+ IsConnectionStatusMessageDisplayed = true ;
851870
852- // Done connecting
853- IsConnecting = false ;
871+ // Connect to the network
872+ var reconnectionKind = instance . ConnectAutomatically
873+ ? WiFiReconnectionKind . Automatic
874+ : WiFiReconnectionKind . Manual ;
854875
855- // Get result
856- ConnectionStatusMessage = connectionResult == WiFiConnectionStatus . Success
857- ? string . Format ( Strings . SuccessfullyConnectedToXXX , ssid )
858- : string . Format ( Strings . CouldNotConnectToXXXReasonXXX , ssid ,
859- ResourceTranslator . Translate ( ResourceIdentifier . WiFiConnectionStatus , connectionResult ) ) ;
876+ var connectionResult = await WiFi . ConnectWpsAsync (
877+ instance . Options . AdapterInfo . WiFiAdapter , instance . Options . NetworkInfo . AvailableNetwork ,
878+ reconnectionKind ) ;
860879
861- // Hide message automatically
862- _hideConnectionStatusMessageTimer . Start ( ) ;
880+ // Done connecting
881+ IsConnecting = false ;
863882
864- // Update the Wi-Fi networks.
865- // Wait because an error may occur if a refresh is done directly after connecting.
866- await ScanAsync ( SelectedAdapter , true , 5000 ) ;
867- } , async instance =>
868- {
869- // Connect WPS
870- await _dialogCoordinator . HideMetroDialogAsync ( this , customDialog ) ;
871-
872- var ssid = selectedNetwork . IsHidden ? instance . Ssid : selectedNetwork . AvailableNetwork . Ssid ;
873-
874- // Show status message
875- IsConnecting = true ;
876- ConnectionStatusMessage = string . Format ( Strings . ConnectingToXXX , ssid ) ;
877- IsConnectionStatusMessageDisplayed = true ;
878-
879- // Connect to the network
880- var reconnectionKind = instance . ConnectAutomatically
881- ? WiFiReconnectionKind . Automatic
882- : WiFiReconnectionKind . Manual ;
883-
884- var connectionResult = await WiFi . ConnectWpsAsync (
885- instance . Options . AdapterInfo . WiFiAdapter , instance . Options . NetworkInfo . AvailableNetwork ,
886- reconnectionKind ) ;
887-
888- // Done connecting
889- IsConnecting = false ;
890-
891- // Get result
892- ConnectionStatusMessage = connectionResult == WiFiConnectionStatus . Success
893- ? string . Format ( Strings . SuccessfullyConnectedToXXX , ssid )
894- : string . Format ( Strings . CouldNotConnectToXXXReasonXXX , ssid ,
895- ResourceTranslator . Translate ( ResourceIdentifier . WiFiConnectionStatus , connectionResult ) ) ;
896-
897- // Hide message automatically
898- _hideConnectionStatusMessageTimer . Start ( ) ;
899-
900- // Update the Wi-Fi networks.
901- // Wait because an error may occur if a refresh is done directly after connecting.
902- await ScanAsync ( SelectedAdapter , true , 5000 ) ;
903- } ,
904- _ => { _dialogCoordinator . HideMetroDialogAsync ( this , customDialog ) ; } , ( selectedAdapter , selectedNetwork ) ,
883+ // Get result
884+ ConnectionStatusMessage = connectionResult == WiFiConnectionStatus . Success
885+ ? string . Format ( Strings . SuccessfullyConnectedToXXX , ssid )
886+ : string . Format ( Strings . CouldNotConnectToXXXReasonXXX , ssid ,
887+ ResourceTranslator . Translate ( ResourceIdentifier . WiFiConnectionStatus , connectionResult ) ) ;
888+
889+ // Hide message automatically
890+ _hideConnectionStatusMessageTimer . Start ( ) ;
891+
892+ // Update the Wi-Fi networks.
893+ // Wait because an error may occur if a refresh is done directly after connecting.
894+ await ScanAsync ( SelectedAdapter , true , 5000 ) ;
895+ } ,
896+ _ => {
897+ childWindow . IsOpen = false ;
898+ ConfigurationManager . Current . IsChildWindowOpen = false ;
899+ } , ( selectedAdapter , selectedNetwork ) ,
905900 connectMode ) ;
906901
907- customDialog . Content = new WiFiConnectDialog
908- {
909- DataContext = connectViewModel
910- } ;
911902
912- await _dialogCoordinator . ShowMetroDialogAsync ( this , customDialog ) ;
903+ childWindow . Title = selectedNetwork . IsHidden
904+ ? Strings . HiddenNetwork
905+ : string . Format ( Strings . ConnectToXXX , selectedNetwork . AvailableNetwork . Ssid ) ;
906+
907+ childWindow . DataContext = childWindowViewModel ;
908+
909+ ConfigurationManager . Current . IsChildWindowOpen = true ;
910+
911+ return Application . Current . MainWindow . ShowChildWindowAsync ( childWindow ) ;
913912 }
914913
915914 private async void Disconnect ( )
0 commit comments