更新整理
This commit is contained in:
31
WPFluent.Gallery/Services/WindowsProviderService.cs
Normal file
31
WPFluent.Gallery/Services/WindowsProviderService.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
|
||||
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
|
||||
// All Rights Reserved.
|
||||
|
||||
namespace WPFluent.Gallery.Services;
|
||||
|
||||
public class WindowsProviderService
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
public WindowsProviderService(IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
public void Show<T>()
|
||||
where T : class
|
||||
{
|
||||
if (!typeof(Window).IsAssignableFrom(typeof(T)))
|
||||
{
|
||||
throw new InvalidOperationException($"The window class should be derived from {typeof(Window)}.");
|
||||
}
|
||||
|
||||
Window windowInstance =
|
||||
_serviceProvider.GetService<T>() as Window
|
||||
?? throw new InvalidOperationException("Window is not registered as service.");
|
||||
windowInstance.Owner = Application.Current.MainWindow;
|
||||
windowInstance.Show();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user