- при нажатии кнопки Ping затем к сообщению добавляется звездочка *..
- если нажата кнопка Pong, то последний символ в сообщении будет удалено.
Код: Выделить всё
public partial class PingPongViewModel : ObservableObject
{
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(PongCommand))]
string _message = string.Empty;
bool IsMessageNotEmpty => Message != string.Empty;
[RelayCommand]
void Ping() => Message += "*";
[RelayCommand(CanExecute = nameof(IsMessageNotEmpty))]
void Pong() => Message = Message.Substring(0, Message.Length - 1);
}
Код: Выделить всё
public partial class PingPongView : ContentView
{
public static readonly BindableProperty MessageProperty =
BindableProperty.Create(
propertyName: nameof(Message),
returnType: typeof(string),
declaringType: typeof(PingPongView),
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: OnMessagePropertyChanged);
public string Message
{
get => (string)GetValue(MessageProperty);
set => SetValue(MessageProperty, value);
}
static void OnMessagePropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var view = (PingPongView)bindable;
view._model.Message = (string)newValue;
}
PingPongViewModel _model;
public PingPongView()
{
InitializeComponent();
_model = new PingPongViewModel();
BindingContext = _model;
}
}
Код: Выделить всё
Код: Выделить всё
Код: Выделить всё
Подробнее здесь: https://stackoverflow.com/questions/790 ... er-control