| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="CultureChangedBehavior.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using MyNet.Globalization.Culture; |
| | | 9 | | using MyNet.Observable.Behaviors.Metadata.Features.Events; |
| | | 10 | | |
| | | 11 | | namespace MyNet.Observable.Behaviors; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Defines a behavior that listens for culture change events from an ICultureService and raises an event with the new c |
| | | 15 | | /// </summary> |
| | | 16 | | public sealed class CultureChangedBehavior : EventBehavior<ObservableObject, CultureChangedEvent> |
| | | 17 | | { |
| | | 18 | | private readonly ICultureService _service; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Initializes a new instance of the <see cref="CultureChangedBehavior"/> class with the specified owner and cultur |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="owner">The owner object of this behavior.</param> |
| | | 24 | | /// <param name="cultureService">The culture service to listen for changes.</param> |
| | | 25 | | /// <exception cref="ArgumentNullException">Thrown if the culture service is null.</exception> |
| | | 26 | | public CultureChangedBehavior(ObservableObject owner, ICultureService cultureService) |
| | 171 | 27 | | : base(owner) |
| | | 28 | | { |
| | 171 | 29 | | _service = cultureService ?? throw new ArgumentNullException(nameof(cultureService)); |
| | 171 | 30 | | _service.CultureChanged += OnCultureChangedCallback; |
| | 171 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Handles the CultureChanged event from the ICultureService. This method is called whenever the culture changes, a |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="sender">The source of the event.</param> |
| | | 37 | | /// <param name="e">The event data containing the new culture information.</param> |
| | | 38 | | private void OnCultureChangedCallback(object? sender, CultureChangedEventArgs e) |
| | | 39 | | { |
| | 252 | 40 | | if (IsDisposed || IsSuspended) |
| | 0 | 41 | | return; |
| | | 42 | | |
| | 252 | 43 | | RaiseEvent(new(e.NewCulture)); |
| | 252 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc/> |
| | | 47 | | protected override void DisposeManagedResources() |
| | | 48 | | { |
| | 3 | 49 | | _service.CultureChanged -= OnCultureChangedCallback; |
| | 3 | 50 | | base.DisposeManagedResources(); |
| | 3 | 51 | | } |
| | | 52 | | } |
| | | 53 | | |