Uppdelningsläge

This commit is contained in:
2025-10-12 08:39:06 +02:00
parent c00819bec6
commit 02a26871c6
15 changed files with 85 additions and 30 deletions

View File

@ -2,7 +2,7 @@
public class BehaviorBase<T> : Behavior<T> where T : BindableObject
{
public T AssociatedObject { get; private set; }
public T? AssociatedObject { get; private set; }
protected override void OnAttachedTo(T bindable)
{
@ -22,8 +22,9 @@ public class BehaviorBase<T> : Behavior<T> where T : BindableObject
AssociatedObject = null;
}
void OnBindingContextChanged(object sender, EventArgs e)
void OnBindingContextChanged(object? sender, EventArgs e)
{
BindingContext = AssociatedObject.BindingContext;
if (AssociatedObject != null)
BindingContext = AssociatedObject.BindingContext;
}
}

View File

@ -1,7 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFrameworks>
net9.0-android;
net9.0-ios;
net9.0-maccatalyst;
net9.0-windows10.0.19041
</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

View File

@ -2,21 +2,27 @@
namespace GreadyPoang.Common;
public class DigitsOnlyBehavior : Behavior<Entry>
public class DigitsOnlyBehavior : Behavior<Entry?>
{
protected override void OnAttachedTo(Entry entry)
protected override void OnAttachedTo(Entry? entry)
{
entry.TextChanged += OnTextChanged;
if (entry != null)
{
entry.TextChanged += OnTextChanged;
}
base.OnAttachedTo(entry);
}
protected override void OnDetachingFrom(Entry entry)
protected override void OnDetachingFrom(Entry? entry)
{
entry.TextChanged -= OnTextChanged;
if (entry != null)
{
entry.TextChanged -= OnTextChanged;
}
base.OnDetachingFrom(entry);
}
private void OnTextChanged(object sender, TextChangedEventArgs e)
private void OnTextChanged(object? sender, TextChangedEventArgs e)
{
var entry = sender as Entry;
if (entry == null) return;

View File

@ -4,7 +4,7 @@ using System.Windows.Input;
namespace GreadyPoang.Common;
public class EventToCommandBehavior : BehaviorBase<VisualElement>
{
public string EventName { get; set; }
public required string EventName { get; set; }
public static readonly BindableProperty CommandProperty =
BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(EventToCommandBehavior));
@ -15,8 +15,9 @@ public class EventToCommandBehavior : BehaviorBase<VisualElement>
set => SetValue(CommandProperty, value);
}
private EventInfo eventInfo;
private Delegate eventHandler;
// Fix for CS8618: Make the field nullable and fix IDE1006: Add '_' prefix
private EventInfo? _eventInfo;
private Delegate? _eventHandler;
protected override void OnAttachedTo(VisualElement bindable)
{
@ -25,21 +26,29 @@ public class EventToCommandBehavior : BehaviorBase<VisualElement>
if (bindable == null || string.IsNullOrEmpty(EventName))
return;
eventInfo = bindable.GetType().GetRuntimeEvent(EventName);
if (eventInfo == null)
var runtimeEvent = bindable.GetType().GetRuntimeEvent(EventName);
if (runtimeEvent == null)
throw new ArgumentException($"Event '{EventName}' not found on type '{bindable.GetType().Name}'");
MethodInfo methodInfo = typeof(EventToCommandBehavior).GetTypeInfo().GetDeclaredMethod(nameof(OnEvent));
eventHandler = methodInfo.CreateDelegate(eventInfo.EventHandlerType, this);
eventInfo.AddEventHandler(bindable, eventHandler);
_eventInfo = runtimeEvent;
MethodInfo? methodInfo = typeof(EventToCommandBehavior).GetTypeInfo().GetDeclaredMethod(nameof(OnEvent));
if (methodInfo == null)
throw new InvalidOperationException($"Method '{nameof(OnEvent)}' not found on type '{typeof(EventToCommandBehavior).Name}'");
if (_eventInfo.EventHandlerType == null)
throw new InvalidOperationException($"EventHandlerType for event '{EventName}' is null on type '{bindable.GetType().Name}'");
_eventHandler = methodInfo.CreateDelegate(_eventInfo.EventHandlerType, this);
_eventInfo.AddEventHandler(bindable, _eventHandler);
}
protected override void OnDetachingFrom(VisualElement bindable)
{
base.OnDetachingFrom(bindable);
if (eventInfo != null && eventHandler != null)
eventInfo.RemoveEventHandler(bindable, eventHandler);
if (_eventInfo != null && _eventHandler != null)
_eventInfo.RemoveEventHandler(bindable, _eventHandler);
}
private void OnEvent(object sender, object eventArgs)