Xamarin Forms DisplayAlert():为什么WinPhone和Android中的参数数量不同

Xamarin Forms DisplayAlert():为什么WinPhone和Android中的参数数量不同,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我想在我的ContentPage中使用DisplayAlert来显示一条简单的消息,通过单击OK按钮进行确认,这样我的第三个参数就可以了。这意味着:第二个按钮不需要第四个参数。现在我发现Windows Phone和Android之间存在差异: 在Android中使用null会在仅使用三个时引发异常 争论是有效的。 使用WinPhone的三个参数给出 编译时错误“DisplayAlert”方法没有重载需要3 论据。 我想知道:我是否只需要编写DependencyService来显示一个只需一个按钮

我想在我的ContentPage中使用DisplayAlert来显示一条简单的消息,通过单击OK按钮进行确认,这样我的第三个参数就可以了。这意味着:第二个按钮不需要第四个参数。现在我发现Windows Phone和Android之间存在差异:

在Android中使用null会在仅使用三个时引发异常 争论是有效的。 使用WinPhone的三个参数给出 编译时错误“DisplayAlert”方法没有重载需要3 论据。 我想知道:我是否只需要编写DependencyService来显示一个只需一个按钮的警报

WinPhone运行时版本:v4.0.30319 Android支持版本:v5

更新Dylan的评论:

async void OnMyEvent(object sender, EventArgs args)
{
    await DisplayAlert("Message 1", "Hello Android!", "OK"); // <- works on Android but doesn't compile on WinPhone
    await DisplayAlert("Message 2", "Hello WinPhone!", "OK", null); // <- works on WinPhone but throws a null pointer exception on Android
}

这是我目前的解决方法:

#if __ANDROID__
            await DisplayAlert("Message 1", "Hello Android!", "OK");
#else
            await DisplayAlert("Message 2", "Hello WinPhone!", "OK", null);
#endif

也许有更好的解决方案…

这是我目前的解决方案:

#if __ANDROID__
            await DisplayAlert("Message 1", "Hello Android!", "OK");
#else
            await DisplayAlert("Message 2", "Hello WinPhone!", "OK", null);
#endif

也许有更好的…

您可以将“确定”按钮作为“取消”按钮,如下所示:

var result = await DisplayAlert("Message", "Hello there!", null, "OK");
这将显示一个只有OK按钮的警报框。然后检查结果是否为假。如果是,则表示单击了“确定”按钮

例如


您可以将“确定”按钮作为“取消”按钮,如下所示:

var result = await DisplayAlert("Message", "Hello there!", null, "OK");
这将显示一个只有OK按钮的警报框。然后检查结果是否为假。如果是,则表示单击了“确定”按钮

例如


您可以发布调用DisplayAlert的代码吗?您是从PCL还是SAP使用它?如果你使用的是Android和Windows Phone,我不确定你怎么会得到不同的编译结果。@Dylan:Done。我正在使用一个共享项目。不确定它在PCL中的行为。能否发布调用DisplayAlert的代码?您是从PCL还是SAP使用它?如果你使用的是Android和Windows Phone,我不确定你怎么会得到不同的编译结果。@Dylan:Done。我正在使用一个共享项目。不确定它在PCL中的行为。