Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 代码隐藏中的Xamarin表单单击事件不等待视图模型中的异步确认_C#_Xamarin_Mvvm_Xamarin.forms - Fatal编程技术网

C# 代码隐藏中的Xamarin表单单击事件不等待视图模型中的异步确认

C# 代码隐藏中的Xamarin表单单击事件不等待视图模型中的异步确认,c#,xamarin,mvvm,xamarin.forms,C#,Xamarin,Mvvm,Xamarin.forms,我在一个页面上有一个取消按钮,该页面在ViewModel中绑定了Command: private async void ExecuteCanceledCommand() { bool confirmResult = await Application.Current.MainPage.DisplayAlert("Cancel", "Are you sure you want to cancel?", "OK",

我在一个页面上有一个取消按钮,该页面在
ViewModel
中绑定了
Command

private async void ExecuteCanceledCommand()
{
    bool confirmResult = await Application.Current.MainPage.DisplayAlert("Cancel",
                "Are you sure you want to cancel?",
                "OK",
                "Cancel");

    if (confirmResult)
    {
        // do stuff in viewmodel
    }
        else return;
    }
}
然后,
XAML
code-behind中单击绑定到此属性的
属性:

public void OnCanceledConfirmed()
{
    // Do stuff in code behind
}

如果用户从
ViewModel
触发的DisplayAlert模式中选择OK,我只想在
onCanceledConfirmmed()
方法中执行一些操作。当从代码背后单击ok按钮时,是否有方法执行此操作或触发此操作

我通过将“模式确认”对话框移到“代码隐藏”而不是“视图模型”来解决这个问题。这允许我在代码隐藏中执行我想要的操作,然后我可以从那里调用ViewModel中我想要的方法。

单击
OK
时,为什么要调用
OnCancled
?为button对象创建一个事件方法/处理程序,并调用所需的内容。为了清晰起见,我进行了更新。我想根据用户是否在viewmodel的弹出窗口中单击“OK”,对代码进行一些更改。