File tree Expand file tree Collapse file tree
NETworkManager.Utilities.WPF Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ using System . Threading . Tasks ;
2+ using System . Windows ;
3+
4+ namespace NETworkManager . Utilities . WPF ;
5+
6+ /// <summary>
7+ /// Extension methods for <see cref="FrameworkElement" />.
8+ /// </summary>
9+ public static class FrameworkElementExtensions
10+ {
11+ /// <summary>
12+ /// Waits until the element is loaded (e.g. before showing a dialog from a view that was just created, which
13+ /// MahApps doesn't allow before the view is loaded). Completes immediately if the element is already loaded.
14+ /// </summary>
15+ /// <param name="element">Element to wait for.</param>
16+ /// <returns>Task that completes when the element is loaded.</returns>
17+ public static Task WaitForLoadedAsync ( this FrameworkElement element )
18+ {
19+ if ( element . IsLoaded )
20+ return Task . CompletedTask ;
21+
22+ var taskCompletionSource = new TaskCompletionSource ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
23+
24+ void OnLoaded ( object sender , RoutedEventArgs e )
25+ {
26+ element . Loaded -= OnLoaded ;
27+ taskCompletionSource . TrySetResult ( ) ;
28+ }
29+
30+ element . Loaded += OnLoaded ;
31+
32+ return taskCompletionSource . Task ;
33+ }
34+ }
Original file line number Diff line number Diff line change 1818 xmlns : wpfHelpers =" clr-namespace:NETworkManager.Utilities.WPF;assembly=NETworkManager.Utilities.WPF"
1919 xmlns : networkManager =" clr-namespace:NETworkManager"
2020 xmlns : hostControls =" clr-namespace:NETworkManager.Views.Controls"
21- Loaded =" UserControl_Loaded"
2221 mc : Ignorable =" d" d : DataContext =" {d:DesignInstance viewModels:PowerShellHostViewModel}" >
2322 <UserControl .Resources>
2423 <converters : BooleanToVisibilityCollapsedConverter x : Key =" BooleanToVisibilityCollapsedConverter" />
Original file line number Diff line number Diff line change 1+ using NETworkManager . Utilities . WPF ;
12using NETworkManager . ViewModels ;
2- using System . Threading . Tasks ;
3- using System . Windows ;
43
54namespace NETworkManager . Views ;
65
76public partial class PowerShellHostView : IApplicationRequestHandler
87{
98 private readonly PowerShellHostViewModel _viewModel = new ( ) ;
109
11- private bool _loaded ;
12-
1310 public PowerShellHostView ( )
1411 {
1512 InitializeComponent ( ) ;
1613 DataContext = _viewModel ;
1714 }
1815
19- private void UserControl_Loaded ( object sender , RoutedEventArgs e )
20- {
21- _loaded = true ;
22- }
23-
2416 public async void Open ( ApplicationRequest request )
2517 {
2618 // Wait for the interface to load, before displaying the dialog to connect a new profile...
2719 // MahApps will throw an exception...
28- while ( ! _loaded )
29- await Task . Delay ( 250 ) ;
20+ await this . WaitForLoadedAsync ( ) ;
3021
3122 if ( ! _viewModel . IsExecutableConfigured )
3223 return ;
Original file line number Diff line number Diff line change 1818 xmlns : wpfHelpers =" clr-namespace:NETworkManager.Utilities.WPF;assembly=NETworkManager.Utilities.WPF"
1919 xmlns : networkManager =" clr-namespace:NETworkManager"
2020 xmlns : hostControls =" clr-namespace:NETworkManager.Views.Controls"
21- Loaded =" UserControl_Loaded"
2221 mc : Ignorable =" d" d : DataContext =" {d:DesignInstance viewModels:PuTTYHostViewModel}" >
2322 <UserControl .Resources>
2423 <converters : BooleanToVisibilityCollapsedConverter x : Key =" BooleanToVisibilityCollapsedConverter" />
Original file line number Diff line number Diff line change 1+ using NETworkManager . Utilities . WPF ;
12using NETworkManager . ViewModels ;
2- using System . Threading . Tasks ;
3- using System . Windows ;
43
54namespace NETworkManager . Views ;
65
76public partial class PuTTYHostView : IApplicationRequestHandler
87{
98 private readonly PuTTYHostViewModel _viewModel = new ( ) ;
109
11- private bool _loaded ;
12-
1310 public PuTTYHostView ( )
1411 {
1512 InitializeComponent ( ) ;
1613 DataContext = _viewModel ;
1714 }
1815
19- private void UserControl_Loaded ( object sender , RoutedEventArgs e )
20- {
21- _loaded = true ;
22- }
23-
2416 public async void Open ( ApplicationRequest request )
2517 {
2618 // Wait for the interface to load, before displaying the dialog to connect a new profile...
2719 // MahApps will throw an exception...
28- while ( ! _loaded )
29- await Task . Delay ( 250 ) ;
20+ await this . WaitForLoadedAsync ( ) ;
3021
3122 if ( ! _viewModel . IsExecutableConfigured )
3223 return ;
Original file line number Diff line number Diff line change 1717 xmlns : wpfHelpers =" clr-namespace:NETworkManager.Utilities.WPF;assembly=NETworkManager.Utilities.WPF"
1818 xmlns : networkManager =" clr-namespace:NETworkManager"
1919 xmlns : hostControls =" clr-namespace:NETworkManager.Views.Controls"
20- Loaded =" UserControl_Loaded"
2120 mc : Ignorable =" d" d : DataContext =" {d:DesignInstance viewModels:RemoteDesktopHostViewModel}" >
2221 <UserControl .Resources>
2322 <converters : BooleanReverseToVisibilityCollapsedConverter x : Key =" BooleanReverseToVisibilityCollapsedConverter" />
Original file line number Diff line number Diff line change 1+ using NETworkManager . Utilities . WPF ;
12using NETworkManager . ViewModels ;
2- using System . Threading . Tasks ;
3- using System . Windows ;
43
54namespace NETworkManager . Views ;
65
76public partial class RemoteDesktopHostView : IApplicationRequestHandler
87{
98 private readonly RemoteDesktopHostViewModel _viewModel = new ( ) ;
109
11- private bool _loaded ;
12-
1310 public RemoteDesktopHostView ( )
1411 {
1512 InitializeComponent ( ) ;
1613 DataContext = _viewModel ;
1714 }
1815
19- private void UserControl_Loaded ( object sender , RoutedEventArgs e )
20- {
21- _loaded = true ;
22- }
23-
2416 public async void Open ( ApplicationRequest request )
2517 {
2618 // Wait for the interface to load, before displaying the dialog to connect a new Profile...
2719 // MahApps will throw an exception...
28- while ( ! _loaded )
29- await Task . Delay ( 250 ) ;
20+ await this . WaitForLoadedAsync ( ) ;
3021
3122 if ( request . Profile != null )
3223 _viewModel . ConnectProfile ( request . Profile ) ;
Original file line number Diff line number Diff line change 1717 xmlns : wpfHelpers =" clr-namespace:NETworkManager.Utilities.WPF;assembly=NETworkManager.Utilities.WPF"
1818 xmlns : networkManager =" clr-namespace:NETworkManager"
1919 xmlns : hostControls =" clr-namespace:NETworkManager.Views.Controls"
20- Loaded =" UserControl_Loaded"
2120 mc : Ignorable =" d" d : DataContext =" {d:DesignInstance viewModels:TigerVNCHostViewModel}" >
2221 <UserControl .Resources>
2322 <converters : BooleanToVisibilityCollapsedConverter x : Key =" BooleanToVisibilityCollapsedConverter" />
Original file line number Diff line number Diff line change 1+ using NETworkManager . Utilities . WPF ;
12using NETworkManager . ViewModels ;
2- using System . Threading . Tasks ;
3- using System . Windows ;
43
54namespace NETworkManager . Views ;
65
76public partial class TigerVNCHostView : IApplicationRequestHandler
87{
98 private readonly TigerVNCHostViewModel _viewModel = new ( ) ;
109
11- private bool _loaded ;
12-
1310 public TigerVNCHostView ( )
1411 {
1512 InitializeComponent ( ) ;
1613 DataContext = _viewModel ;
1714 }
1815
19- private void UserControl_Loaded ( object sender , RoutedEventArgs e )
20- {
21- _loaded = true ;
22- }
23-
2416 public async void Open ( ApplicationRequest request )
2517 {
2618 // Wait for the interface to load, before displaying the dialog to connect a new profile...
2719 // MahApps will throw an exception...
28- while ( ! _loaded )
29- await Task . Delay ( 250 ) ;
20+ await this . WaitForLoadedAsync ( ) ;
3021
3122 if ( ! _viewModel . IsConfigured )
3223 return ;
Original file line number Diff line number Diff line change 1818 xmlns : wpfHelpers =" clr-namespace:NETworkManager.Utilities.WPF;assembly=NETworkManager.Utilities.WPF"
1919 xmlns : networkManager =" clr-namespace:NETworkManager"
2020 xmlns : hostControls =" clr-namespace:NETworkManager.Views.Controls"
21- Loaded =" UserControl_Loaded"
2221 mc : Ignorable =" d" d : DataContext =" {d:DesignInstance viewModels:WebConsoleHostViewModel}" >
2322 <UserControl .Resources>
2423 <converters : BooleanToVisibilityCollapsedConverter x : Key =" BooleanToVisibilityCollapsedConverter" />
You can’t perform that action at this time.
0 commit comments