Skip to content

Commit

Permalink
Refactor and reformat Maui and Xaml platform providers
Browse files Browse the repository at this point in the history
Refactored `BeginOnUIThread` in `MauiPlatformProvider.cs` to discard
the result of `dispatcher.RunAsync` using the discard operator `_`.
Reformatted `XamlPlatformProvider.cs` to place opening braces `{` on
new lines for namespace, class, constructor, and several methods.
Updated `BeginOnUIThread` to use the discard operator `_`. Improved
indentation and formatting in various methods for better readability.
  • Loading branch information
vb2ae committed Dec 18, 2024
1 parent e811191 commit 8d62fc4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private bool CheckAccess()
public virtual void BeginOnUIThread(System.Action action)
{
ValidateDispatcher();
var dummy = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => action());
_ = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => action());
}

/// <summary>
Expand Down
67 changes: 44 additions & 23 deletions src/Caliburn.Micro.Platform/XamlPlatformProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Caliburn.Micro {
namespace Caliburn.Micro
{
using System;
using System.Collections.Generic;
using System.Threading;
Expand All @@ -19,7 +20,8 @@
/// <summary>
/// A <see cref="IPlatformProvider"/> implementation for the XAML platfrom.
/// </summary>
public class XamlPlatformProvider : IPlatformProvider {
public class XamlPlatformProvider : IPlatformProvider
{
#if WINDOWS_UWP
private readonly CoreDispatcher dispatcher;
#else
Expand All @@ -29,7 +31,8 @@ public class XamlPlatformProvider : IPlatformProvider {
/// <summary>
/// Initializes a new instance of the <see cref="XamlPlatformProvider"/> class.
/// </summary>
public XamlPlatformProvider() {
public XamlPlatformProvider()
{
#if WINDOWS_UWP
dispatcher = Window.Current.Dispatcher;
#elif AVALONIA
Expand All @@ -47,16 +50,19 @@ public XamlPlatformProvider() {
/// <summary>
/// Indicates whether or not the framework is in design-time mode.
/// </summary>
public virtual bool InDesignMode {
public virtual bool InDesignMode
{
get { return View.InDesignMode; }
}

private void ValidateDispatcher() {
private void ValidateDispatcher()
{
if (dispatcher == null)
throw new InvalidOperationException("Not initialized with dispatcher.");
}

private bool CheckAccess() {
private bool CheckAccess()
{
#if WINDOWS_UWP
return dispatcher == null || Window.Current != null;
#else
Expand All @@ -68,10 +74,11 @@ private bool CheckAccess() {
/// Executes the action on the UI thread asynchronously.
/// </summary>
/// <param name="action">The action to execute.</param>
public virtual void BeginOnUIThread(System.Action action) {
public virtual void BeginOnUIThread(System.Action action)
{
ValidateDispatcher();
#if WINDOWS_UWP
var dummy = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => action());
_ = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => action());
#elif AVALONIA
dispatcher.Post(action);
#else
Expand All @@ -84,7 +91,8 @@ public virtual void BeginOnUIThread(System.Action action) {
/// </summary>
/// <param name="action">The action to execute.</param>
/// <returns></returns>
public virtual Task OnUIThreadAsync(Func<Task> action) {
public virtual Task OnUIThreadAsync(Func<Task> action)
{
ValidateDispatcher();
#if WINDOWS_UWP
return dispatcher.RunTaskAsync(action);
Expand All @@ -100,27 +108,32 @@ public virtual Task OnUIThreadAsync(Func<Task> action) {
/// </summary>
/// <param name="action">The action to execute.</param>
/// <exception cref="System.NotImplementedException"></exception>
public virtual void OnUIThread(System.Action action) {
public virtual void OnUIThread(System.Action action)
{
if (CheckAccess())
action();
else {
else
{
#if WINDOWS_UWP
dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => action()).AsTask().Wait();
#else
Exception exception = null;
System.Action method = () => {
try {
System.Action method = () =>
{
try
{
action();
}
catch(Exception ex) {
catch (Exception ex)
{
exception = ex;
}

Check notice

Code scanning / CodeQL

Generic catch clause Note

Generic catch clause.
};

dispatcher.Invoke(method);

if (exception != null)
throw new System.Reflection.TargetInvocationException("An error occurred while dispatching a call to the UI Thread", exception);
throw new System.Reflection.TargetInvocationException("An error occurred while dispatching a call to the UI Thread", exception);
#endif
}
}
Expand All @@ -138,7 +151,8 @@ public virtual void OnUIThread(System.Action action) {
/// The WindowManager marks that element as a framework-created element so that it can determine what it created vs. what was intended by the developer.
/// Calling GetFirstNonGeneratedView allows the framework to discover what the original element was.
/// </remarks>
public virtual object GetFirstNonGeneratedView(object view) {
public virtual object GetFirstNonGeneratedView(object view)
{
return View.GetFirstNonGeneratedView(view);
}

Expand All @@ -158,9 +172,11 @@ public virtual object GetFirstNonGeneratedView(object view) {
/// </summary>
/// <param name="view">The view.</param>
/// <param name="handler">The handler.</param>
public virtual void ExecuteOnFirstLoad(object view, Action<object> handler) {
public virtual void ExecuteOnFirstLoad(object view, Action<object> handler)
{
var element = view as FrameworkElement;
if (element != null && !(bool) element.GetValue(PreviouslyAttachedProperty)) {
if (element != null && !(bool)element.GetValue(PreviouslyAttachedProperty))
{
element.SetValue(PreviouslyAttachedProperty, true);
View.ExecuteOnLoad(element, (s, e) => handler(s));
}
Expand All @@ -171,9 +187,11 @@ public virtual void ExecuteOnFirstLoad(object view, Action<object> handler) {
/// </summary>
/// <param name="view">The view.</param>
/// <param name="handler">The handler.</param>
public virtual void ExecuteOnLayoutUpdated(object view, Action<object> handler) {
public virtual void ExecuteOnLayoutUpdated(object view, Action<object> handler)
{
var element = view as FrameworkElement;
if (element != null) {
if (element != null)
{
View.ExecuteOnLayoutUpdated(element, (s, e) => handler(s));
}
}
Expand All @@ -190,7 +208,8 @@ public virtual void ExecuteOnLayoutUpdated(object view, Action<object> handler)
/// <exception cref="System.NotImplementedException"></exception>
public virtual Func<CancellationToken, Task> GetViewCloseAction(object viewModel, ICollection<object> views, bool? dialogResult)
{
foreach (var contextualView in views) {
foreach (var contextualView in views)
{
var viewType = contextualView.GetType();
#if WINDOWS_UWP
var closeMethod = viewType.GetRuntimeMethod("Close", new Type[0]);
Expand All @@ -200,7 +219,8 @@ public virtual Func<CancellationToken, Task> GetViewCloseAction(object viewModel
var closeMethod = viewType.GetMethod("Close", new Type[0]);
#endif
if (closeMethod != null)
return ct => {
return ct =>
{
#if AVALONIA
if (dialogResult != null)
{
Expand Down Expand Up @@ -234,7 +254,8 @@ public virtual Func<CancellationToken, Task> GetViewCloseAction(object viewModel
#else
var isOpenProperty = viewType.GetProperty("IsOpen");
#endif
if (isOpenProperty != null) {
if (isOpenProperty != null)
{
return ct =>
{
isOpenProperty.SetValue(contextualView, false, null);
Expand Down

0 comments on commit 8d62fc4

Please sign in to comment.