From 64813fd541f9937b8a9355acd45d4f0ec3f1b4b1 Mon Sep 17 00:00:00 2001 From: Alex Macocian Date: Tue, 27 Aug 2024 18:38:36 +0200 Subject: [PATCH] ContentPageWithContext implementation --- Net.Maui.Extensions/ContentPageWithContext.cs | 27 +++++++++++++++++++ .../Net.Maui.Extensions.csproj | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 Net.Maui.Extensions/ContentPageWithContext.cs diff --git a/Net.Maui.Extensions/ContentPageWithContext.cs b/Net.Maui.Extensions/ContentPageWithContext.cs new file mode 100644 index 0000000..480b86c --- /dev/null +++ b/Net.Maui.Extensions/ContentPageWithContext.cs @@ -0,0 +1,27 @@ +using System.Extensions; +using System.Runtime.CompilerServices; + +namespace Net.Maui.Extensions; + +public abstract class ContentPageWithContext : ContentPage +{ + public new T BindingContext + { + get => base.BindingContext.Cast(); + 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); + } +} \ No newline at end of file diff --git a/Net.Maui.Extensions/Net.Maui.Extensions.csproj b/Net.Maui.Extensions/Net.Maui.Extensions.csproj index 59fa6b7..3fdfefa 100644 --- a/Net.Maui.Extensions/Net.Maui.Extensions.csproj +++ b/Net.Maui.Extensions/Net.Maui.Extensions.csproj @@ -20,7 +20,7 @@ enable enable true - 0.2.5 + 0.2.6 Alexandru Macocian LICENSE true