6

New methods to override for ViewModelBase in TinyMvvm

 2 years ago
source link: https://danielhindrikes.se/index.php/2021/12/29/new-methods-to-override-for-viewmodelbase-in-tinymvvm/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

New methods to override for ViewModelBase in TinyMvvm

In the latest preview of TinyMvvm, 4.0.1-pre4, there are new methods added to the ViewModelBase class. The new methods will make it easier to handle App lifecycle events in a ViewModel. For example, if you want to unsubscribe from an event when the app goes to sleep. Before you had to handle this in the Application class.

Following methods has been added for you to override:

  • Task OnApplicationResume();
  • Task OnApplicationSleep();

public class MyViewModel : ViewModelBase { public event EventHandler DataUpdated

public override Task Initialize() { //Subscribe to event when ViewModel in initialized. DataUpdated += Data_DataUpdated; }

public override Task OnApplicationSleep() { //Unsubscribe to event. DataUpdated -= Data_DataUpdated; } public override Task OnApplicationResume() { //Subcribe to event again. DataUpdated += Data_DataUpdated; } private void Data_Updated(object sender, EventArgs e) { } }

Those new features are available for both TinyMvvm.Forms (for Xamarin.Forms) and TinyMvvm.Maui (for .NET MAUI). To make them work you need to change the base class of your App class to TinyApplication instead of Application.

<mvvm:TinyApplication xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:windows="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.WindowsSpecific;assembly=Microsoft.Maui.Controls" xmlns:local="clr-namespace:MauiSample" xmlns:mvvm="clr-namespace:TinyMvvm.Maui;assembly=TinyMvvm.Maui" x:Class="MauiSample.App" windows:Application.ImageDirectory="Assets"> <Application.Resources> <ResourceDictionary>

</ResourceDictionary> </Application.Resources> </mvvm:TinyApplication>

If you don't want to change to TinyApplication, nothing old will be broken, but the new methods will not work.

The code for this release is currently just in the maui branch, https://github.com/TinyStuff/TinyMvvm/tree/4.0.1-pre4


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK