Due to the problem of XF's version, the Toolbar
may not be found in OnCreateOptionsMenu
of MainActivity
, if so, you can change the code of SearchPageRenderer
in that blog for example like this:
public class SearchPageRenderer : PageRenderer
{
private Android.Support.V7.Widget.SearchView searchView;
private Android.Support.V7.Widget.Toolbar toolbar;
/// <summary>
/// Reaction on the disposing of the page.
/// </summary>
/// <param name="disposing">A value indicating whether disposing.</param>
protected override void Dispose(bool disposing)
{
if (this.searchView != null)
{
this.searchView.QueryTextChange -= this.OnQueryTextChangeSearchView;
}
this.toolbar?.Menu?.RemoveItem(Resource.Menu.mainmenu);
base.Dispose(disposing);
}
/// <summary>
/// Reaction on the element changed event.
/// </summary>
/// <param name="e">The event argument.</param>
///
protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
{
base.OnElementChanged(e);
if (e?.NewElement == null || e.OldElement != null)
{
return;
}
this.AddSearchToToolBar();
}
/// <summary>
/// Adds a search item to the toolbar.
/// </summary>
private void AddSearchToToolBar()
{
var mainactivity = Xamarin.Forms.Forms.Context as MainActivity;
this.toolbar = mainactivity.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
var bar = mainactivity.SupportActionBar;
if (toolbar != null)
{
this.toolbar.Title = this.Element.Title;
this.toolbar.InflateMenu(Resource.Menu.mainmenu);
this.searchView = this.toolbar.Menu?.FindItem(Resource.Id.action_search)?.ActionView?.JavaCast<Android.Support.V7.Widget.SearchView>();
if (this.searchView != null)
{
this.searchView.QueryTextChange += this.OnQueryTextChangeSearchView;
//Other codes goes here.
}
}
}
private void OnQueryTextChangeSearchView(object sender, Android.Support.V7.Widget.SearchView.QueryTextChangeEventArgs e)
{
//TODO:
}
}
And now you can use this SearchPageRenderer
for the Detail
of MasterDetailPage
:
<local:SearchPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:YOURNAMESPACE"
x:Class="YOURNAMESPACE.DetailPage"
Title="Detail 1">
<ContentPage.Content>
<StackLayout>
<Label Text="Welcome to Xamarin.Forms!"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
</local:SearchPage>
And you may check the result here:

By the way, I'm talking about the solution in pure XF, seems you're using Prism, not so sure what the ContentPage
is like in Prism, but I think the solution should be similar.