Files
Common-VS2019/SacsMobileMaui/Maui/Handlers/SamFormHandler.cs
T

147 lines
5.0 KiB
C#

#if ANDROID
using Microsoft.Maui.Controls.Compatibility.Platform.Android;
#endif
using Sarand.Net.Mobile.Maui.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sarand.Net.Mobile.Maui.Handlers
{
// Ref. https://dev.to/vhugogarcia/remove-entry-and-picker-borders-in-net-maui-2pk2
// Ref. https://github.com/dotnet/maui/issues/7906
public static class SamFormHandler
{
// --- Control handler registration ---
/// <summary>
/// Apply configured native styling to Sam entry, editor, and picker controls.
/// <para>Also make background transparent.</para>
/// </summary>
public static void AssignControlHandlers()
{
// --- Entry handler ---
Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("SamEntryHandler", (handler, view) =>
{
if (view is not ISamNativeControlStyle style) return;
if (style.NativeNoBorder)
{
#if ANDROID
handler.PlatformView.BackgroundTintList = Android.Content.Res.ColorStateList.ValueOf(Colors.Transparent.ToAndroid());
#elif IOS || MACCATALYST
handler.PlatformView.Layer.BorderWidth = 0;
handler.PlatformView.BorderStyle = UIKit.UITextBorderStyle.None;
#elif WINDOWS
handler.PlatformView.BorderThickness = new Microsoft.UI.Xaml.Thickness() { Bottom = 0, Top = 0, Left = 0, Right = 0, };
#endif
}
if (style.NativeNoUnderline)
{
#if ANDROID
handler.PlatformView.BackgroundTintList = Android.Content.Res.ColorStateList.ValueOf(Colors.Transparent.ToAndroid());
#elif IOS || MACCATALYST
// UITextField/UITextView do not expose a separate Android-style underline.
#elif WINDOWS
// WinUI text controls use a border, not a separate underline; use NativeNoBorder to remove it.
#endif
}
if (style.NativeBackgroundClear)
{
#if ANDROID
handler.PlatformView.Background = null;
handler.PlatformView.SetBackgroundColor(Android.Graphics.Color.Transparent);
#elif IOS || MACCATALYST
handler.PlatformView.BackgroundColor = UIKit.UIColor.Clear;
#elif WINDOWS
handler.PlatformView.Background = new Microsoft.UI.Xaml.Media.SolidColorBrush(Microsoft.UI.Colors.Transparent);
#endif
}
});
// --- Editor handler ---
Microsoft.Maui.Handlers.EditorHandler.Mapper.AppendToMapping("SamEditorHandler", (handler, view) =>
{
if (view is not ISamNativeControlStyle style) return;
if (style.NativeNoBorder)
{
#if ANDROID
handler.PlatformView.BackgroundTintList = Android.Content.Res.ColorStateList.ValueOf(Colors.Transparent.ToAndroid());
#elif IOS || MACCATALYST
handler.PlatformView.Layer.BorderWidth = 0;
#elif WINDOWS
handler.PlatformView.BorderThickness = new Microsoft.UI.Xaml.Thickness() { Bottom = 0, Top = 0, Left = 0, Right = 0, };
#endif
}
if (style.NativeNoUnderline)
{
#if ANDROID
handler.PlatformView.BackgroundTintList = Android.Content.Res.ColorStateList.ValueOf(Colors.Transparent.ToAndroid());
#elif IOS || MACCATALYST
// UITextView has no separate underline to remove.
#elif WINDOWS
// WinUI text controls use a border, not a separate underline; use NativeNoBorder to remove it.
#endif
}
if (style.NativeBackgroundClear)
{
#if ANDROID
handler.PlatformView.Background = null;
handler.PlatformView.SetBackgroundColor(Android.Graphics.Color.Transparent);
#elif IOS || MACCATALYST
handler.PlatformView.BackgroundColor = UIKit.UIColor.Clear;
#elif WINDOWS
handler.PlatformView.Background = new Microsoft.UI.Xaml.Media.SolidColorBrush(Microsoft.UI.Colors.Transparent);
#endif
}
});
// --- Picker handler ---
Microsoft.Maui.Handlers.PickerHandler.Mapper.AppendToMapping("SamPickerHandler", (handler, view) =>
{
if (view is not ISamNativeControlStyle style) return;
if (style.NativeNoBorder)
{
#if ANDROID
handler.PlatformView.BackgroundTintList = Android.Content.Res.ColorStateList.ValueOf(Colors.Transparent.ToAndroid());
#elif IOS || MACCATALYST
handler.PlatformView.Layer.BorderWidth = 0;
handler.PlatformView.BorderStyle = UIKit.UITextBorderStyle.None;
#elif WINDOWS
handler.PlatformView.BorderThickness = new Microsoft.UI.Xaml.Thickness() { Bottom = 0, Top = 0, Left = 0, Right = 0, };
#endif
}
if (style.NativeNoUnderline)
{
#if ANDROID
handler.PlatformView.BackgroundTintList = Android.Content.Res.ColorStateList.ValueOf(Colors.Transparent.ToAndroid());
#elif IOS || MACCATALYST
// The native picker has a full border, not a separate underline; use NativeNoBorder to remove it.
#elif WINDOWS
// The WinUI picker has a full border, not a separate underline; use NativeNoBorder to remove it.
#endif
}
if (style.NativeBackgroundClear)
{
#if ANDROID
handler.PlatformView.Background = null;
handler.PlatformView.SetBackgroundColor(Android.Graphics.Color.Transparent);
#elif IOS || MACCATALYST
handler.PlatformView.BackgroundColor = UIKit.UIColor.Clear;
#elif WINDOWS
handler.PlatformView.Background = new Microsoft.UI.Xaml.Media.SolidColorBrush(Microsoft.UI.Colors.Transparent);
#endif
}
});
}
// --- The End ---
}
}