This commit is contained in:
Javier Suárez Ruiz 2020-11-21 11:32:31 +01:00
Родитель 9cd42c2b39
Коммит 8d306dd7da
4 изменённых файлов: 165 добавлений и 0 удалений

Двоичные данные
MonkeyFest - .NET MAUI Handlers.pptx

Двоичный файл не отображается.

Просмотреть файл

@ -0,0 +1,95 @@
public interface IActivityIndicator : IView
{
bool IsRunning { get; }
Color Color { get; }
}
public partial class ActivityIndicator : IActivityIndicator
{
}
public class ActivityIndicator : Xamarin.Forms.View, IActivityIndicator
{
public bool IsRunning { get; set; } = true;
public Color Color { get; set; }
}
public partial class ActivityIndicatorHandler
{
public static PropertyMapper<IActivityIndicator, ActivityIndicatorHandler> ActivityIndicatorMapper = new PropertyMapper<IActivityIndicator, ActivityIndicatorHandler>(ViewHandler.ViewMapper)
{
[nameof(IActivityIndicator.IsRunning)] = MapIsRunning,
[nameof(IActivityIndicator.Color)] = MapColor
};
public static void MapIsRunning(ActivityIndicatorHandler handler, IActivityIndicator activityIndicator)
{
ViewHandler.CheckParameters(handler, activityIndicator);
handler.TypedNativeView?.UpdateIsRunning(activityIndicator);
}
public static void MapColor(ActivityIndicatorHandler handler, IActivityIndicator activityIndicator)
{
ViewHandler.CheckParameters(handler, activityIndicator);
handler.TypedNativeView?.UpdateColor(activityIndicator);
}
public ActivityIndicatorHandler() : base(ActivityIndicatorMapper)
{
}
public ActivityIndicatorHandler(PropertyMapper mapper) : base(mapper ?? ActivityIndicatorMapper)
{
}
}
public partial class ActivityIndicatorHandler : AbstractViewHandler<IActivityIndicator, ProgressBar>
{
protected override ProgressBar CreateNativeView() => new ProgressBar(Context) { Indeterminate = true };
}
public partial class ActivityIndicatorHandler : AbstractViewHandler<IActivityIndicator, UIActivityIndicatorView>
{
protected override UIActivityIndicatorView CreateNativeView() => new UIActivityIndicatorView(CGRect.Empty)
{
ActivityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
};
}
public static class ActivityIndicatorExtensions
{
public static void UpdateIsRunning(this ProgressBar progressBar, IActivityIndicator activityIndicator) =>
progressBar.Visibility = activityIndicator.IsRunning ? ViewStates.Visible : ViewStates.Invisible;
public static void UpdateColor(this ProgressBar progressBar, IActivityIndicator activityIndicator)
{
Color color = activityIndicator.Color;
if (!color.IsDefault)
progressBar.IndeterminateDrawable?.SetColorFilter(color.ToNative(), FilterMode.SrcIn);
else
progressBar.IndeterminateDrawable?.ClearColorFilter();
}
}
public static class ActivityIndicatorExtensions
{
public static void UpdateIsRunning(this UIActivityIndicatorView activityIndicatorView, IActivityIndicator activityIndicator)
{
if (activityIndicator.IsRunning)
activityIndicatorView.StartAnimating();
else
activityIndicatorView.StopAnimating();
}
public static void UpdateColor(this UIActivityIndicatorView activityIndicatorView, IActivityIndicator activityIndicator)
{
activityIndicatorView.Color = activityIndicator.Color == Color.Default ? null : activityIndicator.Color.ToNative();
}
}
RegistrarHandlers.Handlers.Register<ActivityIndicator, ActivityIndicatorHandler>();

Просмотреть файл

@ -0,0 +1,46 @@
using Xamarin.Forms;
using Xamarin.Platform;
using Xamarin.Platform.Handlers;
using RegistrarHandlers = Xamarin.Platform.Registrar;
namespace Sample
{
public class CustomButton : Button
{
public CustomButton()
{
RegistrarHandlers.Handlers.Register<CustomButton, ButtonHandler>();
ButtonHandler.ButtonMapper[nameof(IButton.BackgroundColor)] = (handler, view) =>
{
if (view is CustomButton customButton)
{
#if __IOS__
var nativeButton = handler.NativeView as UIKit.UIButton;
nativeButton.BackgroundColor = UIKit.UIColor.Red;
#endif
}
ViewHandler.MapBackgroundColor(handler, view);
};
ButtonHandler.ButtonMapper[nameof(ShadowColor)] = (handler, view) =>
{
if (view is CustomButton customButton)
{
#if __IOS__
var nativeButton = handler.NativeView as UIKit.UIButton;
nativeButton.Layer.ShadowColor = ShadowColor.ToCGColor();
nativeButton.Layer.ShadowOpacity = 1;
nativeButton.Layer.ShadowOffset = CoreGraphics.CGSize.Empty;
nativeButton.Layer.ShadowRadius = 10;
#endif
}
};
}
public Color ShadowColor { get; set; }
}
}

Просмотреть файл

@ -0,0 +1,24 @@
ButtonHandler.ButtonMapper["MyCustomProperty"] = (handler, view) =>
{
var nativeView = handler.NativeView;
};
ButtonHandler.ButtonMapper[nameof(IButton.BackgroundColor)] = (handler, view) =>
{
#if __IOS__
var nativeButton = handler.NativeView as UIKit.UIButton;
nativeButton.BackgroundColor = UIKit.UIColor.Blue;
#else
ViewHandler.MapBackgroundColor(handler, view);
#endif
};
SliderHandler.SliderMapper[nameof(ISlider.ThumbColor)] = (handler, view) =>
{
#if __IOS__
var nativeSlider = handler.NativeView as UIKit.UISlider;
nativeSlider.UpdateBackgroundColor(view);
#endif
};