Просмотр MainPage (MainPage.xaml)
Код: Выделить всё
Код: Выделить всё
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new MainPageViewModel();
}
}
Код: Выделить всё
public class MainPageViewModel : ObservableObject
{
public ICommand ProcessNewScoreCommand { get; }
public MainPageViewModel()
{
ProcessNewScoreCommand = new Command(DoSomething);
}
private void DoSomething()
{
// Do something
}
}
Код: Выделить всё
public partial class MyCustomControl : ContentView
{
public static readonly BindableProperty DoSomethingProperty =
BindableProperty.Create(nameof(DoSomething), typeof(ICommand), typeof(MyCustomControl));
public ICommand DoSomething
{
get => (ICommand)GetValue(DoSomethingProperty);
set => SetValue(DoSomethingProperty, value);
}
public MyCustomControl()
{
InitializeComponent();
BindingContext = new MyCustomControlViewModel(DoSomething);
}
}
Код: Выделить всё
public class MyCustomControlViewModel : ObservableObject
{
public ICommand DoSomething { get; }
public MyCustomControlViewModel(ICommand doSomethingCommand)
{
DoSomething = doSomethingCommand;
}
private void PerformSomeLogic()
{
// Any calulations/logic
// ...
if (DoSomething.CanExecute(null))
{
// Execute command, so that parent component gets informed and can act.
DoSomething.Execute(null);
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/732 ... m-net-maui