Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgraded VS Extension to use WPF-UI 4.0.0 #1344

Merged
merged 2 commits into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/Wpf.Ui.Extension.Template.Blank/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,19 @@ public partial class App
}).Build();

/// <summary>
/// Gets registered service.
/// Gets services.
/// </summary>
/// <typeparam name="T">Type of the service to get.</typeparam>
/// <returns>Instance of the service or <see langword="null"/>.</returns>
public static T GetService<T>()
where T : class
public static IServiceProvider Services
{
return _host.Services.GetService(typeof(T)) as T;
get { return _host.Services; }
}

/// <summary>
/// Occurs when the application is loading.
/// </summary>
private void OnStartup(object sender, StartupEventArgs e)
private async void OnStartup(object sender, StartupEventArgs e)
{
_host.Start();
await _host.StartAsync();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider wrapping this in try/catch due to async void.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Same for the other two templates.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any suggestions on what to do if an Exception got caught? I basically copied this approach from the Wpf.Ui.Demo.Mvvm project and I'm not sure what else we can do when application initialization and startup fail.
We could call Environment.Exit(1)?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, there's not much that can be done.

I believe not catching it will in this case just send the exception to Windows' Event Log, so the user may be confused why the app is just gone. IMHO, you should present the exception in a plain MessageBox, so it's at least clear that a crash occurred. (WinForms has a built in exception dialog, but I believe WPF does not.)

}

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions src/Wpf.Ui.Extension.Template.Blank/Wpf.Ui.Blank.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<TargetFramework>net9.0-windows</TargetFramework>
<ApplicationManifest>app.manifest</ApplicationManifest>
<ApplicationIcon>wpfui-icon.ico</ApplicationIcon>
<UseWPF>true</UseWPF>
Expand All @@ -15,9 +15,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="WPF-UI" Version="3.0.5" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0 " />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2 "/>
<PackageReference Include="WPF-UI" Version="4.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.1" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0 "/>
</ItemGroup>

<ItemGroup>
Expand Down
19 changes: 8 additions & 11 deletions src/Wpf.Ui.Extension.Template.Compact/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using $safeprojectname$.Views.Pages;
using $safeprojectname$.Views.Windows;
using Wpf.Ui;
using Wpf.Ui.DependencyInjection;

