Code: Select all
public partial class ItemViewModel : ObservableObject
{
[ObservableProperty]
double _quantity;
[ObservableProperty]
double _price;
[ObservableProperty]
string _selectedProperty;
[RelayCommand]
void SetSelectedProperty(string selectedProperty)
=> SelectedProperty = selectedProperty;
}
Code: Select all
public partial class Numpad : ContentView
{
double? _value;
public static readonly BindableProperty TextProperty =
BindableProperty.Create(
propertyName: nameof(Text),
returnType: typeof(string),
defaultValue: "0",
defaultBindingMode: BindingMode.TwoWay,
declaringType: typeof(Numpad),
propertyChanged: (bindable, oldValue, newValue) =>
{
var @this = (Numpad)bindable;
((Command)@this.UpCommand).ChangeCanExecute();
((Command)@this.DownCommand).ChangeCanExecute();
});
public string Text
{
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
public ICommand UpCommand { get; }
public ICommand DownCommand { get; }
public Numpad()
{
UpCommand = new Command(
execute: () =>
{
Text = $"{_value + 1}";
},
canExecute: () =>
{
if (double.TryParse(Text, out double x))
{
_value = x;
return true;
}
_value = null;
return false;
}
);
DownCommand = new Command(
execute: () =>
{
Text = $"{_value - 1}";
},
canExecute: () =>
{
if (double.TryParse(Text, out double x))
{
_value = x;
return _value > 0;
}
_value = null;
return false;
}
);
InitializeComponent();
}
}
Code: Select all
Code: Select all
public MainPage()
{
InitializeComponent();
BindingContext = new ItemViewModel { Quantity = 3, Price = 5 };
}
Code: Select all
- Das Klicken auf eine der ersten beiden Schaltflächen sollte den Text des Numpads binden< /code> entweder auf Menge oder Einzelpreis umstellen.
- Wenn Sie auf eine beliebige Schaltfläche im Numpad klicken, sollte sich die Eigenschaft Text der ausgewählten Schaltfläche ändern der ersten beiden Tasten.
Was ist der Übeltäter?