ContentPageWithContext implementation

This commit is contained in:
2024-08-27 18:38:36 +02:00
parent 12e4213f9e
commit 64813fd541
2 changed files with 28 additions and 1 deletions
@@ -0,0 +1,27 @@
using System.Extensions;
using System.Runtime.CompilerServices;
namespace Net.Maui.Extensions;
public abstract class ContentPageWithContext<T> : ContentPage
{
public new T BindingContext
{
get => base.BindingContext.Cast<T>();
set => base.BindingContext = value;
}
protected override void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
if (propertyName == BindingContextProperty.PropertyName)
{
if (base.BindingContext is not T)
{
throw new InvalidOperationException($"Binding context has been set to a value of type {base.BindingContext.GetType().Name}. Only values of type {typeof(T).Name}");
}
}
base.OnPropertyChanged(propertyName);
}
}
@@ -20,7 +20,7 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<IsPackable>true</IsPackable> <IsPackable>true</IsPackable>
<Version>0.2.5</Version> <Version>0.2.6</Version>
<Authors>Alexandru Macocian</Authors> <Authors>Alexandru Macocian</Authors>
<PackageLicenseFile>LICENSE</PackageLicenseFile> <PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance> <PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>