Nu är det ordning på funktionaliteten när det gäller att hantera spelare nya spelronder och den poänghistorik som lagras
This commit is contained in:
38
GreadyPoang.Common/Specifics/DigitsOnlyBehavior.cs
Normal file
38
GreadyPoang.Common/Specifics/DigitsOnlyBehavior.cs
Normal file
@ -0,0 +1,38 @@
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace GreadyPoang.Common;
|
||||
|
||||
public class DigitsOnlyBehavior : Behavior<Entry>
|
||||
{
|
||||
protected override void OnAttachedTo(Entry entry)
|
||||
{
|
||||
entry.TextChanged += OnTextChanged;
|
||||
base.OnAttachedTo(entry);
|
||||
}
|
||||
|
||||
protected override void OnDetachingFrom(Entry entry)
|
||||
{
|
||||
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.
|
||||
*
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
50
GreadyPoang.Common/Specifics/EventToCommandBehavior.cs
Normal file
50
GreadyPoang.Common/Specifics/EventToCommandBehavior.cs
Normal file
@ -0,0 +1,50 @@
|
||||
using System.Reflection;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace GreadyPoang.Common;
|
||||
public class EventToCommandBehavior : BehaviorBase<VisualElement>
|
||||
{
|
||||
public 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);
|
||||
}
|
||||
|
||||
private EventInfo eventInfo;
|
||||
private Delegate eventHandler;
|
||||
|
||||
protected override void OnAttachedTo(VisualElement bindable)
|
||||
{
|
||||
base.OnAttachedTo(bindable);
|
||||
|
||||
if (bindable == null || string.IsNullOrEmpty(EventName))
|
||||
return;
|
||||
|
||||
eventInfo = bindable.GetType().GetRuntimeEvent(EventName);
|
||||
if (eventInfo == 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);
|
||||
}
|
||||
|
||||
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