From de967b9ee8f700b27360bee57ee705ed20a8a548 Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Mon, 20 Dec 2021 11:04:02 -0700 Subject: [PATCH 1/9] testing moving the Android page stuff --- NuGet.config | 3 +- .../src/Core/Controls.Core-net6.csproj | 1 + src/Controls/src/Core/Page.cs | 98 +++++++++++++++++-- .../AlertManager/AlertManager.Android.cs | 28 ++++-- 4 files changed, 110 insertions(+), 20 deletions(-) diff --git a/NuGet.config b/NuGet.config index 7928751c3b3f..5137e705d7f5 100644 --- a/NuGet.config +++ b/NuGet.config @@ -1,4 +1,4 @@ - + @@ -18,6 +18,7 @@ + diff --git a/src/Controls/src/Core/Controls.Core-net6.csproj b/src/Controls/src/Core/Controls.Core-net6.csproj index 75d5d3b9b772..71d0b60e8b56 100644 --- a/src/Controls/src/Core/Controls.Core-net6.csproj +++ b/src/Controls/src/Core/Controls.Core-net6.csproj @@ -22,6 +22,7 @@ + diff --git a/src/Controls/src/Core/Page.cs b/src/Controls/src/Core/Page.cs index e1de047a55d3..2bab8afa443f 100644 --- a/src/Controls/src/Core/Page.cs +++ b/src/Controls/src/Core/Page.cs @@ -9,6 +9,7 @@ using Microsoft.Maui.Controls.Internals; using Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific; using Microsoft.Maui.Graphics; +using CommunityToolkit.Mvvm.Messaging; namespace Microsoft.Maui.Controls { @@ -168,9 +169,15 @@ public Task DisplayActionSheet(string title, string cancel, string destr args.FlowDirection = flowDirection; if (IsPlatformEnabled) - MessagingCenter.Send(this, ActionSheetSignalName, args); + { + WeakReferenceMessenger.Default.Send(new ActionSheetMessage(this, args)); + //MessagingCenter.Send(this, ActionSheetSignalName, args); + } else - _pendingActions.Add(() => MessagingCenter.Send(this, ActionSheetSignalName, args)); + { + _pendingActions.Add(() => WeakReferenceMessenger.Default.Send(new ActionSheetMessage(this, args))); + //_pendingActions.Add(() => MessagingCenter.Send(this, ActionSheetSignalName, args)); + } return args.Result.Task; } @@ -199,9 +206,15 @@ public Task DisplayAlert(string title, string message, string accept, stri args.FlowDirection = flowDirection; if (IsPlatformEnabled) - MessagingCenter.Send(this, AlertSignalName, args); + { + WeakReferenceMessenger.Default.Send(new PageAlertMessage(this, args)); + //MessagingCenter.Send(this, AlertSignalName, args); + } else - _pendingActions.Add(() => MessagingCenter.Send(this, AlertSignalName, args)); + { + _pendingActions.Add(() => WeakReferenceMessenger.Default.Send(new PageAlertMessage(this, args))); + //_pendingActions.Add(() => MessagingCenter.Send(this, AlertSignalName, args)); + } return args.Result.Task; } @@ -211,9 +224,15 @@ public Task DisplayAlert(string title, string message, string accept, stri var args = new PromptArguments(title, message, accept, cancel, placeholder, maxLength, keyboard, initialValue); if (IsPlatformEnabled) - MessagingCenter.Send(this, PromptSignalName, args); + { + WeakReferenceMessenger.Default.Send(new PromptMessage(this, args)); + //MessagingCenter.Send(this, PromptSignalName, args); + } else - _pendingActions.Add(() => MessagingCenter.Send(this, PromptSignalName, args)); + { + _pendingActions.Add(() => WeakReferenceMessenger.Default.Send(new PromptMessage(this, args))); + //_pendingActions.Add(() => MessagingCenter.Send(this, PromptSignalName, args)); + } return args.Result.Task; } @@ -430,9 +449,15 @@ public void SendAppearing() if (IsBusy) { if (IsPlatformEnabled) - MessagingCenter.Send(this, BusySetSignalName, true); + { + WeakReferenceMessenger.Default.Send(new PageBusyMessage(this, true)); + //MessagingCenter.Send(this, BusySetSignalName, true); + } else - _pendingActions.Add(() => MessagingCenter.Send(this, BusySetSignalName, true)); + { + _pendingActions.Add(() => WeakReferenceMessenger.Default.Send(new PageBusyMessage(this, true))); + //_pendingActions.Add(() => MessagingCenter.Send(this, BusySetSignalName, true)); + } } OnAppearing(); @@ -453,7 +478,9 @@ public void SendDisappearing() _hasAppeared = false; if (IsBusy) - MessagingCenter.Send(this, BusySetSignalName, false); + { + WeakReferenceMessenger.Default.Send(new PageBusyMessage(this, false)); + } var pageContainer = this as IPageContainer; pageContainer?.CurrentPage?.SendDisappearing(); @@ -510,7 +537,8 @@ void OnPageBusyChanged() if (!_hasAppeared) return; - MessagingCenter.Send(this, BusySetSignalName, IsBusy); + WeakReferenceMessenger.Default.Send(new PageBusyMessage(this, IsBusy)); + //MessagingCenter.Send(this, BusySetSignalName, IsBusy); } void OnToolbarItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs args) @@ -564,4 +592,54 @@ internal void SetTitleView(View oldTitleView, View newTitleView) _titleView = newTitleView; } } + + + // TODO ezhart These Pages could probably all be IView + public class PageBusyMessage + { + public PageBusyMessage(Page page, bool isBusy) + { + Page = page; + IsBusy = isBusy; + } + + public Page Page { get; set; } + public bool IsBusy { get; set; } + } + + public class PageAlertMessage + { + public PageAlertMessage(Page page, AlertArguments arguments) + { + Page = page; + Arguments = arguments; + } + + public Page Page { get; set; } + public AlertArguments Arguments { get; set; } + } + + public class PromptMessage + { + public PromptMessage(Page page, PromptArguments arguments) + { + Page = page; + Arguments = arguments; + } + + public Page Page { get; set; } + public PromptArguments Arguments { get; set; } + } + + public class ActionSheetMessage + { + public ActionSheetMessage(Page page, ActionSheetArguments arguments) + { + Page = page; + Arguments = arguments; + } + + public Page Page { get; set; } + public ActionSheetArguments Arguments { get; set; } + } } diff --git a/src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs b/src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs index 1b41260ba404..ff1523fd7c87 100644 --- a/src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs +++ b/src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs @@ -7,6 +7,7 @@ using Android.Text; using Android.Views; using Android.Widget; +using CommunityToolkit.Mvvm.Messaging; using Microsoft.Maui.Controls.Internals; using AButton = Android.Widget.Button; using AppCompatActivity = AndroidX.AppCompat.App.AppCompatActivity; @@ -64,10 +65,19 @@ internal AlertRequestHelper(Activity context, IMauiContext mauiContext) Activity = context; MauiContext = mauiContext; - MessagingCenter.Subscribe(Activity, Page.BusySetSignalName, OnPageBusy); - MessagingCenter.Subscribe(Activity, Page.AlertSignalName, OnAlertRequested); - MessagingCenter.Subscribe(Activity, Page.PromptSignalName, OnPromptRequested); - MessagingCenter.Subscribe(Activity, Page.ActionSheetSignalName, OnActionSheetRequested); + // TODO ezhart All of these OnX methods could just take the messages directly, no need for the lambdas to unpack + + WeakReferenceMessenger.Default.Register(Activity, (r, m) => OnPageBusy(m.Page, m.IsBusy)); + //MessagingCenter.Subscribe(Activity, Page.BusySetSignalName, OnPageBusy); + + WeakReferenceMessenger.Default.Register(Activity, (r, m) => OnAlertRequested(m.Page, m.Arguments)); + //MessagingCenter.Subscribe(Activity, Page.AlertSignalName, OnAlertRequested); + + WeakReferenceMessenger.Default.Register(Activity, (r, m) => OnPromptRequested(m.Page, m.Arguments)); + //MessagingCenter.Subscribe(Activity, Page.PromptSignalName, OnPromptRequested); + + WeakReferenceMessenger.Default.Register(Activity, (r, m) => OnActionSheetRequested(m.Page, m.Arguments)); + //MessagingCenter.Subscribe(Activity, Page.ActionSheetSignalName, OnActionSheetRequested); } public Activity Activity { get; } @@ -75,10 +85,10 @@ internal AlertRequestHelper(Activity context, IMauiContext mauiContext) public void Dispose() { - MessagingCenter.Unsubscribe(Activity, Page.BusySetSignalName); - MessagingCenter.Unsubscribe(Activity, Page.AlertSignalName); - MessagingCenter.Unsubscribe(Activity, Page.PromptSignalName); - MessagingCenter.Unsubscribe(Activity, Page.ActionSheetSignalName); + //MessagingCenter.Unsubscribe(Activity, Page.BusySetSignalName); + //MessagingCenter.Unsubscribe(Activity, Page.AlertSignalName); + //MessagingCenter.Unsubscribe(Activity, Page.PromptSignalName); + //MessagingCenter.Unsubscribe(Activity, Page.ActionSheetSignalName); } public void ResetBusyCount() @@ -303,7 +313,7 @@ bool PageIsInThisContext(IView page) { return false; } - + return nativeView.Context.GetActivity()?.Equals(Activity) ?? false; } From 39867d8095a9cf3e9523356634f3e58b59a6b0c6 Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Tue, 28 Dec 2021 17:05:06 -0700 Subject: [PATCH 2/9] Update the Page stuff for all platforms --- .../Core/src/Android/PopupManager.cs | 14 ++--- .../Core/src/Windows/Platform.cs | 26 ++++++--- src/Compatibility/Core/src/iOS/Platform.cs | 26 ++++++--- src/Controls/src/Core/Page.cs | 8 --- .../AlertManager/AlertManager.Android.cs | 55 +++++++++---------- .../AlertManager/AlertManager.Windows.cs | 31 +++++++---- .../Platform/AlertManager/AlertManager.iOS.cs | 29 +++++----- 7 files changed, 102 insertions(+), 87 deletions(-) diff --git a/src/Compatibility/Core/src/Android/PopupManager.cs b/src/Compatibility/Core/src/Android/PopupManager.cs index 499fc479956a..e86a8b6010a1 100644 --- a/src/Compatibility/Core/src/Android/PopupManager.cs +++ b/src/Compatibility/Core/src/Android/PopupManager.cs @@ -6,6 +6,7 @@ using Android.Text; using Android.Views; using Android.Widget; +using CommunityToolkit.Mvvm.Messaging; using Microsoft.Maui.Controls.Internals; using AppCompatActivity = AndroidX.AppCompat.App.AppCompatActivity; using AppCompatAlertDialog = AndroidX.AppCompat.App.AlertDialog; @@ -50,20 +51,17 @@ internal sealed class PopupRequestHelper : IDisposable internal PopupRequestHelper(Activity context) { Activity = context; - MessagingCenter.Subscribe(Activity, Page.BusySetSignalName, OnPageBusy); - MessagingCenter.Subscribe(Activity, Page.AlertSignalName, OnAlertRequested); - MessagingCenter.Subscribe(Activity, Page.PromptSignalName, OnPromptRequested); - MessagingCenter.Subscribe(Activity, Page.ActionSheetSignalName, OnActionSheetRequested); + + WeakReferenceMessenger.Default.Register(Activity, (r, m) => OnPageBusy(m.Page, m.IsBusy)); + WeakReferenceMessenger.Default.Register(Activity, (r, m) => OnAlertRequested(m.Page, m.Arguments)); + WeakReferenceMessenger.Default.Register(Activity, (r, m) => OnPromptRequested(m.Page, m.Arguments)); + WeakReferenceMessenger.Default.Register(Activity, (r, m) => OnActionSheetRequested(m.Page, m.Arguments)); } public Activity Activity { get; } public void Dispose() { - MessagingCenter.Unsubscribe(Activity, Page.BusySetSignalName); - MessagingCenter.Unsubscribe(Activity, Page.AlertSignalName); - MessagingCenter.Unsubscribe(Activity, Page.PromptSignalName); - MessagingCenter.Unsubscribe(Activity, Page.ActionSheetSignalName); } public void ResetBusyCount() diff --git a/src/Compatibility/Core/src/Windows/Platform.cs b/src/Compatibility/Core/src/Windows/Platform.cs index 277f70d339fb..794f0f0bffb3 100644 --- a/src/Compatibility/Core/src/Windows/Platform.cs +++ b/src/Compatibility/Core/src/Windows/Platform.cs @@ -14,6 +14,7 @@ using Microsoft.Maui.Controls.Platform; using WVisibility = Microsoft.UI.Xaml.Visibility; using Microsoft.Extensions.Logging; +using CommunityToolkit.Mvvm.Messaging; namespace Microsoft.Maui.Controls.Compatibility.Platform.UWP { @@ -136,10 +137,10 @@ internal Platform(Microsoft.UI.Xaml.Window page) _container.SizeChanged += OnRendererSizeChanged; - MessagingCenter.Subscribe(this, Page.BusySetSignalName, (Page sender, bool enabled) => + WeakReferenceMessenger.Default.Register(page, (window, message) => { Microsoft.UI.Xaml.Controls.ProgressBar indicator = GetBusyIndicator(); - indicator.Visibility = enabled ? WVisibility.Visible : WVisibility.Collapsed; + indicator.Visibility = message.IsBusy ? WVisibility.Visible : WVisibility.Collapsed; }); _toolbarTracker.CollectionChanged += OnToolbarItemsChanged; @@ -601,13 +602,16 @@ internal IToolbarProvider GetToolbarProvider() internal static void SubscribeAlertsAndActionSheets() { - MessagingCenter.Subscribe(Forms.MainWindow, Page.AlertSignalName, OnPageAlert); - MessagingCenter.Subscribe(Forms.MainWindow, Page.ActionSheetSignalName, OnPageActionSheet); - MessagingCenter.Subscribe(Forms.MainWindow, Page.PromptSignalName, OnPagePrompt); + WeakReferenceMessenger.Default.Register(Forms.MainWindow, OnPageAlert); + WeakReferenceMessenger.Default.Register(Forms.MainWindow, OnPagePrompt); + WeakReferenceMessenger.Default.Register(Forms.MainWindow, OnPageActionSheet); } - static void OnPageActionSheet(Page sender, ActionSheetArguments options) + static void OnPageActionSheet(UI.Xaml.Window window, ActionSheetMessage message) { + var sender = message.Page; + var options = message.Arguments; + bool userDidSelect = false; if (options.FlowDirection == FlowDirection.MatchParent) @@ -654,8 +658,11 @@ static void OnPageActionSheet(Page sender, ActionSheetArguments options) } } - static async void OnPagePrompt(Page sender, PromptArguments options) + static async void OnPagePrompt(UI.Xaml.Window window, PromptMessage message) { + var sender = message.Page; + var options = message.Arguments; + var promptDialog = new PromptDialog { Title = options.Title ?? string.Empty, @@ -693,8 +700,11 @@ static async Task ShowPrompt(PromptDialog prompt) return null; } - static async void OnPageAlert(Page sender, AlertArguments options) + static async void OnPageAlert(UI.Xaml.Window window, PageAlertMessage message) { + var sender = message.Page; + var options = message.Arguments; + string content = options.Message ?? string.Empty; string title = options.Title ?? string.Empty; diff --git a/src/Compatibility/Core/src/iOS/Platform.cs b/src/Compatibility/Core/src/iOS/Platform.cs index 486ec1b4d83b..971f0636348d 100644 --- a/src/Compatibility/Core/src/iOS/Platform.cs +++ b/src/Compatibility/Core/src/iOS/Platform.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using CommunityToolkit.Mvvm.Messaging; using CoreGraphics; using Foundation; using Microsoft.Extensions.Logging; @@ -655,30 +656,43 @@ internal static string ResolveMsAppDataUri(Uri uri) internal void SubscribeToAlertsAndActionSheets() { var busyCount = 0; - MessagingCenter.Subscribe(this, Page.BusySetSignalName, (Page sender, bool enabled) => + + WeakReferenceMessenger.Default.Register(this, (platform, message) => { + var sender = message.Page; + var enabled = message.IsBusy; + if (!PageIsChildOfPlatform(sender)) return; busyCount = Math.Max(0, enabled ? busyCount + 1 : busyCount - 1); UIApplication.SharedApplication.NetworkActivityIndicatorVisible = busyCount > 0; }); - MessagingCenter.Subscribe(this, Page.AlertSignalName, (Page sender, AlertArguments arguments) => + WeakReferenceMessenger.Default.Register(this, (platform, message) => { + var sender = message.Page; + var arguments = message.Arguments; + if (!PageIsChildOfPlatform(sender)) return; PresentAlert(arguments); }); - MessagingCenter.Subscribe(this, Page.PromptSignalName, (Page sender, PromptArguments arguments) => + WeakReferenceMessenger.Default.Register(this, (platform, message) => { + var sender = message.Page; + var arguments = message.Arguments; + if (!PageIsChildOfPlatform(sender)) return; PresentPrompt(arguments); }); - MessagingCenter.Subscribe(this, Page.ActionSheetSignalName, (Page sender, ActionSheetArguments arguments) => + WeakReferenceMessenger.Default.Register(this, (platform, message) => { + var sender = message.Page; + var arguments = message.Arguments; + if (!PageIsChildOfPlatform(sender)) return; @@ -695,10 +709,6 @@ static bool IsModalPresentedFullScreen(Page modal) internal void UnsubscribeFromAlertsAndActionsSheets() { - MessagingCenter.Unsubscribe(this, Page.ActionSheetSignalName); - MessagingCenter.Unsubscribe(this, Page.AlertSignalName); - MessagingCenter.Unsubscribe(this, Page.PromptSignalName); - MessagingCenter.Unsubscribe(this, Page.BusySetSignalName); } internal void MarkForRemoval() diff --git a/src/Controls/src/Core/Page.cs b/src/Controls/src/Core/Page.cs index 2bab8afa443f..c19c2caef111 100644 --- a/src/Controls/src/Core/Page.cs +++ b/src/Controls/src/Core/Page.cs @@ -15,14 +15,6 @@ namespace Microsoft.Maui.Controls { public partial class Page : VisualElement, ILayout, IPageController, IElementConfiguration, IPaddingElement, ISafeAreaView { - public const string BusySetSignalName = "Microsoft.Maui.Controls.BusySet"; - - public const string AlertSignalName = "Microsoft.Maui.Controls.SendAlert"; - - public const string PromptSignalName = "Microsoft.Maui.Controls.SendPrompt"; - - public const string ActionSheetSignalName = "Microsoft.Maui.Controls.ShowActionSheet"; - internal static readonly BindableProperty IgnoresContainerAreaProperty = BindableProperty.Create("IgnoresContainerArea", typeof(bool), typeof(Page), false); public static readonly BindableProperty BackgroundImageSourceProperty = BindableProperty.Create(nameof(BackgroundImageSource), typeof(ImageSource), typeof(Page), default(ImageSource)); diff --git a/src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs b/src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs index ff1523fd7c87..6ae1b1ef9823 100644 --- a/src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs +++ b/src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs @@ -65,19 +65,10 @@ internal AlertRequestHelper(Activity context, IMauiContext mauiContext) Activity = context; MauiContext = mauiContext; - // TODO ezhart All of these OnX methods could just take the messages directly, no need for the lambdas to unpack - - WeakReferenceMessenger.Default.Register(Activity, (r, m) => OnPageBusy(m.Page, m.IsBusy)); - //MessagingCenter.Subscribe(Activity, Page.BusySetSignalName, OnPageBusy); - - WeakReferenceMessenger.Default.Register(Activity, (r, m) => OnAlertRequested(m.Page, m.Arguments)); - //MessagingCenter.Subscribe(Activity, Page.AlertSignalName, OnAlertRequested); - - WeakReferenceMessenger.Default.Register(Activity, (r, m) => OnPromptRequested(m.Page, m.Arguments)); - //MessagingCenter.Subscribe(Activity, Page.PromptSignalName, OnPromptRequested); - - WeakReferenceMessenger.Default.Register(Activity, (r, m) => OnActionSheetRequested(m.Page, m.Arguments)); - //MessagingCenter.Subscribe(Activity, Page.ActionSheetSignalName, OnActionSheetRequested); + WeakReferenceMessenger.Default.Register(Activity, OnPageBusy); + WeakReferenceMessenger.Default.Register(Activity, OnAlertRequested); + WeakReferenceMessenger.Default.Register(Activity, OnPromptRequested); + WeakReferenceMessenger.Default.Register(Activity, OnActionSheetRequested); } public Activity Activity { get; } @@ -85,10 +76,6 @@ internal AlertRequestHelper(Activity context, IMauiContext mauiContext) public void Dispose() { - //MessagingCenter.Unsubscribe(Activity, Page.BusySetSignalName); - //MessagingCenter.Unsubscribe(Activity, Page.AlertSignalName); - //MessagingCenter.Unsubscribe(Activity, Page.PromptSignalName); - //MessagingCenter.Unsubscribe(Activity, Page.ActionSheetSignalName); } public void ResetBusyCount() @@ -96,8 +83,11 @@ public void ResetBusyCount() _busyCount = 0; } - void OnPageBusy(IView sender, bool enabled) + void OnPageBusy(Activity activity, PageBusyMessage message) { + var sender = message.Page; + var enabled = message.IsBusy; + // Verify that the page making the request is part of this activity if (!PageIsInThisContext(sender)) { @@ -109,15 +99,18 @@ void OnPageBusy(IView sender, bool enabled) UpdateProgressBarVisibility(_busyCount > 0); } - void OnActionSheetRequested(IView sender, ActionSheetArguments arguments) + void OnActionSheetRequested(Activity activity, ActionSheetMessage message) { + var sender = message.Page; + var arguments = message.Arguments; + // Verify that the page making the request is part of this activity if (!PageIsInThisContext(sender)) { return; } - var builder = new DialogBuilder(Activity); + var builder = new DialogBuilder(activity); builder.SetTitle(arguments.Title); string[] items = arguments.Buttons.ToArray(); @@ -168,8 +161,11 @@ void OnActionSheetRequested(IView sender, ActionSheetArguments arguments) } } - void OnAlertRequested(IView sender, AlertArguments arguments) + void OnAlertRequested(Activity activity, PageAlertMessage message) { + var sender = message.Page; + var arguments = message.Arguments; + // Verify that the page making the request is part of this activity if (!PageIsInThisContext(sender)) { @@ -177,7 +173,7 @@ void OnAlertRequested(IView sender, AlertArguments arguments) } int messageID = 16908299; - var alert = new DialogBuilder(Activity).Create(); + var alert = new DialogBuilder(activity).Create(); if (alert == null) return; @@ -232,15 +228,18 @@ TextDirection GetTextDirection(IView sender, FlowDirection flowDirection) return TextDirection.Ltr; } - void OnPromptRequested(IView sender, PromptArguments arguments) + void OnPromptRequested(Activity activity, PromptMessage message) { + var sender = message.Page; + var arguments = message.Arguments; + // Verify that the page making the request is part of this activity if (!PageIsInThisContext(sender)) { return; } - var alertDialog = new DialogBuilder(Activity).Create(); + var alertDialog = new DialogBuilder(activity).Create(); if (alertDialog == null) return; @@ -248,12 +247,12 @@ void OnPromptRequested(IView sender, PromptArguments arguments) alertDialog.SetTitle(arguments.Title); alertDialog.SetMessage(arguments.Message); - var frameLayout = new FrameLayout(Activity); - var editText = new EditText(Activity) { Hint = arguments.Placeholder, Text = arguments.InitialValue }; + var frameLayout = new FrameLayout(activity); + var editText = new EditText(activity) { Hint = arguments.Placeholder, Text = arguments.InitialValue }; var layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent) { - LeftMargin = (int)(22 * Activity.Resources.DisplayMetrics.Density), - RightMargin = (int)(22 * Activity.Resources.DisplayMetrics.Density) + LeftMargin = (int)(22 * activity.Resources.DisplayMetrics.Density), + RightMargin = (int)(22 * activity.Resources.DisplayMetrics.Density) }; editText.LayoutParameters = layoutParams; diff --git a/src/Controls/src/Core/Platform/AlertManager/AlertManager.Windows.cs b/src/Controls/src/Core/Platform/AlertManager/AlertManager.Windows.cs index dfb01a40d136..bb48f55e71af 100644 --- a/src/Controls/src/Core/Platform/AlertManager/AlertManager.Windows.cs +++ b/src/Controls/src/Core/Platform/AlertManager/AlertManager.Windows.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using CommunityToolkit.Mvvm.Messaging; using Microsoft.Maui.Controls.Internals; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; @@ -46,10 +47,10 @@ internal AlertRequestHelper(UI.Xaml.Window window, IMauiContext mauiContext) Window = window; MauiContext = mauiContext; - MessagingCenter.Subscribe(Window, Page.BusySetSignalName, OnPageBusy); - MessagingCenter.Subscribe(Window, Page.AlertSignalName, OnAlertRequested); - MessagingCenter.Subscribe(Window, Page.PromptSignalName, OnPromptRequested); - MessagingCenter.Subscribe(Window, Page.ActionSheetSignalName, OnActionSheetRequested); + WeakReferenceMessenger.Default.Register(Window, OnPageBusy); + WeakReferenceMessenger.Default.Register(Window, OnAlertRequested); + WeakReferenceMessenger.Default.Register(Window, OnPromptRequested); + WeakReferenceMessenger.Default.Register(Window, OnActionSheetRequested); } public UI.Xaml.Window Window { get; } @@ -57,19 +58,20 @@ internal AlertRequestHelper(UI.Xaml.Window window, IMauiContext mauiContext) public void Dispose() { - MessagingCenter.Unsubscribe(Window, Page.BusySetSignalName); - MessagingCenter.Unsubscribe(Window, Page.AlertSignalName); - MessagingCenter.Unsubscribe(Window, Page.PromptSignalName); - MessagingCenter.Unsubscribe(Window, Page.ActionSheetSignalName); } - void OnPageBusy(Page sender, bool enabled) + void OnPageBusy(UI.Xaml.Window window, PageBusyMessage message) { + var sender = message.Page; + var busy = message.IsBusy; + // TODO: Wrap the pages in a Canvas, and dynamically add a ProgressBar } - async void OnAlertRequested(Page sender, AlertArguments arguments) + async void OnAlertRequested(UI.Xaml.Window window, PageAlertMessage message) { + var arguments = message.Arguments; + string content = arguments.Message ?? string.Empty; string title = arguments.Title ?? string.Empty; @@ -113,8 +115,10 @@ async void OnAlertRequested(Page sender, AlertArguments arguments) CurrentAlert = null; } - async void OnPromptRequested(Page sender, PromptArguments arguments) + async void OnPromptRequested(UI.Xaml.Window window, PromptMessage message) { + var arguments = message.Arguments; + var promptDialog = new PromptDialog { Title = arguments.Title ?? string.Empty, @@ -147,8 +151,11 @@ async void OnPromptRequested(Page sender, PromptArguments arguments) CurrentPrompt = null; } - void OnActionSheetRequested(Page sender, ActionSheetArguments arguments) + void OnActionSheetRequested(UI.Xaml.Window window, ActionSheetMessage message) { + var sender = message.Page; + var arguments = message.Arguments; + bool userDidSelect = false; if (arguments.FlowDirection == FlowDirection.MatchParent) diff --git a/src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs b/src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs index eb259412057e..70f02508383d 100644 --- a/src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs +++ b/src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using CommunityToolkit.Mvvm.Messaging; using Foundation; using Microsoft.Extensions.DependencyInjection; using Microsoft.Maui.Controls.Internals; @@ -49,41 +50,39 @@ internal AlertRequestHelper(UIWindow window) { Window = window; - MessagingCenter.Subscribe(Window, Page.BusySetSignalName, OnPageBusy); - MessagingCenter.Subscribe(Window, Page.AlertSignalName, OnAlertRequested); - MessagingCenter.Subscribe(Window, Page.PromptSignalName, OnPromptRequested); - MessagingCenter.Subscribe(Window, Page.ActionSheetSignalName, OnActionSheetRequested); + WeakReferenceMessenger.Default.Register(Window, OnPageBusy); + WeakReferenceMessenger.Default.Register(Window, OnAlertRequested); + WeakReferenceMessenger.Default.Register(Window, OnPromptRequested); + WeakReferenceMessenger.Default.Register(Window, OnActionSheetRequested); } public UIWindow Window { get; } public void Dispose() { - MessagingCenter.Unsubscribe(Window, Page.BusySetSignalName); - MessagingCenter.Unsubscribe(Window, Page.AlertSignalName); - MessagingCenter.Unsubscribe(Window, Page.PromptSignalName); - MessagingCenter.Unsubscribe(Window, Page.ActionSheetSignalName); } - void OnPageBusy(IView sender, bool enabled) + void OnPageBusy(UIWindow window, PageBusyMessage message) { + var enabled = message.IsBusy; + _busyCount = Math.Max(0, enabled ? _busyCount + 1 : _busyCount - 1); UIApplication.SharedApplication.NetworkActivityIndicatorVisible = _busyCount > 0; } - void OnAlertRequested(IView sender, AlertArguments arguments) + void OnAlertRequested(UIWindow window, PageAlertMessage message) { - PresentAlert(arguments); + PresentAlert(message.Arguments); } - void OnPromptRequested(IView sender, PromptArguments arguments) + void OnPromptRequested(UIWindow window, PromptMessage message) { - PresentPrompt(arguments); + PresentPrompt(message.Arguments); } - void OnActionSheetRequested(IView sender, ActionSheetArguments arguments) + void OnActionSheetRequested(UIWindow window, ActionSheetMessage message) { - PresentActionSheet(arguments); + PresentActionSheet(message.Arguments); } void PresentAlert(AlertArguments arguments) From 6cb67bbbebd4e058efa6250af9f8edc5f8e17284 Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Wed, 29 Dec 2021 16:02:51 -0700 Subject: [PATCH 3/9] Replace the rest of the MC stuff --- .../Core/src/Android/AppCompat/Platform.cs | 5 +-- .../src/Android/Renderers/ListViewAdapter.cs | 5 ++- .../src/iOS/Renderers/NavigationRenderer.cs | 8 ++-- .../iOS/Renderers/TabletFlyoutPageRenderer.cs | 8 ++-- .../Maps/src/Android/MapRenderer.cs | 11 +++--- src/Compatibility/Maps/src/iOS/MapRenderer.cs | 10 ++--- src/Controls/Maps/src/Map.cs | 4 +- .../src/Core/HandlerImpl/Page.Impl.cs | 2 + src/Controls/src/Core/Page.cs | 11 ------ .../AlertManager/AlertManager.Android.cs | 1 + .../AlertManager/AlertManager.Windows.cs | 1 + .../Platform/AlertManager/AlertManager.iOS.cs | 1 + .../ModalNavigationManager.Android.cs | 3 +- src/Controls/src/Core/RadioButton.cs | 39 +++++++++---------- src/Controls/src/Core/RadioButtonGroup.cs | 7 +--- .../src/Core/RadioButtonGroupController.cs | 23 +++++------ .../Core/RadioButtonGroupSelectionChanged.cs | 14 ++++++- src/Controls/tests/Core.UnitTests/MapTests.cs | 8 ++-- .../tests/Core.UnitTests/PageTests.cs | 22 ++++++----- 19 files changed, 97 insertions(+), 86 deletions(-) diff --git a/src/Compatibility/Core/src/Android/AppCompat/Platform.cs b/src/Compatibility/Core/src/Android/AppCompat/Platform.cs index c9cdc3f0aa2f..e7366697c58d 100644 --- a/src/Compatibility/Core/src/Android/AppCompat/Platform.cs +++ b/src/Compatibility/Core/src/Android/AppCompat/Platform.cs @@ -7,6 +7,7 @@ using Android.OS; using Android.Views; using Android.Views.Animations; +using CommunityToolkit.Mvvm.Messaging; using Microsoft.Maui.Controls.Internals; using Microsoft.Maui.Controls.Platform; using Microsoft.Maui.Graphics; @@ -27,8 +28,6 @@ public class Platform : BindableObject, IPlatformLayout, INavigation internal static string PackageName { get; private set; } internal static string GetPackageName() => PackageName; - internal const string CloseContextActionsSignalName = "Xamarin.CloseContextActions"; - internal static readonly BindableProperty RendererProperty = BindableProperty.CreateAttached("Renderer", typeof(IVisualElementRenderer), typeof(Platform), default(IVisualElementRenderer), propertyChanged: (bindable, oldvalue, newvalue) => { @@ -81,7 +80,7 @@ internal bool NavAnimationInProgress return; _navAnimationInProgress = value; if (value) - MessagingCenter.Send(this, CloseContextActionsSignalName); + WeakReferenceMessenger.Default.Send(new CloseContextActionsMessage()); } } diff --git a/src/Compatibility/Core/src/Android/Renderers/ListViewAdapter.cs b/src/Compatibility/Core/src/Android/Renderers/ListViewAdapter.cs index 4e317dbf05d6..9f500f98dfe1 100644 --- a/src/Compatibility/Core/src/Android/Renderers/ListViewAdapter.cs +++ b/src/Compatibility/Core/src/Android/Renderers/ListViewAdapter.cs @@ -7,6 +7,7 @@ using Android.Util; using Android.Views; using Android.Widget; +using CommunityToolkit.Mvvm.Messaging; using Microsoft.Maui.Controls.Internals; using Microsoft.Maui.Graphics; using AListView = Android.Widget.ListView; @@ -64,7 +65,7 @@ public ListViewAdapter(Context context, AListView realListView, ListView listVie realListView.OnItemClickListener = this; realListView.OnItemLongClickListener = this; - MessagingCenter.Subscribe(this, Platform.CloseContextActionsSignalName, lva => CloseContextActions()); + WeakReferenceMessenger.Default.Register(this, (recipient, msg) => CloseContextActions()); InvalidateCount(); } @@ -436,7 +437,7 @@ protected override void Dispose(bool disposing) { CloseContextActions(); - MessagingCenter.Unsubscribe(this, Platform.CloseContextActionsSignalName); + WeakReferenceMessenger.Default.Unregister(this); _realListView.OnItemClickListener = null; _realListView.OnItemLongClickListener = null; diff --git a/src/Compatibility/Core/src/iOS/Renderers/NavigationRenderer.cs b/src/Compatibility/Core/src/iOS/Renderers/NavigationRenderer.cs index 8f767d141c84..3060bcbbb774 100644 --- a/src/Compatibility/Core/src/iOS/Renderers/NavigationRenderer.cs +++ b/src/Compatibility/Core/src/iOS/Renderers/NavigationRenderer.cs @@ -17,12 +17,12 @@ using PointF = CoreGraphics.CGPoint; using RectangleF = CoreGraphics.CGRect; using SizeF = CoreGraphics.CGSize; +using CommunityToolkit.Mvvm.Messaging; namespace Microsoft.Maui.Controls.Compatibility.Platform.iOS { public class NavigationRenderer : UINavigationController, IVisualElementRenderer, IEffectControlProvider { - internal const string UpdateToolbarButtons = "Xamarin.UpdateToolbarButtons"; bool _appeared; bool _ignorePopCall; bool _loaded; @@ -40,7 +40,7 @@ public class NavigationRenderer : UINavigationController, IVisualElementRenderer [Preserve(Conditional = true)] public NavigationRenderer() : base(typeof(FormsNavigationBar), null) { - MessagingCenter.Subscribe(this, UpdateToolbarButtons, sender => + WeakReferenceMessenger.Default.Register(this, (receiver, message) => { if (!ViewControllers.Any()) return; @@ -262,7 +262,7 @@ protected override void Dispose(bool disposing) if (disposing) { - MessagingCenter.Unsubscribe(this, UpdateToolbarButtons); + WeakReferenceMessenger.Default.Unregister(this); foreach (var childViewController in ViewControllers) childViewController.Dispose(); @@ -1711,4 +1711,6 @@ protected override void Dispose(bool disposing) } } } + + internal class UpdateToolBarButtonsMessage { } } diff --git a/src/Compatibility/Core/src/iOS/Renderers/TabletFlyoutPageRenderer.cs b/src/Compatibility/Core/src/iOS/Renderers/TabletFlyoutPageRenderer.cs index 10e3cd8bffe3..c33b6580ac48 100644 --- a/src/Compatibility/Core/src/iOS/Renderers/TabletFlyoutPageRenderer.cs +++ b/src/Compatibility/Core/src/iOS/Renderers/TabletFlyoutPageRenderer.cs @@ -6,6 +6,7 @@ using Microsoft.Maui.Graphics; using ObjCRuntime; using UIKit; +using CommunityToolkit.Mvvm.Messaging; namespace Microsoft.Maui.Controls.Compatibility.Platform.iOS { @@ -391,7 +392,8 @@ public override void WillRotate(UIInterfaceOrientation toInterfaceOrientation, d } FlyoutPage.UpdateFlyoutLayoutBehavior(); - MessagingCenter.Send(this, NavigationRenderer.UpdateToolbarButtons); + + WeakReferenceMessenger.Default.Send(new UpdateToolBarButtonsMessage()); } base.WillRotate(toInterfaceOrientation, duration); @@ -449,7 +451,7 @@ void ClearControllers() void HandleFlyoutPropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == Page.IconImageSourceProperty.PropertyName || e.PropertyName == Page.TitleProperty.PropertyName) - MessagingCenter.Send(this, NavigationRenderer.UpdateToolbarButtons); + WeakReferenceMessenger.Default.Send(new UpdateToolBarButtonsMessage()); } void HandlePropertyChanged(object sender, PropertyChangedEventArgs e) @@ -470,7 +472,7 @@ void HandlePropertyChanged(object sender, PropertyChangedEventArgs e) else if (e.Is(Microsoft.Maui.Controls.FlyoutPage.FlyoutLayoutBehaviorProperty)) UpdateFlyoutLayoutBehavior(base.View.Bounds.Size); - MessagingCenter.Send(this, NavigationRenderer.UpdateToolbarButtons); + WeakReferenceMessenger.Default.Send(new UpdateToolBarButtonsMessage()); } public override void ViewWillTransitionToSize(CGSize toSize, IUIViewControllerTransitionCoordinator coordinator) diff --git a/src/Compatibility/Maps/src/Android/MapRenderer.cs b/src/Compatibility/Maps/src/Android/MapRenderer.cs index c4429269f684..139a3b665ced 100644 --- a/src/Compatibility/Maps/src/Android/MapRenderer.cs +++ b/src/Compatibility/Maps/src/Android/MapRenderer.cs @@ -27,13 +27,12 @@ using Math = System.Math; using Polygon = Microsoft.Maui.Controls.Maps.Polygon; using Polyline = Microsoft.Maui.Controls.Maps.Polyline; +using CommunityToolkit.Mvvm.Messaging; namespace Microsoft.Maui.Controls.Compatibility.Maps.Android { public class MapRenderer : ViewRenderer, GoogleMap.IOnCameraMoveListener, IOnMapReadyCallback { - const string MoveMessageName = "MapMoveToRegion"; - static Bundle s_bundle; bool _disposed; @@ -82,8 +81,8 @@ protected override void Dispose(bool disposing) { if (Element != null) { - MessagingCenter.Unsubscribe(this, MoveMessageName); - + WeakReferenceMessenger.Default.Unregister(this); + ((ObservableCollection)Element.Pins).CollectionChanged -= OnPinCollectionChanged; foreach (Pin pin in Element.Pins) { @@ -142,7 +141,7 @@ protected override void OnElementChanged(ElementChangedEventArgs e) child.PropertyChanged -= MapElementPropertyChanged; } - MessagingCenter.Unsubscribe(this, MoveMessageName); + WeakReferenceMessenger.Default.Unregister(this); if (NativeMap != null) { @@ -158,7 +157,7 @@ protected override void OnElementChanged(ElementChangedEventArgs e) Control.GetMapAsync(this); - MessagingCenter.Subscribe(this, MoveMessageName, OnMoveToRegionMessage, Map); + WeakReferenceMessenger.Default.Register(this, (renderer, args) => OnMoveToRegionMessage(renderer.Element, args)); ((INotifyCollectionChanged)Map.Pins).CollectionChanged += OnPinCollectionChanged; ((INotifyCollectionChanged)Map.MapElements).CollectionChanged += OnMapElementCollectionChanged; diff --git a/src/Compatibility/Maps/src/iOS/MapRenderer.cs b/src/Compatibility/Maps/src/iOS/MapRenderer.cs index fc0d8d527145..e2c9d4f63517 100644 --- a/src/Compatibility/Maps/src/iOS/MapRenderer.cs +++ b/src/Compatibility/Maps/src/iOS/MapRenderer.cs @@ -11,6 +11,7 @@ using Microsoft.Maui.Controls.Maps; using Microsoft.Maui.Graphics; using Microsoft.Maui.Controls.Platform; +using CommunityToolkit.Mvvm.Messaging; #if __MOBILE__ using UIKit; @@ -34,8 +35,6 @@ public class MapRenderer : ViewRenderer UITapGestureRecognizer _mapClickedGestureRecognizer; #endif - const string MoveMessageName = "MapMoveToRegion"; - public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint) { return Control.GetSizeRequest(widthConstraint, heightConstraint); @@ -64,7 +63,7 @@ protected override void Dispose(bool disposing) if (Element != null) { var mapModel = (Map)Element; - MessagingCenter.Unsubscribe(this, MoveMessageName); + WeakReferenceMessenger.Default.Unregister(this); ((ObservableCollection)mapModel.Pins).CollectionChanged -= OnPinCollectionChanged; ((ObservableCollection)mapModel.MapElements).CollectionChanged -= OnMapElementCollectionChanged; foreach (Pin pin in mapModel.Pins) @@ -118,7 +117,7 @@ protected override void OnElementChanged(ElementChangedEventArgs e) { var mapModel = (Map)e.OldElement; - MessagingCenter.Unsubscribe(this, MoveMessageName); + WeakReferenceMessenger.Default.Unregister(this); ((ObservableCollection)mapModel.Pins).CollectionChanged -= OnPinCollectionChanged; foreach (Pin pin in mapModel.Pins) @@ -165,7 +164,8 @@ protected override void OnElementChanged(ElementChangedEventArgs e) #endif } - MessagingCenter.Subscribe(this, MoveMessageName, (s, a) => MoveToRegion(a), mapModel); + WeakReferenceMessenger.Default.Register(this, (renderer, args) => MoveToRegion(args)); + if (mapModel.LastMoveToRegion != null) MoveToRegion(mapModel.LastMoveToRegion, false); diff --git a/src/Controls/Maps/src/Map.cs b/src/Controls/Maps/src/Map.cs index f5d0065828fc..f4a73dd0f55c 100644 --- a/src/Controls/Maps/src/Map.cs +++ b/src/Controls/Maps/src/Map.cs @@ -5,6 +5,7 @@ using System.Collections.Specialized; using System.ComponentModel; using System.Linq; +using CommunityToolkit.Mvvm.Messaging; using Microsoft.Maui.Controls.Internals; namespace Microsoft.Maui.Controls.Maps @@ -153,7 +154,8 @@ public void MoveToRegion(MapSpan mapSpan) if (mapSpan == null) throw new ArgumentNullException(nameof(mapSpan)); LastMoveToRegion = mapSpan; - MessagingCenter.Send(this, "MapMoveToRegion", mapSpan); + + WeakReferenceMessenger.Default.Send(mapSpan); } void PinsOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) diff --git a/src/Controls/src/Core/HandlerImpl/Page.Impl.cs b/src/Controls/src/Core/HandlerImpl/Page.Impl.cs index bfaba777c8ad..a11b3d36e862 100644 --- a/src/Controls/src/Core/HandlerImpl/Page.Impl.cs +++ b/src/Controls/src/Core/HandlerImpl/Page.Impl.cs @@ -79,4 +79,6 @@ internal NavigatedFromEventArgs(Page destinationPage) internal Page DestinationPage { get; } } + + public class CloseContextActionsMessage { } } diff --git a/src/Controls/src/Core/Page.cs b/src/Controls/src/Core/Page.cs index c19c2caef111..4dee86fff3f3 100644 --- a/src/Controls/src/Core/Page.cs +++ b/src/Controls/src/Core/Page.cs @@ -4,7 +4,6 @@ using System.Collections.Specialized; using System.ComponentModel; using System.Linq; -using System.Threading; using System.Threading.Tasks; using Microsoft.Maui.Controls.Internals; using Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific; @@ -163,12 +162,10 @@ public Task DisplayActionSheet(string title, string cancel, string destr if (IsPlatformEnabled) { WeakReferenceMessenger.Default.Send(new ActionSheetMessage(this, args)); - //MessagingCenter.Send(this, ActionSheetSignalName, args); } else { _pendingActions.Add(() => WeakReferenceMessenger.Default.Send(new ActionSheetMessage(this, args))); - //_pendingActions.Add(() => MessagingCenter.Send(this, ActionSheetSignalName, args)); } return args.Result.Task; @@ -200,12 +197,10 @@ public Task DisplayAlert(string title, string message, string accept, stri if (IsPlatformEnabled) { WeakReferenceMessenger.Default.Send(new PageAlertMessage(this, args)); - //MessagingCenter.Send(this, AlertSignalName, args); } else { _pendingActions.Add(() => WeakReferenceMessenger.Default.Send(new PageAlertMessage(this, args))); - //_pendingActions.Add(() => MessagingCenter.Send(this, AlertSignalName, args)); } return args.Result.Task; @@ -218,12 +213,10 @@ public Task DisplayAlert(string title, string message, string accept, stri if (IsPlatformEnabled) { WeakReferenceMessenger.Default.Send(new PromptMessage(this, args)); - //MessagingCenter.Send(this, PromptSignalName, args); } else { _pendingActions.Add(() => WeakReferenceMessenger.Default.Send(new PromptMessage(this, args))); - //_pendingActions.Add(() => MessagingCenter.Send(this, PromptSignalName, args)); } return args.Result.Task; @@ -443,12 +436,10 @@ public void SendAppearing() if (IsPlatformEnabled) { WeakReferenceMessenger.Default.Send(new PageBusyMessage(this, true)); - //MessagingCenter.Send(this, BusySetSignalName, true); } else { _pendingActions.Add(() => WeakReferenceMessenger.Default.Send(new PageBusyMessage(this, true))); - //_pendingActions.Add(() => MessagingCenter.Send(this, BusySetSignalName, true)); } } @@ -530,7 +521,6 @@ void OnPageBusyChanged() return; WeakReferenceMessenger.Default.Send(new PageBusyMessage(this, IsBusy)); - //MessagingCenter.Send(this, BusySetSignalName, IsBusy); } void OnToolbarItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs args) @@ -585,7 +575,6 @@ internal void SetTitleView(View oldTitleView, View newTitleView) } } - // TODO ezhart These Pages could probably all be IView public class PageBusyMessage { diff --git a/src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs b/src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs index 6ae1b1ef9823..9ef277c45618 100644 --- a/src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs +++ b/src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs @@ -76,6 +76,7 @@ internal AlertRequestHelper(Activity context, IMauiContext mauiContext) public void Dispose() { + WeakReferenceMessenger.Default.UnregisterAll(Activity); } public void ResetBusyCount() diff --git a/src/Controls/src/Core/Platform/AlertManager/AlertManager.Windows.cs b/src/Controls/src/Core/Platform/AlertManager/AlertManager.Windows.cs index bb48f55e71af..14ddb237fbe0 100644 --- a/src/Controls/src/Core/Platform/AlertManager/AlertManager.Windows.cs +++ b/src/Controls/src/Core/Platform/AlertManager/AlertManager.Windows.cs @@ -58,6 +58,7 @@ internal AlertRequestHelper(UI.Xaml.Window window, IMauiContext mauiContext) public void Dispose() { + WeakReferenceMessenger.Default.UnregisterAll(Window); } void OnPageBusy(UI.Xaml.Window window, PageBusyMessage message) diff --git a/src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs b/src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs index 70f02508383d..a15415d542f0 100644 --- a/src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs +++ b/src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs @@ -60,6 +60,7 @@ internal AlertRequestHelper(UIWindow window) public void Dispose() { + WeakReferenceMessenger.Default.UnregisterAll(Window); } void OnPageBusy(UIWindow window, PageBusyMessage message) diff --git a/src/Controls/src/Core/Platform/ModalNavigationManager/ModalNavigationManager.Android.cs b/src/Controls/src/Core/Platform/ModalNavigationManager/ModalNavigationManager.Android.cs index a1dd194bc46c..760c25b8089b 100644 --- a/src/Controls/src/Core/Platform/ModalNavigationManager/ModalNavigationManager.Android.cs +++ b/src/Controls/src/Core/Platform/ModalNavigationManager/ModalNavigationManager.Android.cs @@ -11,6 +11,7 @@ using AndroidX.Fragment.App; using Microsoft.Maui.Graphics; using AView = Android.Views.View; +using CommunityToolkit.Mvvm.Messaging; namespace Microsoft.Maui.Controls.Platform { @@ -34,7 +35,7 @@ internal bool NavAnimationInProgress return; _navAnimationInProgress = value; if (value) - MessagingCenter.Send(this, CloseContextActionsSignalName); + WeakReferenceMessenger.Default.Send(new CloseContextActionsMessage()); } } diff --git a/src/Controls/src/Core/RadioButton.cs b/src/Controls/src/Core/RadioButton.cs index f7c85f648c5c..930779dbaab6 100644 --- a/src/Controls/src/Core/RadioButton.cs +++ b/src/Controls/src/Core/RadioButton.cs @@ -1,4 +1,5 @@ using System; +using CommunityToolkit.Mvvm.Messaging; using Microsoft.Extensions.Logging; using Microsoft.Maui.Controls.Internals; using Microsoft.Maui.Controls.Shapes; @@ -15,9 +16,6 @@ public partial class RadioButton : TemplatedView, IElementConfiguration(this, - RadioButtonGroup.GroupSelectionChangedMessage, HandleRadioButtonGroupSelectionChanged); - MessagingCenter.Subscribe, RadioButtonGroupValueChanged>(this, - RadioButtonGroup.GroupValueChangedMessage, HandleRadioButtonGroupValueChanged); + WeakReferenceMessenger.Default.Register(this, HandleRadioButtonGroupSelectionChanged); + WeakReferenceMessenger.Default.Register(this, HandleRadioButtonGroupValueChanged); } - MessagingCenter.Send(this, GroupNameChangedMessage, - new RadioButtonGroupNameChanged(RadioButtonGroup.GetVisualRoot(this), oldGroupName)); + WeakReferenceMessenger.Default.Send(new RadioButtonGroupNameChanged(RadioButtonGroup.GetVisualRoot(this), oldGroupName)); } else { if (!string.IsNullOrEmpty(oldGroupName)) { - MessagingCenter.Unsubscribe(this, RadioButtonGroup.GroupSelectionChangedMessage); - MessagingCenter.Unsubscribe, RadioButtonGroupValueChanged>(this, RadioButtonGroup.GroupValueChangedMessage); + WeakReferenceMessenger.Default.Unregister(this); + WeakReferenceMessenger.Default.Unregister(this); } } } - bool MatchesScope(RadioButtonScopeMessage message) + static bool MatchesScope(RadioButtonScopeMessage message, RadioButton radioButton) { - return RadioButtonGroup.GetVisualRoot(this) == message.Scope; + return RadioButtonGroup.GetVisualRoot(radioButton) == message.Scope; } - void HandleRadioButtonGroupSelectionChanged(RadioButton selected, RadioButtonGroupSelectionChanged args) + static void HandleRadioButtonGroupSelectionChanged(RadioButton receiver, RadioButtonGroupSelectionChanged args) { - if (!IsChecked || selected == this || string.IsNullOrEmpty(GroupName) || GroupName != selected.GroupName || !MatchesScope(args)) + var selected = args.RadioButton; + + if (!receiver.IsChecked || selected == receiver || string.IsNullOrEmpty(receiver.GroupName) || receiver.GroupName != selected.GroupName || !MatchesScope(args, receiver)) { return; } - IsChecked = false; + receiver.IsChecked = false; } - void HandleRadioButtonGroupValueChanged(Compatibility.Layout layout, RadioButtonGroupValueChanged args) + static void HandleRadioButtonGroupValueChanged(RadioButton radioButton, RadioButtonGroupValueChanged args) { - if (IsChecked || string.IsNullOrEmpty(GroupName) || GroupName != args.GroupName || Value != args.Value || !MatchesScope(args)) + if (radioButton.IsChecked || string.IsNullOrEmpty(radioButton.GroupName) || radioButton.GroupName != args.GroupName + || radioButton.Value != args.Value || !MatchesScope(args, radioButton)) { return; } - IsChecked = true; + radioButton.IsChecked = true; } static void BindToTemplatedParent(BindableObject bindableObject, params BindableProperty[] properties) diff --git a/src/Controls/src/Core/RadioButtonGroup.cs b/src/Controls/src/Core/RadioButtonGroup.cs index a147540ba241..8dc1b754ea1b 100644 --- a/src/Controls/src/Core/RadioButtonGroup.cs +++ b/src/Controls/src/Core/RadioButtonGroup.cs @@ -1,12 +1,10 @@ using System.Collections; +using CommunityToolkit.Mvvm.Messaging; namespace Microsoft.Maui.Controls { public static class RadioButtonGroup { - internal const string GroupSelectionChangedMessage = "RadioButtonGroupSelectionChanged"; - internal const string GroupValueChangedMessage = "RadioButtonGroupValueChanged"; - static readonly BindableProperty RadioButtonGroupControllerProperty = BindableProperty.CreateAttached("RadioButtonGroupController", typeof(RadioButtonGroupController), typeof(Compatibility.Layout), default(RadioButtonGroupController), defaultValueCreator: (b) => new RadioButtonGroupController((Compatibility.Layout)b), @@ -54,8 +52,7 @@ internal static void UpdateRadioButtonGroup(RadioButton radioButton) ? GroupByParent(radioButton) : GetVisualRoot(radioButton); - MessagingCenter.Send(radioButton, GroupSelectionChangedMessage, - new RadioButtonGroupSelectionChanged(scope)); + WeakReferenceMessenger.Default.Send(new RadioButtonGroupSelectionChanged(scope, radioButton)); } internal static Element GroupByParent(RadioButton radioButton) diff --git a/src/Controls/src/Core/RadioButtonGroupController.cs b/src/Controls/src/Core/RadioButtonGroupController.cs index 85e35175ba09..4a80b68af8f2 100644 --- a/src/Controls/src/Core/RadioButtonGroupController.cs +++ b/src/Controls/src/Core/RadioButtonGroupController.cs @@ -1,4 +1,5 @@ using System; +using CommunityToolkit.Mvvm.Messaging; namespace Microsoft.Maui.Controls { @@ -27,12 +28,9 @@ public RadioButtonGroupController(Compatibility.Layout layout) UpdateGroupNames(layout, _groupName); } - MessagingCenter.Subscribe(this, - RadioButtonGroup.GroupSelectionChangedMessage, HandleRadioButtonGroupSelectionChanged); - MessagingCenter.Subscribe(this, RadioButton.GroupNameChangedMessage, - HandleRadioButtonGroupNameChanged); - MessagingCenter.Subscribe(this, RadioButton.ValueChangedMessage, - HandleRadioButtonValueChanged); + WeakReferenceMessenger.Default.Register(this, HandleRadioButtonGroupSelectionChanged); + WeakReferenceMessenger.Default.Register(this, HandleRadioButtonGroupNameChanged); + WeakReferenceMessenger.Default.Register(this, HandleRadioButtonValueChanged); } bool MatchesScope(RadioButtonScopeMessage message) @@ -40,8 +38,10 @@ bool MatchesScope(RadioButtonScopeMessage message) return RadioButtonGroup.GetVisualRoot(_layout) == message.Scope; } - void HandleRadioButtonGroupSelectionChanged(RadioButton selected, RadioButtonGroupSelectionChanged args) + void HandleRadioButtonGroupSelectionChanged(RadioButtonGroupController controller, RadioButtonGroupSelectionChanged args) { + var selected = args.RadioButton; + if (selected.GroupName != _groupName || !MatchesScope(args)) { return; @@ -50,7 +50,7 @@ void HandleRadioButtonGroupSelectionChanged(RadioButton selected, RadioButtonGro _layout.SetValue(RadioButtonGroup.SelectedValueProperty, selected.Value); } - void HandleRadioButtonGroupNameChanged(RadioButton radioButton, RadioButtonGroupNameChanged args) + void HandleRadioButtonGroupNameChanged(RadioButtonGroupController controller, RadioButtonGroupNameChanged args) { if (args.OldName != _groupName || !MatchesScope(args)) { @@ -60,8 +60,10 @@ void HandleRadioButtonGroupNameChanged(RadioButton radioButton, RadioButtonGroup _layout.ClearValue(RadioButtonGroup.SelectedValueProperty); } - void HandleRadioButtonValueChanged(RadioButton radioButton, RadioButtonValueChanged args) + void HandleRadioButtonValueChanged(RadioButtonGroupController controller, RadioButtonValueChanged args) { + var radioButton = args.RadioButton; + if (radioButton.GroupName != _groupName || !MatchesScope(args)) { return; @@ -132,8 +134,7 @@ void SetSelectedValue(object radioButtonValue) if (radioButtonValue != null) { - MessagingCenter.Send(_layout, RadioButtonGroup.GroupValueChangedMessage, - new RadioButtonGroupValueChanged(_groupName, RadioButtonGroup.GetVisualRoot(_layout), radioButtonValue)); + WeakReferenceMessenger.Default.Send(new RadioButtonGroupValueChanged(_groupName, RadioButtonGroup.GetVisualRoot(_layout), radioButtonValue)); } } diff --git a/src/Controls/src/Core/RadioButtonGroupSelectionChanged.cs b/src/Controls/src/Core/RadioButtonGroupSelectionChanged.cs index 78e731c4b92d..a6b7f4b6fe70 100644 --- a/src/Controls/src/Core/RadioButtonGroupSelectionChanged.cs +++ b/src/Controls/src/Core/RadioButtonGroupSelectionChanged.cs @@ -9,7 +9,12 @@ internal abstract class RadioButtonScopeMessage internal class RadioButtonGroupSelectionChanged : RadioButtonScopeMessage { - public RadioButtonGroupSelectionChanged(Element scope) : base(scope) { } + public RadioButtonGroupSelectionChanged(Element scope, RadioButton radioButton) : base(scope) + { + RadioButton = radioButton; + } + + public RadioButton RadioButton { get; } } internal class RadioButtonGroupNameChanged : RadioButtonScopeMessage @@ -24,7 +29,12 @@ public RadioButtonGroupNameChanged(Element scope, string oldName) : base(scope) internal class RadioButtonValueChanged : RadioButtonScopeMessage { - public RadioButtonValueChanged(Element scope) : base(scope) { } + public RadioButtonValueChanged(Element scope, RadioButton radioButton) : base(scope) + { + RadioButton = radioButton; + } + + public RadioButton RadioButton { get; } } internal class RadioButtonGroupValueChanged : RadioButtonScopeMessage diff --git a/src/Controls/tests/Core.UnitTests/MapTests.cs b/src/Controls/tests/Core.UnitTests/MapTests.cs index 4eca8131cdb4..8aa7b1113c72 100644 --- a/src/Controls/tests/Core.UnitTests/MapTests.cs +++ b/src/Controls/tests/Core.UnitTests/MapTests.cs @@ -4,6 +4,7 @@ using System.Linq; using Microsoft.Maui.Controls.Maps; using NUnit.Framework; +using CommunityToolkit.Mvvm.Messaging; namespace Microsoft.Maui.Controls.Core.UnitTests { @@ -127,11 +128,12 @@ public void VisibleRegion() Assert.AreEqual(null, map.VisibleRegion); bool signaled = false; - MessagingCenter.Subscribe(this, "MapMoveToRegion", (s, a) => + + WeakReferenceMessenger.Default.Register(this, (receiver, args) => { signaled = true; - map.SetVisibleRegion(a); - }, map); + map.SetVisibleRegion(args); + }); map.MoveToRegion(new MapSpan(new Position(1, 2), 3, 4)); Assert.AreEqual(new MapSpan(new Position(1, 2), 3, 4), map.LastMoveToRegion); diff --git a/src/Controls/tests/Core.UnitTests/PageTests.cs b/src/Controls/tests/Core.UnitTests/PageTests.cs index 384efe4f61ba..4bb350ea2d45 100644 --- a/src/Controls/tests/Core.UnitTests/PageTests.cs +++ b/src/Controls/tests/Core.UnitTests/PageTests.cs @@ -4,6 +4,7 @@ using Microsoft.Maui.Controls.Internals; using Microsoft.Maui.Graphics; using NUnit.Framework; +using CommunityToolkit.Mvvm.Messaging; namespace Microsoft.Maui.Controls.Core.UnitTests { @@ -14,7 +15,7 @@ public class PageTests : BaseTestFixture public override void TearDown() { base.TearDown(); - MessagingCenter.ClearSubscribers(); + WeakReferenceMessenger.Default.Reset(); } [Test] @@ -308,7 +309,8 @@ public void TestThrowOnInvalidAlignment() public void BusyNotSentWhenNotVisible() { var sent = false; - MessagingCenter.Subscribe(this, Page.BusySetSignalName, (p, b) => sent = true); + + WeakReferenceMessenger.Default.Register(this, (r, m) => sent = true); new ContentPage { IsBusy = true }; @@ -319,9 +321,10 @@ public void BusyNotSentWhenNotVisible() public void BusySentWhenBusyPageAppears() { var sent = false; - MessagingCenter.Subscribe(this, Page.BusySetSignalName, (p, b) => + + WeakReferenceMessenger.Default.Register(this, (r, m) => { - Assert.That(b, Is.True); + Assert.That(m.IsBusy, Is.True); sent = true; }); @@ -342,9 +345,9 @@ public void BusySentWhenBusyPageDisappears() ((IPageController)page).SendAppearing(); var sent = false; - MessagingCenter.Subscribe(this, Page.BusySetSignalName, (p, b) => + WeakReferenceMessenger.Default.Register(this, (r, m) => { - Assert.That(b, Is.False); + Assert.That(m.IsBusy, Is.True); sent = true; }); @@ -357,7 +360,7 @@ public void BusySentWhenBusyPageDisappears() public void BusySentWhenVisiblePageSetToBusy() { var sent = false; - MessagingCenter.Subscribe(this, Page.BusySetSignalName, (p, b) => sent = true); + WeakReferenceMessenger.Default.Register(this, (r, m) => sent = true); var page = new ContentPage(); _ = new Window(page); @@ -376,7 +379,7 @@ public void DisplayAlert() var page = new ContentPage() { IsPlatformEnabled = true }; AlertArguments args = null; - MessagingCenter.Subscribe(this, Page.AlertSignalName, (Page sender, AlertArguments e) => args = e); + WeakReferenceMessenger.Default.Register(this, (r, m) => args = m.Arguments); var task = page.DisplayAlert("Title", "Message", "Accept", "Cancel"); @@ -399,7 +402,8 @@ public void DisplayActionSheet() var page = new ContentPage() { IsPlatformEnabled = true }; ActionSheetArguments args = null; - MessagingCenter.Subscribe(this, Page.ActionSheetSignalName, (Page sender, ActionSheetArguments e) => args = e); + + WeakReferenceMessenger.Default.Register(this, (r, m) => args = m.Arguments); var task = page.DisplayActionSheet("Title", "Cancel", "Destruction", "Other 1", "Other 2"); From 4a01020e8044020c0505dae47993754067ccbb74 Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Thu, 30 Dec 2021 12:43:55 -0700 Subject: [PATCH 4/9] Convert message classes to records --- .../src/iOS/Renderers/NavigationRenderer.cs | 2 +- .../src/Core/HandlerImpl/Page.Impl.cs | 2 +- src/Controls/src/Core/IsExternalInit.cs | 9 ++++ src/Controls/src/Core/Page.cs | 51 +++---------------- .../src/Core/RadioButtonGroupController.cs | 2 +- .../Core/RadioButtonGroupSelectionChanged.cs | 51 ------------------- src/Controls/src/Core/RadioButtonMessages.cs | 16 ++++++ 7 files changed, 34 insertions(+), 99 deletions(-) create mode 100644 src/Controls/src/Core/IsExternalInit.cs delete mode 100644 src/Controls/src/Core/RadioButtonGroupSelectionChanged.cs create mode 100644 src/Controls/src/Core/RadioButtonMessages.cs diff --git a/src/Compatibility/Core/src/iOS/Renderers/NavigationRenderer.cs b/src/Compatibility/Core/src/iOS/Renderers/NavigationRenderer.cs index 3060bcbbb774..07284197b1b3 100644 --- a/src/Compatibility/Core/src/iOS/Renderers/NavigationRenderer.cs +++ b/src/Compatibility/Core/src/iOS/Renderers/NavigationRenderer.cs @@ -1712,5 +1712,5 @@ protected override void Dispose(bool disposing) } } - internal class UpdateToolBarButtonsMessage { } + internal sealed record UpdateToolBarButtonsMessage { } } diff --git a/src/Controls/src/Core/HandlerImpl/Page.Impl.cs b/src/Controls/src/Core/HandlerImpl/Page.Impl.cs index a11b3d36e862..9e5843bb763e 100644 --- a/src/Controls/src/Core/HandlerImpl/Page.Impl.cs +++ b/src/Controls/src/Core/HandlerImpl/Page.Impl.cs @@ -80,5 +80,5 @@ internal NavigatedFromEventArgs(Page destinationPage) internal Page DestinationPage { get; } } - public class CloseContextActionsMessage { } + public sealed record CloseContextActionsMessage { } } diff --git a/src/Controls/src/Core/IsExternalInit.cs b/src/Controls/src/Core/IsExternalInit.cs new file mode 100644 index 000000000000..49c7c9430e3d --- /dev/null +++ b/src/Controls/src/Core/IsExternalInit.cs @@ -0,0 +1,9 @@ +// https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.isexternalinit?view=net-5.0 +// https://developercommunity.visualstudio.com/t/error-cs0518-predefined-type-systemruntimecompiler/1244809 +// Adding this because at least one of the target frameworks doesn't include it; hopefully we can drop this at some point +// (and hopefully before release) +// TODO ezhart Evaluate whether we still need this +namespace System.Runtime.CompilerServices +{ + internal static class IsExternalInit { } +} diff --git a/src/Controls/src/Core/Page.cs b/src/Controls/src/Core/Page.cs index 4dee86fff3f3..2b375c4e24fa 100644 --- a/src/Controls/src/Core/Page.cs +++ b/src/Controls/src/Core/Page.cs @@ -576,51 +576,12 @@ internal void SetTitleView(View oldTitleView, View newTitleView) } // TODO ezhart These Pages could probably all be IView - public class PageBusyMessage - { - public PageBusyMessage(Page page, bool isBusy) - { - Page = page; - IsBusy = isBusy; - } - - public Page Page { get; set; } - public bool IsBusy { get; set; } - } - - public class PageAlertMessage - { - public PageAlertMessage(Page page, AlertArguments arguments) - { - Page = page; - Arguments = arguments; - } - - public Page Page { get; set; } - public AlertArguments Arguments { get; set; } - } + public sealed record PageBusyMessage(Page Page, bool IsBusy); - public class PromptMessage - { - public PromptMessage(Page page, PromptArguments arguments) - { - Page = page; - Arguments = arguments; - } + public sealed record PageAlertMessage(Page Page, AlertArguments Arguments); - public Page Page { get; set; } - public PromptArguments Arguments { get; set; } - } - - public class ActionSheetMessage - { - public ActionSheetMessage(Page page, ActionSheetArguments arguments) - { - Page = page; - Arguments = arguments; - } - - public Page Page { get; set; } - public ActionSheetArguments Arguments { get; set; } - } + public sealed record PromptMessage(Page Page, PromptArguments Arguments); + + public sealed record ActionSheetMessage(Page Page, ActionSheetArguments Arguments); } + diff --git a/src/Controls/src/Core/RadioButtonGroupController.cs b/src/Controls/src/Core/RadioButtonGroupController.cs index 4a80b68af8f2..de56e56a1685 100644 --- a/src/Controls/src/Core/RadioButtonGroupController.cs +++ b/src/Controls/src/Core/RadioButtonGroupController.cs @@ -134,7 +134,7 @@ void SetSelectedValue(object radioButtonValue) if (radioButtonValue != null) { - WeakReferenceMessenger.Default.Send(new RadioButtonGroupValueChanged(_groupName, RadioButtonGroup.GetVisualRoot(_layout), radioButtonValue)); + WeakReferenceMessenger.Default.Send(new RadioButtonGroupValueChanged(RadioButtonGroup.GetVisualRoot(_layout), radioButtonValue, _groupName)); } } diff --git a/src/Controls/src/Core/RadioButtonGroupSelectionChanged.cs b/src/Controls/src/Core/RadioButtonGroupSelectionChanged.cs deleted file mode 100644 index a6b7f4b6fe70..000000000000 --- a/src/Controls/src/Core/RadioButtonGroupSelectionChanged.cs +++ /dev/null @@ -1,51 +0,0 @@ -namespace Microsoft.Maui.Controls -{ - internal abstract class RadioButtonScopeMessage - { - public Element Scope { get; } - - protected RadioButtonScopeMessage(Element scope) => Scope = scope; - } - - internal class RadioButtonGroupSelectionChanged : RadioButtonScopeMessage - { - public RadioButtonGroupSelectionChanged(Element scope, RadioButton radioButton) : base(scope) - { - RadioButton = radioButton; - } - - public RadioButton RadioButton { get; } - } - - internal class RadioButtonGroupNameChanged : RadioButtonScopeMessage - { - public string OldName { get; } - - public RadioButtonGroupNameChanged(Element scope, string oldName) : base(scope) - { - OldName = oldName; - } - } - - internal class RadioButtonValueChanged : RadioButtonScopeMessage - { - public RadioButtonValueChanged(Element scope, RadioButton radioButton) : base(scope) - { - RadioButton = radioButton; - } - - public RadioButton RadioButton { get; } - } - - internal class RadioButtonGroupValueChanged : RadioButtonScopeMessage - { - public object Value { get; } - public string GroupName { get; } - - public RadioButtonGroupValueChanged(string groupName, Element scope, object value) : base(scope) - { - GroupName = groupName; - Value = value; - } - } -} \ No newline at end of file diff --git a/src/Controls/src/Core/RadioButtonMessages.cs b/src/Controls/src/Core/RadioButtonMessages.cs new file mode 100644 index 000000000000..ad51d711da19 --- /dev/null +++ b/src/Controls/src/Core/RadioButtonMessages.cs @@ -0,0 +1,16 @@ +namespace Microsoft.Maui.Controls +{ + internal abstract record RadioButtonScopeMessage(Element Scope); + + internal sealed record RadioButtonGroupSelectionChanged(Element Scope, RadioButton RadioButton) + : RadioButtonScopeMessage(Scope); + + internal sealed record RadioButtonGroupNameChanged(Element Scope, string OldName) + : RadioButtonScopeMessage(Scope); + + internal sealed record RadioButtonValueChanged(Element Scope, RadioButton RadioButton) + : RadioButtonScopeMessage(Scope); + + internal sealed record RadioButtonGroupValueChanged (Element Scope, object Value, string GroupName) + : RadioButtonScopeMessage(Scope); +} \ No newline at end of file From c4e8f49d5e15b8f03c29a1ff9328e6be7cf1aa93 Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Thu, 30 Dec 2021 12:52:51 -0700 Subject: [PATCH 5/9] Don't make empty classes into records --- src/Compatibility/Core/src/iOS/Renderers/NavigationRenderer.cs | 2 +- src/Controls/src/Core/HandlerImpl/Page.Impl.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Compatibility/Core/src/iOS/Renderers/NavigationRenderer.cs b/src/Compatibility/Core/src/iOS/Renderers/NavigationRenderer.cs index 07284197b1b3..071b792c5497 100644 --- a/src/Compatibility/Core/src/iOS/Renderers/NavigationRenderer.cs +++ b/src/Compatibility/Core/src/iOS/Renderers/NavigationRenderer.cs @@ -1712,5 +1712,5 @@ protected override void Dispose(bool disposing) } } - internal sealed record UpdateToolBarButtonsMessage { } + internal sealed class UpdateToolBarButtonsMessage { } } diff --git a/src/Controls/src/Core/HandlerImpl/Page.Impl.cs b/src/Controls/src/Core/HandlerImpl/Page.Impl.cs index 9e5843bb763e..a8678fafbe9a 100644 --- a/src/Controls/src/Core/HandlerImpl/Page.Impl.cs +++ b/src/Controls/src/Core/HandlerImpl/Page.Impl.cs @@ -80,5 +80,5 @@ internal NavigatedFromEventArgs(Page destinationPage) internal Page DestinationPage { get; } } - public sealed record CloseContextActionsMessage { } + public sealed class CloseContextActionsMessage { } } From 63a4020d1cfbef4fbf5b624c65dfdeefe2b92665 Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Thu, 30 Dec 2021 13:26:26 -0700 Subject: [PATCH 6/9] Fix some closures and method group usage --- .../src/Android/Renderers/ListViewAdapter.cs | 2 +- .../Maps/src/Android/MapRenderer.cs | 3 ++- src/Compatibility/Maps/src/iOS/MapRenderer.cs | 2 +- src/Controls/src/Core/RadioButton.cs | 21 +++++++++---------- .../src/Core/RadioButtonGroupController.cs | 12 +++++------ 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/Compatibility/Core/src/Android/Renderers/ListViewAdapter.cs b/src/Compatibility/Core/src/Android/Renderers/ListViewAdapter.cs index 9f500f98dfe1..af04fbb4890d 100644 --- a/src/Compatibility/Core/src/Android/Renderers/ListViewAdapter.cs +++ b/src/Compatibility/Core/src/Android/Renderers/ListViewAdapter.cs @@ -65,7 +65,7 @@ public ListViewAdapter(Context context, AListView realListView, ListView listVie realListView.OnItemClickListener = this; realListView.OnItemLongClickListener = this; - WeakReferenceMessenger.Default.Register(this, (recipient, msg) => CloseContextActions()); + WeakReferenceMessenger.Default.Register(this, (r, m) => r.CloseContextActions()); InvalidateCount(); } diff --git a/src/Compatibility/Maps/src/Android/MapRenderer.cs b/src/Compatibility/Maps/src/Android/MapRenderer.cs index 139a3b665ced..6d9733a3dc84 100644 --- a/src/Compatibility/Maps/src/Android/MapRenderer.cs +++ b/src/Compatibility/Maps/src/Android/MapRenderer.cs @@ -157,7 +157,8 @@ protected override void OnElementChanged(ElementChangedEventArgs e) Control.GetMapAsync(this); - WeakReferenceMessenger.Default.Register(this, (renderer, args) => OnMoveToRegionMessage(renderer.Element, args)); + WeakReferenceMessenger.Default.Register(this, + (r, m) => r.OnMoveToRegionMessage(r.Element, m)); ((INotifyCollectionChanged)Map.Pins).CollectionChanged += OnPinCollectionChanged; ((INotifyCollectionChanged)Map.MapElements).CollectionChanged += OnMapElementCollectionChanged; diff --git a/src/Compatibility/Maps/src/iOS/MapRenderer.cs b/src/Compatibility/Maps/src/iOS/MapRenderer.cs index e2c9d4f63517..6c0732abbb28 100644 --- a/src/Compatibility/Maps/src/iOS/MapRenderer.cs +++ b/src/Compatibility/Maps/src/iOS/MapRenderer.cs @@ -164,7 +164,7 @@ protected override void OnElementChanged(ElementChangedEventArgs e) #endif } - WeakReferenceMessenger.Default.Register(this, (renderer, args) => MoveToRegion(args)); + WeakReferenceMessenger.Default.Register(this, (r, m) => r.MoveToRegion(m)); if (mapModel.LastMoveToRegion != null) MoveToRegion(mapModel.LastMoveToRegion, false); diff --git a/src/Controls/src/Core/RadioButton.cs b/src/Controls/src/Core/RadioButton.cs index 930779dbaab6..a5569cbdfb9e 100644 --- a/src/Controls/src/Core/RadioButton.cs +++ b/src/Controls/src/Core/RadioButton.cs @@ -388,8 +388,8 @@ void OnGroupNamePropertyChanged(string oldGroupName, string newGroupName) { if (string.IsNullOrEmpty(oldGroupName)) { - WeakReferenceMessenger.Default.Register(this, HandleRadioButtonGroupSelectionChanged); - WeakReferenceMessenger.Default.Register(this, HandleRadioButtonGroupValueChanged); + WeakReferenceMessenger.Default.Register(this, (r,m) => r.HandleRadioButtonGroupSelectionChanged(m)); + WeakReferenceMessenger.Default.Register(this, (r,m) => r.HandleRadioButtonGroupValueChanged(m)); } WeakReferenceMessenger.Default.Send(new RadioButtonGroupNameChanged(RadioButtonGroup.GetVisualRoot(this), oldGroupName)); @@ -404,32 +404,31 @@ void OnGroupNamePropertyChanged(string oldGroupName, string newGroupName) } } - static bool MatchesScope(RadioButtonScopeMessage message, RadioButton radioButton) + bool MatchesScope(RadioButtonScopeMessage message) { - return RadioButtonGroup.GetVisualRoot(radioButton) == message.Scope; + return RadioButtonGroup.GetVisualRoot(this) == message.Scope; } - static void HandleRadioButtonGroupSelectionChanged(RadioButton receiver, RadioButtonGroupSelectionChanged args) + void HandleRadioButtonGroupSelectionChanged(RadioButtonGroupSelectionChanged args) { var selected = args.RadioButton; - if (!receiver.IsChecked || selected == receiver || string.IsNullOrEmpty(receiver.GroupName) || receiver.GroupName != selected.GroupName || !MatchesScope(args, receiver)) + if (!IsChecked || selected == this || string.IsNullOrEmpty(GroupName) || GroupName != selected.GroupName || !MatchesScope(args)) { return; } - receiver.IsChecked = false; + IsChecked = false; } - static void HandleRadioButtonGroupValueChanged(RadioButton radioButton, RadioButtonGroupValueChanged args) + void HandleRadioButtonGroupValueChanged(RadioButtonGroupValueChanged args) { - if (radioButton.IsChecked || string.IsNullOrEmpty(radioButton.GroupName) || radioButton.GroupName != args.GroupName - || radioButton.Value != args.Value || !MatchesScope(args, radioButton)) + if (IsChecked || string.IsNullOrEmpty(GroupName) || GroupName != args.GroupName || Value != args.Value || !MatchesScope(args)) { return; } - radioButton.IsChecked = true; + IsChecked = true; } static void BindToTemplatedParent(BindableObject bindableObject, params BindableProperty[] properties) diff --git a/src/Controls/src/Core/RadioButtonGroupController.cs b/src/Controls/src/Core/RadioButtonGroupController.cs index de56e56a1685..0920afde5bb4 100644 --- a/src/Controls/src/Core/RadioButtonGroupController.cs +++ b/src/Controls/src/Core/RadioButtonGroupController.cs @@ -28,9 +28,9 @@ public RadioButtonGroupController(Compatibility.Layout layout) UpdateGroupNames(layout, _groupName); } - WeakReferenceMessenger.Default.Register(this, HandleRadioButtonGroupSelectionChanged); - WeakReferenceMessenger.Default.Register(this, HandleRadioButtonGroupNameChanged); - WeakReferenceMessenger.Default.Register(this, HandleRadioButtonValueChanged); + WeakReferenceMessenger.Default.Register(this, (r,m) => r.HandleRadioButtonGroupSelectionChanged(m)); + WeakReferenceMessenger.Default.Register(this, (r,m) => HandleRadioButtonGroupNameChanged(m)); + WeakReferenceMessenger.Default.Register(this, (r,m) => r.HandleRadioButtonValueChanged(m)); } bool MatchesScope(RadioButtonScopeMessage message) @@ -38,7 +38,7 @@ bool MatchesScope(RadioButtonScopeMessage message) return RadioButtonGroup.GetVisualRoot(_layout) == message.Scope; } - void HandleRadioButtonGroupSelectionChanged(RadioButtonGroupController controller, RadioButtonGroupSelectionChanged args) + void HandleRadioButtonGroupSelectionChanged(RadioButtonGroupSelectionChanged args) { var selected = args.RadioButton; @@ -50,7 +50,7 @@ void HandleRadioButtonGroupSelectionChanged(RadioButtonGroupController controlle _layout.SetValue(RadioButtonGroup.SelectedValueProperty, selected.Value); } - void HandleRadioButtonGroupNameChanged(RadioButtonGroupController controller, RadioButtonGroupNameChanged args) + void HandleRadioButtonGroupNameChanged(RadioButtonGroupNameChanged args) { if (args.OldName != _groupName || !MatchesScope(args)) { @@ -60,7 +60,7 @@ void HandleRadioButtonGroupNameChanged(RadioButtonGroupController controller, Ra _layout.ClearValue(RadioButtonGroup.SelectedValueProperty); } - void HandleRadioButtonValueChanged(RadioButtonGroupController controller, RadioButtonValueChanged args) + void HandleRadioButtonValueChanged(RadioButtonValueChanged args) { var radioButton = args.RadioButton; From b425af7a93ff169ce130d62fba24fd622f1ec0ba Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Thu, 30 Dec 2021 13:50:14 -0700 Subject: [PATCH 7/9] Mark lambdas static --- .../Core/src/Android/Renderers/ListViewAdapter.cs | 2 +- src/Controls/src/Core/RadioButton.cs | 4 ++-- src/Controls/src/Core/RadioButtonGroupController.cs | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Compatibility/Core/src/Android/Renderers/ListViewAdapter.cs b/src/Compatibility/Core/src/Android/Renderers/ListViewAdapter.cs index af04fbb4890d..cd04743b46e1 100644 --- a/src/Compatibility/Core/src/Android/Renderers/ListViewAdapter.cs +++ b/src/Compatibility/Core/src/Android/Renderers/ListViewAdapter.cs @@ -65,7 +65,7 @@ public ListViewAdapter(Context context, AListView realListView, ListView listVie realListView.OnItemClickListener = this; realListView.OnItemLongClickListener = this; - WeakReferenceMessenger.Default.Register(this, (r, m) => r.CloseContextActions()); + WeakReferenceMessenger.Default.Register(this, static (r, m) => r.CloseContextActions()); InvalidateCount(); } diff --git a/src/Controls/src/Core/RadioButton.cs b/src/Controls/src/Core/RadioButton.cs index a5569cbdfb9e..0bbc348f579d 100644 --- a/src/Controls/src/Core/RadioButton.cs +++ b/src/Controls/src/Core/RadioButton.cs @@ -388,8 +388,8 @@ void OnGroupNamePropertyChanged(string oldGroupName, string newGroupName) { if (string.IsNullOrEmpty(oldGroupName)) { - WeakReferenceMessenger.Default.Register(this, (r,m) => r.HandleRadioButtonGroupSelectionChanged(m)); - WeakReferenceMessenger.Default.Register(this, (r,m) => r.HandleRadioButtonGroupValueChanged(m)); + WeakReferenceMessenger.Default.Register(this, static (r,m) => r.HandleRadioButtonGroupSelectionChanged(m)); + WeakReferenceMessenger.Default.Register(this, static (r,m) => r.HandleRadioButtonGroupValueChanged(m)); } WeakReferenceMessenger.Default.Send(new RadioButtonGroupNameChanged(RadioButtonGroup.GetVisualRoot(this), oldGroupName)); diff --git a/src/Controls/src/Core/RadioButtonGroupController.cs b/src/Controls/src/Core/RadioButtonGroupController.cs index 0920afde5bb4..ec83d14e0317 100644 --- a/src/Controls/src/Core/RadioButtonGroupController.cs +++ b/src/Controls/src/Core/RadioButtonGroupController.cs @@ -28,9 +28,9 @@ public RadioButtonGroupController(Compatibility.Layout layout) UpdateGroupNames(layout, _groupName); } - WeakReferenceMessenger.Default.Register(this, (r,m) => r.HandleRadioButtonGroupSelectionChanged(m)); - WeakReferenceMessenger.Default.Register(this, (r,m) => HandleRadioButtonGroupNameChanged(m)); - WeakReferenceMessenger.Default.Register(this, (r,m) => r.HandleRadioButtonValueChanged(m)); + WeakReferenceMessenger.Default.Register(this, static (r,m) => r.HandleRadioButtonGroupSelectionChanged(m)); + WeakReferenceMessenger.Default.Register(this, static (r,m) => r.HandleRadioButtonGroupNameChanged(m)); + WeakReferenceMessenger.Default.Register(this, static (r,m) => r.HandleRadioButtonValueChanged(m)); } bool MatchesScope(RadioButtonScopeMessage message) From ca9f0d7b040a71920b66f2a6929812d9026bb0a3 Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Thu, 30 Dec 2021 15:17:58 -0700 Subject: [PATCH 8/9] Clean up more closures and method groups --- .../Core/src/Android/PopupManager.cs | 9 +++--- .../src/Android/Renderers/ListViewAdapter.cs | 2 +- .../Core/src/Windows/Platform.cs | 14 ++++----- .../src/iOS/Renderers/NavigationRenderer.cs | 6 ++-- .../Maps/src/Android/MapRenderer.cs | 3 +- src/Compatibility/Maps/src/iOS/MapRenderer.cs | 2 +- .../AlertManager/AlertManager.Android.cs | 30 +++++++++---------- .../AlertManager/AlertManager.Windows.cs | 18 +++++------ .../Platform/AlertManager/AlertManager.iOS.cs | 18 +++++------ 9 files changed, 51 insertions(+), 51 deletions(-) diff --git a/src/Compatibility/Core/src/Android/PopupManager.cs b/src/Compatibility/Core/src/Android/PopupManager.cs index e86a8b6010a1..bcdab131d088 100644 --- a/src/Compatibility/Core/src/Android/PopupManager.cs +++ b/src/Compatibility/Core/src/Android/PopupManager.cs @@ -52,16 +52,17 @@ internal PopupRequestHelper(Activity context) { Activity = context; - WeakReferenceMessenger.Default.Register(Activity, (r, m) => OnPageBusy(m.Page, m.IsBusy)); - WeakReferenceMessenger.Default.Register(Activity, (r, m) => OnAlertRequested(m.Page, m.Arguments)); - WeakReferenceMessenger.Default.Register(Activity, (r, m) => OnPromptRequested(m.Page, m.Arguments)); - WeakReferenceMessenger.Default.Register(Activity, (r, m) => OnActionSheetRequested(m.Page, m.Arguments)); + WeakReferenceMessenger.Default.Register(this, static (r, m) => r.OnPageBusy(m.Page, m.IsBusy)); + WeakReferenceMessenger.Default.Register(this, static (r, m) => r.OnAlertRequested(m.Page, m.Arguments)); + WeakReferenceMessenger.Default.Register(this, static (r, m) => r.OnPromptRequested(m.Page, m.Arguments)); + WeakReferenceMessenger.Default.Register(this, static (r, m) => r.OnActionSheetRequested(m.Page, m.Arguments)); } public Activity Activity { get; } public void Dispose() { + WeakReferenceMessenger.Default.UnregisterAll(this); } public void ResetBusyCount() diff --git a/src/Compatibility/Core/src/Android/Renderers/ListViewAdapter.cs b/src/Compatibility/Core/src/Android/Renderers/ListViewAdapter.cs index cd04743b46e1..7b9ff6f4c7c7 100644 --- a/src/Compatibility/Core/src/Android/Renderers/ListViewAdapter.cs +++ b/src/Compatibility/Core/src/Android/Renderers/ListViewAdapter.cs @@ -65,7 +65,7 @@ public ListViewAdapter(Context context, AListView realListView, ListView listVie realListView.OnItemClickListener = this; realListView.OnItemLongClickListener = this; - WeakReferenceMessenger.Default.Register(this, static (r, m) => r.CloseContextActions()); + WeakReferenceMessenger.Default.Register(this, static (r, m) => r.CloseContextActions()); InvalidateCount(); } diff --git a/src/Compatibility/Core/src/Windows/Platform.cs b/src/Compatibility/Core/src/Windows/Platform.cs index 794f0f0bffb3..c2e010c535ca 100644 --- a/src/Compatibility/Core/src/Windows/Platform.cs +++ b/src/Compatibility/Core/src/Windows/Platform.cs @@ -137,7 +137,7 @@ internal Platform(Microsoft.UI.Xaml.Window page) _container.SizeChanged += OnRendererSizeChanged; - WeakReferenceMessenger.Default.Register(page, (window, message) => + WeakReferenceMessenger.Default.Register(page, (_, message) => { Microsoft.UI.Xaml.Controls.ProgressBar indicator = GetBusyIndicator(); indicator.Visibility = message.IsBusy ? WVisibility.Visible : WVisibility.Collapsed; @@ -602,12 +602,12 @@ internal IToolbarProvider GetToolbarProvider() internal static void SubscribeAlertsAndActionSheets() { - WeakReferenceMessenger.Default.Register(Forms.MainWindow, OnPageAlert); - WeakReferenceMessenger.Default.Register(Forms.MainWindow, OnPagePrompt); - WeakReferenceMessenger.Default.Register(Forms.MainWindow, OnPageActionSheet); + WeakReferenceMessenger.Default.Register(Forms.MainWindow, (r,m) => OnPageAlert(m)); + WeakReferenceMessenger.Default.Register(Forms.MainWindow, (r, m) => OnPagePrompt(m)); + WeakReferenceMessenger.Default.Register(Forms.MainWindow, (r, m) => OnPageActionSheet(m)); } - static void OnPageActionSheet(UI.Xaml.Window window, ActionSheetMessage message) + static void OnPageActionSheet(ActionSheetMessage message) { var sender = message.Page; var options = message.Arguments; @@ -658,7 +658,7 @@ static void OnPageActionSheet(UI.Xaml.Window window, ActionSheetMessage message) } } - static async void OnPagePrompt(UI.Xaml.Window window, PromptMessage message) + static async void OnPagePrompt(PromptMessage message) { var sender = message.Page; var options = message.Arguments; @@ -700,7 +700,7 @@ static async Task ShowPrompt(PromptDialog prompt) return null; } - static async void OnPageAlert(UI.Xaml.Window window, PageAlertMessage message) + static async void OnPageAlert(PageAlertMessage message) { var sender = message.Page; var options = message.Arguments; diff --git a/src/Compatibility/Core/src/iOS/Renderers/NavigationRenderer.cs b/src/Compatibility/Core/src/iOS/Renderers/NavigationRenderer.cs index 071b792c5497..3ee8f8bb7461 100644 --- a/src/Compatibility/Core/src/iOS/Renderers/NavigationRenderer.cs +++ b/src/Compatibility/Core/src/iOS/Renderers/NavigationRenderer.cs @@ -40,11 +40,11 @@ public class NavigationRenderer : UINavigationController, IVisualElementRenderer [Preserve(Conditional = true)] public NavigationRenderer() : base(typeof(FormsNavigationBar), null) { - WeakReferenceMessenger.Default.Register(this, (receiver, message) => + WeakReferenceMessenger.Default.Register(this, static (receiver, message) => { - if (!ViewControllers.Any()) + if (!receiver.ViewControllers.Any()) return; - var parentingViewController = GetParentingViewController(); + var parentingViewController = receiver.GetParentingViewController(); parentingViewController?.UpdateLeftBarButtonItem(); }); } diff --git a/src/Compatibility/Maps/src/Android/MapRenderer.cs b/src/Compatibility/Maps/src/Android/MapRenderer.cs index 6d9733a3dc84..8b6b14073c65 100644 --- a/src/Compatibility/Maps/src/Android/MapRenderer.cs +++ b/src/Compatibility/Maps/src/Android/MapRenderer.cs @@ -157,8 +157,7 @@ protected override void OnElementChanged(ElementChangedEventArgs e) Control.GetMapAsync(this); - WeakReferenceMessenger.Default.Register(this, - (r, m) => r.OnMoveToRegionMessage(r.Element, m)); + WeakReferenceMessenger.Default.Register(this, static (r, m) => r.OnMoveToRegionMessage(r.Element, m)); ((INotifyCollectionChanged)Map.Pins).CollectionChanged += OnPinCollectionChanged; ((INotifyCollectionChanged)Map.MapElements).CollectionChanged += OnMapElementCollectionChanged; diff --git a/src/Compatibility/Maps/src/iOS/MapRenderer.cs b/src/Compatibility/Maps/src/iOS/MapRenderer.cs index 6c0732abbb28..ed8f874c9be2 100644 --- a/src/Compatibility/Maps/src/iOS/MapRenderer.cs +++ b/src/Compatibility/Maps/src/iOS/MapRenderer.cs @@ -164,7 +164,7 @@ protected override void OnElementChanged(ElementChangedEventArgs e) #endif } - WeakReferenceMessenger.Default.Register(this, (r, m) => r.MoveToRegion(m)); + WeakReferenceMessenger.Default.Register(this, static (r, m) => r.MoveToRegion(m)); if (mapModel.LastMoveToRegion != null) MoveToRegion(mapModel.LastMoveToRegion, false); diff --git a/src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs b/src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs index 9ef277c45618..4e9ca672272b 100644 --- a/src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs +++ b/src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs @@ -65,10 +65,10 @@ internal AlertRequestHelper(Activity context, IMauiContext mauiContext) Activity = context; MauiContext = mauiContext; - WeakReferenceMessenger.Default.Register(Activity, OnPageBusy); - WeakReferenceMessenger.Default.Register(Activity, OnAlertRequested); - WeakReferenceMessenger.Default.Register(Activity, OnPromptRequested); - WeakReferenceMessenger.Default.Register(Activity, OnActionSheetRequested); + WeakReferenceMessenger.Default.Register(this, static (r,m) => r.OnPageBusy(m)); + WeakReferenceMessenger.Default.Register(this, static (r, m) => r.OnAlertRequested(m)); + WeakReferenceMessenger.Default.Register(this, static (r, m) => r.OnPromptRequested(m)); + WeakReferenceMessenger.Default.Register(this, static (r, m) => r.OnActionSheetRequested(m)); } public Activity Activity { get; } @@ -84,7 +84,7 @@ public void ResetBusyCount() _busyCount = 0; } - void OnPageBusy(Activity activity, PageBusyMessage message) + void OnPageBusy(PageBusyMessage message) { var sender = message.Page; var enabled = message.IsBusy; @@ -100,7 +100,7 @@ void OnPageBusy(Activity activity, PageBusyMessage message) UpdateProgressBarVisibility(_busyCount > 0); } - void OnActionSheetRequested(Activity activity, ActionSheetMessage message) + void OnActionSheetRequested(ActionSheetMessage message) { var sender = message.Page; var arguments = message.Arguments; @@ -111,7 +111,7 @@ void OnActionSheetRequested(Activity activity, ActionSheetMessage message) return; } - var builder = new DialogBuilder(activity); + var builder = new DialogBuilder(Activity); builder.SetTitle(arguments.Title); string[] items = arguments.Buttons.ToArray(); @@ -162,7 +162,7 @@ void OnActionSheetRequested(Activity activity, ActionSheetMessage message) } } - void OnAlertRequested(Activity activity, PageAlertMessage message) + void OnAlertRequested(PageAlertMessage message) { var sender = message.Page; var arguments = message.Arguments; @@ -174,7 +174,7 @@ void OnAlertRequested(Activity activity, PageAlertMessage message) } int messageID = 16908299; - var alert = new DialogBuilder(activity).Create(); + var alert = new DialogBuilder(Activity).Create(); if (alert == null) return; @@ -229,7 +229,7 @@ TextDirection GetTextDirection(IView sender, FlowDirection flowDirection) return TextDirection.Ltr; } - void OnPromptRequested(Activity activity, PromptMessage message) + void OnPromptRequested(PromptMessage message) { var sender = message.Page; var arguments = message.Arguments; @@ -240,7 +240,7 @@ void OnPromptRequested(Activity activity, PromptMessage message) return; } - var alertDialog = new DialogBuilder(activity).Create(); + var alertDialog = new DialogBuilder(Activity).Create(); if (alertDialog == null) return; @@ -248,12 +248,12 @@ void OnPromptRequested(Activity activity, PromptMessage message) alertDialog.SetTitle(arguments.Title); alertDialog.SetMessage(arguments.Message); - var frameLayout = new FrameLayout(activity); - var editText = new EditText(activity) { Hint = arguments.Placeholder, Text = arguments.InitialValue }; + var frameLayout = new FrameLayout(Activity); + var editText = new EditText(Activity) { Hint = arguments.Placeholder, Text = arguments.InitialValue }; var layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent) { - LeftMargin = (int)(22 * activity.Resources.DisplayMetrics.Density), - RightMargin = (int)(22 * activity.Resources.DisplayMetrics.Density) + LeftMargin = (int)(22 * Activity.Resources.DisplayMetrics.Density), + RightMargin = (int)(22 * Activity.Resources.DisplayMetrics.Density) }; editText.LayoutParameters = layoutParams; diff --git a/src/Controls/src/Core/Platform/AlertManager/AlertManager.Windows.cs b/src/Controls/src/Core/Platform/AlertManager/AlertManager.Windows.cs index 14ddb237fbe0..08de02c9f3b0 100644 --- a/src/Controls/src/Core/Platform/AlertManager/AlertManager.Windows.cs +++ b/src/Controls/src/Core/Platform/AlertManager/AlertManager.Windows.cs @@ -47,10 +47,10 @@ internal AlertRequestHelper(UI.Xaml.Window window, IMauiContext mauiContext) Window = window; MauiContext = mauiContext; - WeakReferenceMessenger.Default.Register(Window, OnPageBusy); - WeakReferenceMessenger.Default.Register(Window, OnAlertRequested); - WeakReferenceMessenger.Default.Register(Window, OnPromptRequested); - WeakReferenceMessenger.Default.Register(Window, OnActionSheetRequested); + WeakReferenceMessenger.Default.Register(this, static (r,m) => r.OnPageBusy(m)); + WeakReferenceMessenger.Default.Register(this, static (r, m) => r.OnAlertRequested(m)); + WeakReferenceMessenger.Default.Register(this, static (r, m) => r.OnPromptRequested(m)); + WeakReferenceMessenger.Default.Register(this, static (r, m) => r.OnActionSheetRequested(m)); } public UI.Xaml.Window Window { get; } @@ -58,10 +58,10 @@ internal AlertRequestHelper(UI.Xaml.Window window, IMauiContext mauiContext) public void Dispose() { - WeakReferenceMessenger.Default.UnregisterAll(Window); + WeakReferenceMessenger.Default.UnregisterAll(this); } - void OnPageBusy(UI.Xaml.Window window, PageBusyMessage message) + void OnPageBusy(PageBusyMessage message) { var sender = message.Page; var busy = message.IsBusy; @@ -69,7 +69,7 @@ void OnPageBusy(UI.Xaml.Window window, PageBusyMessage message) // TODO: Wrap the pages in a Canvas, and dynamically add a ProgressBar } - async void OnAlertRequested(UI.Xaml.Window window, PageAlertMessage message) + async void OnAlertRequested(PageAlertMessage message) { var arguments = message.Arguments; @@ -116,7 +116,7 @@ async void OnAlertRequested(UI.Xaml.Window window, PageAlertMessage message) CurrentAlert = null; } - async void OnPromptRequested(UI.Xaml.Window window, PromptMessage message) + async void OnPromptRequested(PromptMessage message) { var arguments = message.Arguments; @@ -152,7 +152,7 @@ async void OnPromptRequested(UI.Xaml.Window window, PromptMessage message) CurrentPrompt = null; } - void OnActionSheetRequested(UI.Xaml.Window window, ActionSheetMessage message) + void OnActionSheetRequested(ActionSheetMessage message) { var sender = message.Page; var arguments = message.Arguments; diff --git a/src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs b/src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs index a15415d542f0..9c3fd5ab9e58 100644 --- a/src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs +++ b/src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs @@ -50,20 +50,20 @@ internal AlertRequestHelper(UIWindow window) { Window = window; - WeakReferenceMessenger.Default.Register(Window, OnPageBusy); - WeakReferenceMessenger.Default.Register(Window, OnAlertRequested); - WeakReferenceMessenger.Default.Register(Window, OnPromptRequested); - WeakReferenceMessenger.Default.Register(Window, OnActionSheetRequested); + WeakReferenceMessenger.Default.Register(this, static (r,m) => r.OnPageBusy(m)); + WeakReferenceMessenger.Default.Register(this, static (r, m) => r.OnAlertRequested(m)); + WeakReferenceMessenger.Default.Register(this, static (r, m) => r.OnPromptRequested(m)); + WeakReferenceMessenger.Default.Register(this, static (r, m) => r.OnActionSheetRequested(m)); } public UIWindow Window { get; } public void Dispose() { - WeakReferenceMessenger.Default.UnregisterAll(Window); + WeakReferenceMessenger.Default.UnregisterAll(this); } - void OnPageBusy(UIWindow window, PageBusyMessage message) + void OnPageBusy(PageBusyMessage message) { var enabled = message.IsBusy; @@ -71,17 +71,17 @@ void OnPageBusy(UIWindow window, PageBusyMessage message) UIApplication.SharedApplication.NetworkActivityIndicatorVisible = _busyCount > 0; } - void OnAlertRequested(UIWindow window, PageAlertMessage message) + void OnAlertRequested(PageAlertMessage message) { PresentAlert(message.Arguments); } - void OnPromptRequested(UIWindow window, PromptMessage message) + void OnPromptRequested(PromptMessage message) { PresentPrompt(message.Arguments); } - void OnActionSheetRequested(UIWindow window, ActionSheetMessage message) + void OnActionSheetRequested(ActionSheetMessage message) { PresentActionSheet(message.Arguments); } From b5125da9022473eec4db987a5d08d9e3a351f5fa Mon Sep 17 00:00:00 2001 From: Jonathan Dick Date: Mon, 24 Jan 2022 16:00:07 -0500 Subject: [PATCH 9/9] Update NuGet.config --- NuGet.config | 1 - 1 file changed, 1 deletion(-) diff --git a/NuGet.config b/NuGet.config index 5137e705d7f5..c9f489a210d2 100644 --- a/NuGet.config +++ b/NuGet.config @@ -18,7 +18,6 @@ -