Create ToolbarItems using template or some other dynamic mechanism.

N_BauaN_Baua INMember ✭✭✭✭✭

Hi guys,

I have few common toolbar items which I want to add on every view of the application.
(Though there are some specific items per view, however let's not worry about them right now.)

What I want is to add these common toolbar items via a template or external file/code in XF - PCL.

I tried the following code which gets compiled without error, however crashes, I am not sure how I can overcome this issue.

MainPage.xaml: (Note the xmlns:mnu)

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns=""
    <StackLayout HorizontalOptions="Center" VerticalOptions="Center">


<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="" 
    xmlns:x="" x:Class="Toolbarz.Views.ToolBarMenu">
        <ToolbarItem Text="Help" Priority="0" Order="Secondary"/>
        <ToolbarItem Text="License" Priority="0" Order="Secondary"/>
        <ToolbarItem Text="About" Priority="0" Order="Secondary"/>

Can you suggest what's wrong with this approach?
Could you please suggest any solution which will help me use the ToolbarItems using some template?

N Baua

Best Answers


  • N_BauaN_Baua INMember ✭✭✭✭✭

    @NPC , @NMackay

    I took the liberty to tag you and bother you guys again, I know you guys really think out of box, and I seek new ideas always.
    Could you please be generous with your great ideas on the concept described above.

    If you have any better suggestion, please do let me know, I will try to adapt to that change.

    N Baua

  • N_BauaN_Baua INMember ✭✭✭✭✭

    Thanks @NMackay ,

    I will check and will mark this as an answer, tomorrow, my client is just making he*l lot of changes.
    :(, wish life was smooth like a butter on bread.

    N Baua

  • NPCNPC USMember ✭✭

    So, to answer your question on the previous page about data sharing across pages. While you can declare some static variables and access them later, I'd suggest using App.Current.Properties, it is Xamarin's built-in dictionary for all primitive types. This is the easiest way to pass data between pages, and use App.Current.Properties frivolously to make sure all of these values persist.

    This will allow you to access your data AFTER the app has been off, or sleeping. The file App.xaml.cs should include the onResume method. If you make alternative constructors in your pages, you could have it launch these pages and check App.Current.Properties for those keys, and update their values thusly.

    I honestly didn't read your whole xaml file and try to troubleshoot it. Xaml struggles with the concept of dynamically created elements. If you'd like to have a common template, I'd suggest making these in the .xaml.cs file. Maybe just worry about defining names/properties of the page-specific items in the xaml. Then use a static class with methods to create and return your toolbar items.

    Hope this helps,

  • N_BauaN_Baua INMember ✭✭✭✭✭


    Your suggestion of use a static class with methods to create and return your toolbar items. which something I tried before, however then it does not work with MVVM (at least I do not know how to access Commands and Binding then).

    Thanks for your suggestion though.

    Your solution is great, I should have thought of the OOPs way. Sometimes I overthink and ignore the simple facts.
    Just saw the example files at home yesterday and kept wondering, how could I didn't see this. Anyways you're Xamarin God.

    N Baua

  • PriyanKaAgRawaLPriyanKaAgRawaL Member ✭✭

    @NMackay said:

    You can just create a custom common toolbar page and the other pages could inherit from that page.

    I've attached a sample that shows this concept and you can add your own extra toolbar buttons in each page if need be.

    You obviously need to wrap the contentpage in a navigation page to get the toolbar items, this approach will work fine in your Prism app.

    @NMackay My Main.xaml page already extending by TabbedPage than how can i use your example which is extending ContentPage.

  • SuatKorkmazSuatKorkmaz USMember ✭✭✭

    Sorry for bringing this back up.

    Can you put a ToolBar to a ContentView? I just need to place iOS' "Share Button" into a ContentView.

Sign In or Register to comment.