Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

Commit

Permalink
added IPageAware to Respond to the page lifecycle in the viewmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
vniehues committed Jan 20, 2022
1 parent 5e7f878 commit c2056ee
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
13 changes: 12 additions & 1 deletion samples/mavvmApp/ViewModels/SecondPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Hosting;
using mavvm;
using mavvm.Interfaces;

namespace mavvmApp.ViewModels
{
[QueryProperty(nameof(Count), "countParam")]
public class SecondPageViewModel : BindableBase
public class SecondPageViewModel : BindableBase, IPageAware
{
string _title;
public string Title
Expand Down Expand Up @@ -54,6 +55,16 @@ async void LogCount()
var optionAlert = await BaseMethods.ShowAlert("Count", $"Count is {Count}", "Cancel", "Ok");
_consoleService.Log(optionAlert.ToString());
}

public void Appearing()
{
_consoleService.Log("Appearing");
}

public void Disappearing()
{
_consoleService.Log("Disappearing");
}
}
}

14 changes: 9 additions & 5 deletions src/mavvm/Factories/MavvmResolveRouteFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using mavvm.Interfaces;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.Controls;

Expand All @@ -13,11 +14,14 @@ public override Element GetOrCreate()
BindingContext = MavvmContainer.ServiceProvider.GetRequiredService<TViewModel>()
};

//if (view.BindingContext is IPageAware pageAware)
//{
// view.Disappearing += (s, e) => pageAware.Disappearing();
// view.Appearing += (s, e) => pageAware.Appearing();
//}
if (view.BindingContext is IPageAware pageAware)
{
view.Disappearing -= (s, e) => pageAware.Disappearing();
view.Appearing -= (s, e) => pageAware.Appearing();

view.Disappearing += (s, e) => pageAware.Disappearing();
view.Appearing += (s, e) => pageAware.Appearing();
}

return view;
} 
Expand Down
10 changes: 10 additions & 0 deletions src/mavvm/Interfaces/IPageAware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
namespace mavvm.Interfaces
{
public interface IPageAware
{
void Appearing();
void Disappearing();
}
}

4 changes: 3 additions & 1 deletion src/mavvm/mavvm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'net6.0-android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$(TargetFramework.Contains('-windows'))">10.0.18362.0</SupportedOSPlatformVersion>
<PackageId>mavvm</PackageId>
<PackageVersion>0.9.4</PackageVersion>
<PackageVersion>0.9.5</PackageVersion>
<Authors>Vincent Niehues</Authors>
<Copyright>Vincent Niehues 2021</Copyright>
<Owners>Vincent Niehues</Owners>
Expand All @@ -28,10 +28,12 @@ It uses the Shell we all know from xamarin together with the all new .net maui w
<None Remove="ViewModels\" />
<None Remove="Containers\" />
<None Remove="Navigation\" />
<None Remove="Interfaces\" />
</ItemGroup>
<ItemGroup>
<Folder Include="Implementations\" />
<Folder Include="Containers\" />
<Folder Include="Navigation\" />
<Folder Include="Interfaces\" />
</ItemGroup>
</Project>

0 comments on commit c2056ee

Please sign in to comment.