xamarin.android中的页面导航,带后退按钮

时间:2018-04-24 10:16:51

标签: xamarin.android navigation

如何在xamarin.android中导航到带有后退按钮的页面。

我是xamarin.andorid的新手。

我已经完成了这个Link,但我找不到后退按钮的选项。我还需要为每个页面添加另一个需要添加活动类的查询?

2 个答案:

答案 0 :(得分:0)

你的问题不清楚,但后退按钮有一个覆盖

public override void OnBackPressed()
{
                base.OnBackPressed(); // will close open activity either closing 
the application or going to your previous activity   
}

您可以将此添加到您的活动中,然后只要按下后退按钮执行某些操作。 base.OnBackPressed();将运行默认的后退按钮行为并关闭当前活动。我经常这样做以显示关于离开页面的确认对话框或关闭背面的片段

public override void OnBackPressed()
{
    if (_isBlahFragmentOpen)
    {
        ShowActionDialogue("Close blah", "Are you sure you wish to cancel adding blah any changes made will be lost.", CloseDateFragment);
    }

    else
       base.OnBackPressed();
}

答案 1 :(得分:0)

您需要在应用程序中安装AppCompat库:

首先,您需要添加工具栏

<强> toolbar.axml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"   />

然后,只要您需要使用它,就可以像这样使用它

  <include
   android:id="@+id/toolbar"
   layout="@layout/toolbar" />

例如:

<强> some_page.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF">
<include
  android:id="@+id/toolbar"
  layout="@layout/toolbar" />
  .
  .
  .
</LinearLayout>

您可以为您的案例使用活动和片段,并非强制要求您使用活动

如果您计划使用活动,那么这样做非常简单:

  • 将活动添加到您的项目中。

  • 添加如上所示的工具栏

  • 覆盖活动中的OnCreateOptionsMenu和OnOptionsItemSelected方法

  • 要隐藏任何工具栏菜单项,您可以在OnCreateOptionsMenu中更改并单击事件,其他所有内容都可以添加到OnOptionsItemSelected

  • 为了正确理解检查this,您可以找到一个回答大部分问题的示例herehere

古德勒克!

在查询时还原

相关问题