Xamarin.Forms:在Android 7.0及更高版本上未调用OnCreateOptionsMenu方法

时间:2019-04-25 07:05:17

标签: xamarin xamarin.forms searchview android-7.0-nougat

我正在尝试在Android的xamarin.forms项目中的导航栏上显示搜索视图。但是搜索工具栏项(搜索图标)在android 7.0及更高版本上不可见,在android 6.0上运行良好。

我已点击this链接来为内容页面编写自定义渲染器。它在android 6.0上工作正常。

在进一步搜索中,我得到了this链接,如果我创建xamarin.android项目,则该链接工作正常,但在xamarin.forms上却无效。

这是我的MainActivity代码:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    public static Android.Support.V7.Widget.Toolbar ToolBar { get; private set; }

    protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);

        global::Xamarin.Forms.Forms.Init(this, bundle);

        LoadApplication(new App());
    }

    public override bool OnCreateOptionsMenu(IMenu menu)
    {
        ToolBar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
        return base.OnCreateOptionsMenu(menu);
    }
}

1 个答案:

答案 0 :(得分:1)

不知道为什么此自定义方法在以后的版本中不再起作用。但是在Xamarin表单中,有一种简单的方法可以在 NavigationBar 中显示 SearchBar 也许您可以尝试一下。

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Samples.Views.MainPage" Title="Samples">
    <NavigationPage.TitleView>
        <SearchBar Text="input here"></SearchBar>
    </NavigationPage.TitleView>

    <StackLayout>
        <Button Text="SearchPage Sample" VerticalOptions="Center" Clicked="SearchPageSample_Clicked"/>
    </StackLayout>
</ContentPage>

enter image description here