I cannot find the WeakEventManger class in WINUI3.
All my WPF projects uses this in the view models when I subscribe on model events that lives longer than the ViewModel.
ViewModels with long living models must be a common thing.
E.g. a short living view model subscribing to the Clipboard.
What is best pratise in WINUI3?
public class ItemViewModel : INotifyPropertyChanged
{
private string _text;
public ItemViewModel()
{
// GC collects MainViewModel, but WeakEventManager does not exist in WINUI3
//WeakEventManager<Clipboard, object>.AddHandler(Clipboard, nameof(Clipboard.ContentChanged), OnClipboardContentChanged);
// GC does not collect MainViewModel
Clipboard.ContentChanged += OnClipboardContentChanged;
}
private void OnClipboardContentChanged(object sender, object e)
{
Text = Clipboard.GetContent().GetTextAsync().AsTask().Result;
}
public string Text
{
get { return _text; }
set
{
if (_text != value)
{
_text = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Text)));
}
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
It's marked as hidden from Intellisense for some reason, but it's under the
CommunityToolkit.WinUI.Helpers
namespace:https://github.com/CommunityToolkit/Windows/blob/main/components/Helpers/src/WeakEventListener.cs