我正在使用PCL创建xamarin表单。我想在用户从移动设备硬件按后退按钮时提供一项功能,然后我想DispalyActionSheet
.我可以这样做的任何选择。下面是我的示例代码。
protected override bool OnBackButtonPressed()
{
base.OnBackButtonPressed();
//new thread
Device.BeginInvokeOnMainThread(async () =>
{
var action = await DisplayActionSheet("ActionSheet: Send to?", "Cancel", null, "Facebook", "twitter", "Instagram");
// Here i want to get action result for next step
});
return true; //
}
根据Martin Zikmund的建议进行代码更新:代码执行后在不同页面上重定向时感到困难
protected override bool OnBackButtonPressed()
{
bool returnvalue = true;
Device.BeginInvokeOnMainThread(async () =>
{
vawait DisplayActionSheet("ActionSheet: Send to?", "null", null, "Facebook", "twitter", "Instagram");
switch (action)
{
case "Facebook":
// My code
CallPage(); Here i want to redirect on different page
break;
}
});
}
public async void CallPage()
{
try
{
await RetrunToPreviousPage();
}
catch (Exception ex)
{
//throw;
}
}
public async Task<dynamic> RetrunToPreviousPage()
{
Navigation.InsertPageBefore(new InboundOrderList(), this);
await Navigation.PopAsync();
return true;
}
如果要
自己处理后退按钮,则需要删除对base.OnBackButtonPressed
的调用。
protected override bool OnBackButtonPressed()
{
Device.BeginInvokeOnMainThread(async () =>
{
var action = await DisplayActionSheet(
"ActionSheet: Send to?", "Cancel", null, "Facebook", "twitter", "Instagram");
//your logic
});
return true; //you handled the back button press
}
当您实际上想在逻辑中导航回时,您必须手动弹出堆栈的页面:
await this.Navigation.PopAsync();
更新
您仍然必须在OnBackButtonPressed
结束时返回true
以将其标记为已处理。此外,ReturnToPreviousPage
不需要任何return true;
,返回类型可以是一个简单的Task
:
protected override bool OnBackButtonPressed()
{
bool returnvalue = true;
Device.BeginInvokeOnMainThread(async () =>
{
vawait DisplayActionSheet("ActionSheet: Send to?", "null", null, "Facebook", "twitter", "Instagram");
switch (action)
{
case "Facebook":
// My code
CallPage(); Here i want to redirect on different page
break;
}
});
return true; // always return true
}
...
public async Task RetrunToPreviousPage()
{
Navigation.InsertPageBefore(new InboundOrderList(), this);
await Navigation.PopAsync();
}