namespace $safeprojectname$
{
Expand All @@ -28,10 +29,9 @@ public partial class App
.ConfigureAppConfiguration(c => { c.SetBasePath(Path.GetDirectoryName(AppContext.BaseDirectory)); })
.ConfigureServices((context, services) =>
{
services.AddHostedService<ApplicationHostService>();
services.AddNavigationViewPageProvider();

// Page resolver service
services.AddSingleton<IPageService, PageService>();
services.AddHostedService<ApplicationHostService>();

// Theme manipulation
services.AddSingleton<IThemeService, ThemeService>();
Expand All @@ -55,22 +55,19 @@ public partial class App
}).Build();

/// <summary>
/// Gets registered service.
/// Gets services.
/// </summary>
/// <typeparam name="T">Type of the service to get.</typeparam>
/// <returns>Instance of the service or <see langword="null"/>.</returns>
public static T GetService<T>()
where T : class
public static IServiceProvider Services
{
return _host.Services.GetService(typeof(T)) as T;
get { return _host.Services; }
}

/// <summary>
/// Occurs when the application is loading.
/// </summary>
private void OnStartup(object sender, StartupEventArgs e)
private async void OnStartup(object sender, StartupEventArgs e)
{
_host.Start();
await _host.StartAsync();
}

/// <summary>
Expand Down
42 changes: 0 additions & 42 deletions src/Wpf.Ui.Extension.Template.Compact/Services/PageService.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Windows.Media;
using $safeprojectname$.Models;
using Wpf.Ui.Controls;
using Wpf.Ui.Abstractions.Controls;

namespace $safeprojectname$.ViewModels.Pages
{
Expand All @@ -11,13 +11,15 @@ public partial class DataViewModel : ObservableObject, INavigationAware
[ObservableProperty]
private IEnumerable<DataColor> _colors;

public void OnNavigatedTo()
public Task OnNavigatedToAsync()
{
if (!_isInitialized)
InitializeViewModel();

return Task.CompletedTask;
}

public void OnNavigatedFrom() { }
public Task OnNavigatedFromAsync() => Task.CompletedTask;

private void InitializeViewModel()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Wpf.Ui.Appearance;
using Wpf.Ui.Controls;
using Wpf.Ui.Abstractions.Controls;

namespace $safeprojectname$.ViewModels.Pages
{
Expand All @@ -13,13 +13,15 @@ public partial class SettingsViewModel : ObservableObject, INavigationAware
[ObservableProperty]
private ApplicationTheme _currentTheme = ApplicationTheme.Unknown;

public void OnNavigatedTo()
public Task OnNavigatedToAsync()
{
if (!_isInitialized)
InitializeViewModel();

return Task.CompletedTask;
}

public void OnNavigatedFrom() { }
public Task OnNavigatedFromAsync() => Task.CompletedTask;

private void InitializeViewModel()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using $safeprojectname$.ViewModels.Pages;
using Wpf.Ui.Controls;
using Wpf.Ui.Abstractions.Controls;

namespace $safeprojectname$.Views.Pages
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using $safeprojectname$.ViewModels.Pages;
using Wpf.Ui.Controls;
using Wpf.Ui.Abstractions.Controls;

namespace $safeprojectname$.Views.Pages
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using $safeprojectname$.ViewModels.Pages;
using Wpf.Ui.Controls;
using Wpf.Ui.Abstractions.Controls;

namespace $safeprojectname$.Views.Pages
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using $safeprojectname$.ViewModels.Windows;
using Wpf.Ui;
using Wpf.Ui.Abstractions;
using Wpf.Ui.Appearance;
using Wpf.Ui.Controls;

Expand All @@ -11,7 +12,7 @@ public partial class MainWindow : INavigationWindow

public MainWindow(
MainWindowViewModel viewModel,
IPageService pageService,
INavigationViewPageProvider navigationViewPageProvider,
INavigationService navigationService
)
{
Expand All @@ -21,7 +22,7 @@ INavigationService navigationService
SystemThemeWatcher.Watch(this);

InitializeComponent();
SetPageService(pageService);
SetPageService(navigationViewPageProvider);

navigationService.SetNavigationControl(RootNavigation);
}
Expand All @@ -32,7 +33,7 @@ INavigationService navigationService

public bool Navigate(Type pageType) => RootNavigation.Navigate(pageType);

public void SetPageService(IPageService pageService) => RootNavigation.SetPageService(pageService);
public void SetPageService(INavigationViewPageProvider navigationViewPageProvider) => RootNavigation.SetPageProviderService(navigationViewPageProvider);

public void ShowWindow() => Show();

Expand Down
9 changes: 5 additions & 4 deletions src/Wpf.Ui.Extension.Template.Compact/Wpf.Ui.Compact.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<TargetFramework>net9.0-windows</TargetFramework>
<ApplicationManifest>app.manifest</ApplicationManifest>
<ApplicationIcon>wpfui-icon.ico</ApplicationIcon>
<UseWPF>true</UseWPF>
Expand All @@ -15,9 +15,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="WPF-UI" Version="3.0.5" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0 " />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2 "/>
<PackageReference Include="WPF-UI" Version="4.0.0" />
<PackageReference Include="WPF-UI.DependencyInjection" Version="4.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.1" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0 "/>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
</Folder>
<Folder Name="Services" TargetFolderName="Services">
<ProjectItem ReplaceParameters="true" TargetFileName="ApplicationHostService.cs">ApplicationHostService.cs</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="PageService.cs">PageService.cs</ProjectItem>
</Folder>
<Folder Name="ViewModels" TargetFolderName="ViewModels">
<Folder Name="Pages" TargetFolderName="Pages">
Expand Down
19 changes: 8 additions & 11 deletions src/Wpf.Ui.Extension.Template.Fluent/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using $safeprojectname$.Views.Pages;
using $safeprojectname$.Views.Windows;
using Wpf.Ui;
using Wpf.Ui.DependencyInjection;

namespace $safeprojectname$
{
Expand All @@ -28,10 +29,9 @@ public partial class App
.ConfigureAppConfiguration(c => { c.SetBasePath(Path.GetDirectoryName(AppContext.BaseDirectory)); })
.ConfigureServices((context, services) =>
{
services.AddHostedService<ApplicationHostService>();
services.AddNavigationViewPageProvider();

// Page resolver service
services.AddSingleton<IPageService, PageService>();
services.AddHostedService<ApplicationHostService>();

// Theme manipulation
services.AddSingleton<IThemeService, ThemeService>();
Expand All @@ -55,22 +55,19 @@ public partial class App
}).Build();

/// <summary>
/// Gets registered service.
/// Gets services.
/// </summary>
/// <typeparam name="T">Type of the service to get.</typeparam>
/// <returns>Instance of the service or <see langword="null"/>.</returns>
public static T GetService<T>()
where T : class
public static IServiceProvider Services
{
return _host.Services.GetService(typeof(T)) as T;
get { return _host.Services; }
}

/// <summary>
/// Occurs when the application is loading.
/// </summary>
private void OnStartup(object sender, StartupEventArgs e)
private async void OnStartup(object sender, StartupEventArgs e)
{
_host.Start();
await _host.StartAsync();
}

/// <summary>
Expand Down
42 changes: 0 additions & 42 deletions src/Wpf.Ui.Extension.Template.Fluent/Services/PageService.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Windows.Media;
using $safeprojectname$.Models;
using Wpf.Ui.Controls;
using Wpf.Ui.Abstractions.Controls;

namespace $safeprojectname$.ViewModels.Pages
{
Expand All @@ -11,13 +11,15 @@ public partial class DataViewModel : ObservableObject, INavigationAware
[ObservableProperty]
private IEnumerable<DataColor> _colors;

public void OnNavigatedTo()
public Task OnNavigatedToAsync()
{
if (!_isInitialized)
InitializeViewModel();

return Task.CompletedTask;
}

public void OnNavigatedFrom() { }
public Task OnNavigatedFromAsync() => Task.CompletedTask;

private void InitializeViewModel()
{
Expand Down
Loading