在非事件处理程序方法中定义时 DisplayAlert 不起作用
DisplayAlert is not working when defined in non event handler method
我正在尝试向用户显示警报对话框。我试图通过在代码隐藏 (About.xaml.cs) class 中定义一个方法并从 ViewModel(myViewModel.cs) class 调用它来实现这一点。但这是行不通的。 DisplayAlert 在其他事件处理程序方法中工作正常。
About.xaml.cs
public void RadioButton_CheckedChanged(object sender, CheckedChangedEventArgs e)
{
DisplayAlert("hello", "test", "Yes", "No"); //working
RadioButton rb = sender as RadioButton;
gender = rb.Content as string;
}
public async Task<bool> DisplayConfirmation(string title, string msg)
{
return await DisplayAlert(title, msg, "Yes", "No"); //not working
}
myViewModel.cs
async void deleteStudentRecord(int id)
{
About about = new About();
if (await about.DisplayConfirmation("Alert!", "Are you sure?"))
{
try
{
int deleted_count = await DBServices.DeleteRecord(id);
await Refresh();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
}
}
如果有人能解释这种行为并展示正确的方法,那将非常有帮助。
提前致谢!
您可以使用 App Context 使用 Like Below,
Application.Current.MainPage.DisplayAlert("hello", "test", "Yes", "No")
谢谢
我正在尝试向用户显示警报对话框。我试图通过在代码隐藏 (About.xaml.cs) class 中定义一个方法并从 ViewModel(myViewModel.cs) class 调用它来实现这一点。但这是行不通的。 DisplayAlert 在其他事件处理程序方法中工作正常。
About.xaml.cs
public void RadioButton_CheckedChanged(object sender, CheckedChangedEventArgs e)
{
DisplayAlert("hello", "test", "Yes", "No"); //working
RadioButton rb = sender as RadioButton;
gender = rb.Content as string;
}
public async Task<bool> DisplayConfirmation(string title, string msg)
{
return await DisplayAlert(title, msg, "Yes", "No"); //not working
}
myViewModel.cs
async void deleteStudentRecord(int id)
{
About about = new About();
if (await about.DisplayConfirmation("Alert!", "Are you sure?"))
{
try
{
int deleted_count = await DBServices.DeleteRecord(id);
await Refresh();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
}
}
如果有人能解释这种行为并展示正确的方法,那将非常有帮助。 提前致谢!
您可以使用 App Context 使用 Like Below,
Application.Current.MainPage.DisplayAlert("hello", "test", "Yes", "No")
谢谢