Add project files.
This commit is contained in:
30
Gready_Poang.Common/Base/BehaviorBase.cs
Normal file
30
Gready_Poang.Common/Base/BehaviorBase.cs
Normal file
@ -0,0 +1,30 @@
|
||||
namespace GreadyPoang.Common;
|
||||
|
||||
public class BehaviorBase<T> : Behavior<T> where T : BindableObject
|
||||
{
|
||||
public T? AssociatedObject { get; private set; }
|
||||
|
||||
protected override void OnAttachedTo(T bindable)
|
||||
{
|
||||
base.OnAttachedTo(bindable);
|
||||
AssociatedObject = bindable;
|
||||
|
||||
if (bindable.BindingContext != null)
|
||||
BindingContext = bindable.BindingContext;
|
||||
|
||||
bindable.BindingContextChanged += OnBindingContextChanged;
|
||||
}
|
||||
|
||||
protected override void OnDetachingFrom(T bindable)
|
||||
{
|
||||
base.OnDetachingFrom(bindable);
|
||||
bindable.BindingContextChanged -= OnBindingContextChanged;
|
||||
AssociatedObject = null;
|
||||
}
|
||||
|
||||
void OnBindingContextChanged(object? sender, EventArgs e)
|
||||
{
|
||||
if (AssociatedObject != null)
|
||||
BindingContext = AssociatedObject.BindingContext;
|
||||
}
|
||||
}
|
||||
19
Gready_Poang.Common/Gready_Poang.Common.csproj
Normal file
19
Gready_Poang.Common/Gready_Poang.Common.csproj
Normal file
@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>
|
||||
net9.0-android;
|
||||
net9.0-ios;
|
||||
net9.0-maccatalyst;
|
||||
net9.0-windows10.0.19041
|
||||
</TargetFrameworks>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Maui.Controls" Version="9.0.100" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
</Project>
|
||||
44
Gready_Poang.Common/Specifics/DigitsOnlyBehavior.cs
Normal file
44
Gready_Poang.Common/Specifics/DigitsOnlyBehavior.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace GreadyPoang.Common;
|
||||
|
||||
public class DigitsOnlyBehavior : Behavior<Entry?>
|
||||
{
|
||||
protected override void OnAttachedTo(Entry? entry)
|
||||
{
|
||||
if (entry != null)
|
||||
{
|
||||
entry.TextChanged += OnTextChanged;
|
||||
}
|
||||
base.OnAttachedTo(entry);
|
||||
}
|
||||
|
||||
protected override void OnDetachingFrom(Entry? entry)
|
||||
{
|
||||
if (entry != null)
|
||||
{
|
||||
entry.TextChanged -= OnTextChanged;
|
||||
}
|
||||
base.OnDetachingFrom(entry);
|
||||
}
|
||||
|
||||
private void OnTextChanged(object? sender, TextChangedEventArgs e)
|
||||
{
|
||||
var entry = sender as Entry;
|
||||
if (entry == null) return;
|
||||
|
||||
// Tillåt endast siffror (0–9)
|
||||
if (!Regex.IsMatch(e.NewTextValue, @"^\d*$"))
|
||||
{
|
||||
entry.Text = e.OldTextValue; // Återställ till tidigare giltigt värde
|
||||
}
|
||||
/*
|
||||
* Vill du tillåta decimaler? Ändra regex till @"^\d*\.?\d*$"
|
||||
Vill du tillåta negativa tal? Använd @"^-?\d*$"
|
||||
Vill du visa en varning när användaren skriver fel?
|
||||
Lägg till en BindableProperty för IsValid och bind den till UI.
|
||||
*
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
59
Gready_Poang.Common/Specifics/EventToCommandBehavior.cs
Normal file
59
Gready_Poang.Common/Specifics/EventToCommandBehavior.cs
Normal file
@ -0,0 +1,59 @@
|
||||
using System.Reflection;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace GreadyPoang.Common;
|
||||
public class EventToCommandBehavior : BehaviorBase<VisualElement>
|
||||
{
|
||||
public required string EventName { get; set; }
|
||||
|
||||
public static readonly BindableProperty CommandProperty =
|
||||
BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(EventToCommandBehavior));
|
||||
|
||||
public ICommand Command
|
||||
{
|
||||
get => (ICommand)GetValue(CommandProperty);
|
||||
set => SetValue(CommandProperty, value);
|
||||
}
|
||||
|
||||
// Fix for CS8618: Make the field nullable and fix IDE1006: Add '_' prefix
|
||||
private EventInfo? _eventInfo;
|
||||
private Delegate? _eventHandler;
|
||||
|
||||
protected override void OnAttachedTo(VisualElement bindable)
|
||||
{
|
||||
base.OnAttachedTo(bindable);
|
||||
|
||||
if (bindable == null || string.IsNullOrEmpty(EventName))
|
||||
return;
|
||||
|
||||
var runtimeEvent = bindable.GetType().GetRuntimeEvent(EventName);
|
||||
if (runtimeEvent == null)
|
||||
throw new ArgumentException($"Event '{EventName}' not found on type '{bindable.GetType().Name}'");
|
||||
|
||||
_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);
|
||||
}
|
||||
|
||||
private void OnEvent(object sender, object eventArgs)
|
||||
{
|
||||
if (Command?.CanExecute(eventArgs) == true)
|
||||
Command.Execute(eventArgs);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user