Чтобы отобразить Alert
, введите код ниже в своем классе ViewModel
public class MainViewModel
{
public ICommand ShowAlertCommand { get; set; }
public MainViewModel()
{
ShowAlertCommand = new Command(get => MakeAlter());
}
void MakeAlter()
{
Application.Current.MainPage.DisplayAlert("Alert", "Hello", "Cancel", "ok");
}
}
Установите Command
на Button
в xaml
<StackLayout>
<Button Text="Click for alert" Command="{Binding ShowAlertCommand}"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
</StackLayout>
Установите BindingContext
в кодепозади вашего файла xaml.Если вы xaml файл MainPage.xaml
public MainPage()
{
InitializeComponent();
BindingContext = new MainViewModel();
}