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="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:mnu="clr-namespace:Toolbarz.Views;assembly=Toolbarz"
             xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="Toolbarz.Views.MainPage"
             Title="MainPage">
    <mnu:ToolBarMenu></mnu:ToolBarMenu> 
    
    <ContentPage.Content>
    <StackLayout HorizontalOptions="Center" VerticalOptions="Center">
        ....
    </StackLayout>
    </ContentPage.Content>
</ContentPage>

ToolBarMenu.xaml

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Toolbarz.Views.ToolBarMenu">
    <ContentPage.ToolbarItems>
        <ToolbarItem Text="Help" Priority="0" Order="Secondary"/>
        <ToolbarItem Text="License" Priority="0" Order="Secondary"/>
        <ToolbarItem Text="About" Priority="0" Order="Secondary"/>
    </ContentPage.ToolbarItems>
</ContentView>

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

Thanks.
N Baua

Best Answers

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.

    Thanks,
    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 ✭✭✭✭✭

    @NPC,

    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.

    @NMacky,
    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.
    Thanks,

    N Baua

  • PriyanKaAgRawaLPriyanKaAgRawaL Member ✭✭

    @NMackay said:
    @N_Baua

    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.