Last active
August 27, 2024 03:28
-
-
Save VergilGao/372801d390b09e91bd36c7d15fd2275c to your computer and use it in GitHub Desktop.
WPF scoped service provider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[GenerateDependencyProperty("Processing", typeof(bool))] | |
[GenerateDependencyProperty("PageSizeSelection", typeof(int[]))] | |
public partial class FormulaView : UserControl | |
{ | |
private readonly IMessenger messenger; | |
public FormulaView() | |
{ | |
InitializeComponent(); | |
var scope = App.ServiceProvider.CreateScope(); | |
messenger = scope.ServiceProvider.GetRequiredService<IMessenger>(); | |
Dispatcher.ShutdownStarted += (s, e) => | |
{ | |
scope.Dispose(); | |
}; | |
} | |
private static partial bool DefaultProcessingProperty() => false; | |
private static partial int[] DefaultPageSizeSelectionProperty() => [10, 20, 50, 100]; | |
private async void OnNewPointButtonClick(object sender, RoutedEventArgs e) | |
{ | |
try | |
{ | |
Processing = true; | |
var response = await messenger.Send(new NewPointMessage("new name")); | |
// do something... | |
} | |
catch (InvalidOperationException) { } | |
finally | |
{ | |
Processing = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment