private void OnLoaded(object sender, RoutedEventArgs e) { // if DataContext is null this does nothing (so it's safe) DialogParticipation.SetRegister(this, DataContext); NavigationService.Navigating += OnNavigating; _cachedService = NavigationService; DoLoad(); }
public AccountOpenView() { InitializeComponent(); DataContextChanged += (sender, args) => { DialogParticipation.SetRegister(this, this.DataContext); }; }
public override Task <MessageBoxResult> ShowMessageBoxAsync(object context, string message, string title, string trueText, BorderStyle?trueStyle, string falseText, BorderStyle?falseStyle, MessageBoxButton button, MessageBoxImage icon) { if (GetWindow(context) is MetroWindow mw) { Task <MessageBoxResult> showCore() { if (DialogParticipation.GetRegister(mw) != null) { switch (button) { case MessageBoxButton.YesNo: return(mw.ShowMessageAsync(title, message, MessageDialogStyle.AffirmativeAndNegative, new MetroDialogSettings { AffirmativeButtonText = !string.IsNullOrEmpty(trueText) ? trueText : "はい", NegativeButtonText = !string.IsNullOrEmpty(falseText) ? falseText : "いいえ", OwnerCanCloseWithDialog = true }).ContinueWith(t => t.Status != TaskStatus.RanToCompletion ? MessageBoxResult.None : t.Result == MessageDialogResult.Affirmative ? MessageBoxResult.Yes : MessageBoxResult.No)); case MessageBoxButton.OK: return(mw.ShowMessageAsync(title, message, MessageDialogStyle.Affirmative, new MetroDialogSettings { AffirmativeButtonText = !string.IsNullOrEmpty(trueText) ? trueText : "OK", OwnerCanCloseWithDialog = true }).ContinueWith(t => t.Status != TaskStatus.RanToCompletion ? MessageBoxResult.None : MessageBoxResult.OK)); } } return(base.ShowMessageBoxAsync(context, message, title, trueText, trueStyle, falseText, falseStyle, button, icon)); } if (mw.Dispatcher.Thread != Thread.CurrentThread) { var tcs = new TaskCompletionSource <MessageBoxResult>(); InvokeAsync(context, () => showCore().ContinueWith(t => { if (t.Status == TaskStatus.RanToCompletion) { tcs.TrySetResult(t.Result); } else if (t.Exception != null) { tcs.TrySetException(t.Exception); } else { tcs.TrySetCanceled(); } }, TaskScheduler.FromCurrentSynchronizationContext())); return(tcs.Task); } return(showCore()); } return(base.ShowMessageBoxAsync(context, message, title, trueText, trueStyle, falseText, falseStyle, button, icon)); }
public MainWindow() { InitializeComponent(); Loaded += (sender, e) => { DialogParticipation.SetRegister(this, this.DataContext); }; Unloaded += (sender, e) => { DialogParticipation.SetRegister(this, null); }; }
public FoodRequestView() { InitializeComponent(); DataContextChanged += (sender, args) => { DialogParticipation.SetRegister(this, this.DataContext); }; DataContextChanged += (sender, args) => { (this.DataContext as FoodRequestViewModel).Model.ReloadItems(); }; }
public LastPageView() { InitializeComponent(); this.Loaded += (sender, args) => { DialogParticipation.SetRegister(this, this.DataContext); ((IWizardPageLoadableViewModel)this.DataContext).LoadedCommand.Execute(this); }; // if using DialogParticipation on Windows which open / close frequently you will get a // memory leak unless you unregister. The easiest way to do this is in your Closing/ Unloaded // event, as so: // // DialogParticipation.SetRegister(this, null); this.Unloaded += (sender, args) => { DialogParticipation.SetRegister(this, null); }; }
private async Task RunGenerator(int index) { DependencyObject registration = null; if (_treeGeneratorWindow == null) { registration = DialogParticipation.GetAssociation(this); DialogParticipation.SetRegister(registration, _treeGeneratorViewModel); } var generator = _treeGeneratorViewModel.Tabs[index]; await _treeGeneratorViewModel.RunAsync(generator); if (registration != null) { DialogParticipation.SetRegister(registration, this); } }
/// <summary>Constructor.</summary> public RotarySwitchMappingControl() { InitializeComponent(); // TODO I'm sure this isn't the best way. DataContext is null after InitializeComponent() so handle its changed event. DataContextChanged += (object sender, DependencyPropertyChangedEventArgs e) => { m_rotarySwitchMapping = (DeviceInstance.Settings.RotarySwitchMapping)DataContext; if (m_rotarySwitchMapping != null) { // TODO ditto. Using dialog:DialogParticipation.Register="{Binding}" in the xaml doesn't work, presumably // because {Binding} resolves to null at that point. DialogParticipation.SetRegister(this, this); } }; }
protected override Task ShowModalAsync(object context, FrameworkElement frameworkElement) { if (GetWindow(context) is MetroWindow mw) { Task showCore() { if (DialogParticipation.GetRegister(mw) != null && !(frameworkElement is Window)) { ConfigureViewModel(frameworkElement.DataContext); var tcs = new TaskCompletionSource <object>(); if (frameworkElement is BaseMetroDialog d) { d.Unloaded += (s, e) => tcs.TrySetResult(null); mw.ShowMetroDialogAsync(d); } else { var gd = new GenericMetroDialog() { DataContext = frameworkElement.DataContext, Content = frameworkElement, }; gd.Unloaded += (s, e) => tcs.TrySetResult(null); mw.ShowMetroDialogAsync(gd); } return(tcs.Task); } return(base.ShowModalAsync(context, frameworkElement)); } if (mw.Dispatcher.Thread != Thread.CurrentThread) { return(InvokeAsync(context, showCore)); } return(showCore()); } return(base.ShowModalAsync(context, frameworkElement)); }
private Shell(ShellViewModel viewModel) { InitializeComponent(); ViewModel = viewModel; DialogParticipation.SetRegister(this, ViewModel); this.WhenActivated(d => { this.BindCommand(ViewModel, vm => vm.TestCommand, v => v.MenuItem_Test).DisposeWith(d); this.OneWayBind(ViewModel, vm => vm.TestName, v => v.TextBox_Test.Text).DisposeWith(d); this.BindCommand(ViewModel, vm => vm.LoadFileCommand, v => v.MenuItem_LoadFile).DisposeWith(d); this.BindCommand(ViewModel, vm => vm.UpCommand, v => v.Button_Up).DisposeWith(d); this.BindCommand(ViewModel, vm => vm.CreateNodeCommand, v => v.MenuItem_CreateNode).DisposeWith(d); this.OneWayBind(ViewModel, vm => vm.HasFile, v => v.Grid_Content.Visibility).DisposeWith(d); this.OneWayBind(ViewModel, vm => vm.NodeNames, v => v.ListBox_Nodes.ItemsSource).DisposeWith(d); this.Bind(ViewModel, vm => vm.SelectedNodeIndex, v => v.ListBox_Nodes.SelectedIndex).DisposeWith(d); this.OneWayBind(ViewModel, vm => vm.ItemViewModels, v => v.ListBox_BrowseItems.ItemsSource).DisposeWith(d); this.Bind(ViewModel, vm => vm.SelectedItemIndex, v => v.ListBox_BrowseItems.SelectedIndex).DisposeWith(d); this.BindCommand(ViewModel, vm => vm.SaveCommand, v => v.KeyBinding_Save).DisposeWith(d); }); }
private static CrystalWindow GetCrystalWindow(object context) { if (context is null) { throw new ArgumentNullException(nameof(context)); } if (DialogParticipation.IsRegistered(context) == false) { throw new InvalidOperationException($"The context `{context}` is not registered. Consider using the DialogParticipation.Register property in XAML to bind in the DataContext."); } var association = DialogParticipation.GetAssociation(context); var crystalWindow = association.Invoke(() => Window.GetWindow(association) as CrystalWindow); if (crystalWindow is null) { throw new InvalidOperationException($"The context `{context}` is not inside a CrystalWindow."); } return(crystalWindow); }
protected override bool CloseModal(object context, object viewModel) { var app = Application.Current; if (app != null) { foreach (var mw in app.Windows.OfType <MetroWindow>().ToList()) { if (DialogParticipation.GetRegister(mw) != null) { var dc = mw.FindChild <Panel>("PART_MetroActiveDialogContainer"); if (dc != null) { foreach (var d in dc.Children.OfType <BaseMetroDialog>().Where(e => e.DataContext == viewModel).ToList()) { mw.HideMetroDialogAsync(d).GetHashCode(); } } } } } return(base.CloseModal(context, viewModel)); }
private void OnUnloaded(object sender, RoutedEventArgs e) { DialogParticipation.SetRegister(this, null); _cachedService.Navigating -= OnNavigating; DoUnLoad(); }
public MetroDialogWindow(IDialogCoordinator coordinator) { DialogParticipation.SetRegister(this, this); }
/// <summary> /// If using DialogParticipation on Windows which open / close frequently you will get a /// memory leak unless you unregister. The easiest way to do this is in your Closing/ Unloaded event /// </summary> /// <param name="sender">Source of the event.</param> /// <param name="e"> Routed event information.</param> private void Shell_OnUnloaded(object sender, RoutedEventArgs e) => DialogParticipation.SetRegister(this, null);
public MainWindow() { InitializeComponent(); DialogParticipation.SetRegister(this, this); _dialogCoordinator = DialogCoordinator.Instance; }