Skip to content

Commit

Permalink
Automatically set MainWindow when showing the first window
Browse files Browse the repository at this point in the history
  • Loading branch information
mysteryx93 committed Mar 18, 2023
1 parent d0eecc7 commit e4a327e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion samples/Wpf/Demo.ViewEvents/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ protected override void OnStartup(StartupEventArgs e)
var dialogService = Ioc.Default.GetRequiredService<IDialogService>();
var vm = dialogService.CreateViewModel<MainWindowViewModel>();
dialogService.Show(null, vm);
Application.Current.MainWindow = Application.Current.Windows[0];
//Application.Current.MainWindow = Application.Current.Windows[0];
}
}
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<VersionPrefix>2.0.0</VersionPrefix>
<VersionSuffix>preview6</VersionSuffix>
<VersionSuffix>preview7</VersionSuffix>
<Authors>Etienne Charland</Authors>
<Company>Hanuman Institute</Company>
<Product>HanumanInstitute.MvvmDialogs</Product>
Expand Down
2 changes: 1 addition & 1 deletion src/MvvmDialogs.Avalonia.Fluent/FluentDialogFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private static TaskDialogButton[] SyncButton(MessageBoxButton value, bool? defau
};

private static TaskDialogButton GetButton(string text, bool? value, bool? defaultValue) =>
new TaskDialogButton(text, value)
new(text, value)
{
IsDefault = defaultValue == value
};
Expand Down
19 changes: 16 additions & 3 deletions src/MvvmDialogs.Avalonia/ViewWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// ReSharper disable VirtualMemberCallInConstructor

using System.Collections.Generic;
using Avalonia.Controls.ApplicationLifetimes;

namespace HanumanInstitute.MvvmDialogs.Avalonia;

Expand Down Expand Up @@ -91,15 +92,19 @@ public INotifyPropertyChanged ViewModel
/// <inheritdoc />
public Task ShowDialogAsync(IView owner)
{
return Ref.ShowDialog<bool?>((Window)owner.RefObj);
var window = (Window)owner.RefObj;
SetMainWindowIfEmpty(window);
return Ref.ShowDialog<bool?>(window);
}

/// <inheritdoc />
public void Show(IView? owner)
{
if (owner?.RefObj != null)
var own = owner?.RefObj as Window;
SetMainWindowIfEmpty(Ref);
if (own != null)
{
Ref.Show((Window)owner.RefObj);
Ref.Show(own);
}
else
{
Expand All @@ -125,4 +130,12 @@ public bool IsEnabled

/// <inheritdoc />
public bool ClosingConfirmed { get; set; }

private void SetMainWindowIfEmpty(Window? window)
{
if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow: null } desktop)
{
desktop.MainWindow = window;
}
}
}
2 changes: 1 addition & 1 deletion src/MvvmDialogs/FrameworkDialogs/FileDialogSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public abstract class FileDialogSettings : DialogSettingsBase
/// to the descriptions unless it contains '('.
/// If you do not wish to display extensions, end the name with '()' and it will be trimmed away.
/// </remarks>
public List<FileFilter> Filters { get; set; } = new List<FileFilter>();
public List<FileFilter> Filters { get; set; } = new();

/// <summary>
/// Gets or sets the initial directory that is displayed by a file dialog.
Expand Down

0 comments on commit e4a327e

Please sign in to comment.