Quantcast
Viewing all articles
Browse latest Browse all 91519

How to add a ToolbarItem on one platform (Android), but not on the other (iOS)?

Hi there

I have a Xamarin.Forms App for Android and iOS. On one of my Pages I like to add some secondary ToolbarItems for the Android version. I don't want to add them into the Toolbar of an iOS app cause the Xamarin Secondary Toolbar for iOS does not work with the new App Shell.

But with the following XAML I get a NullReferenceException in Page.cs OnToolbarItemsCollectionChanged when I navigate to the page on iOS.

<ContentPage.ToolbarItems>
    <OnPlatform x:TypeArguments="ToolbarItem">
        <On Platform="Android">
            <ToolbarItem Command="{Binding LaunchRocketCommand}" Text="Launch Rocket" Order="Secondary" Priority="3"/>
        </On>
    </OnPlatform>
</ContentPage.ToolbarItems>

Digging into Page.cs I see that the error occurs when the OnToolbarItemsCollectionChanged wants to set item.Parent but I assume item is null cause I did not specify for iOS. Can I somehow manipulate the args.Action? Is it a bug? Does anyone have an idea for a workaround?

    void OnToolbarItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
    {
        if (args.Action != NotifyCollectionChangedAction.Add)
            return;
        foreach (IElement item in args.NewItems)
            item.Parent = this;
    }

Xamarin.Forms: 4.3.0.947036 (latest stable)

Cheers


Viewing all articles
Browse latest Browse all 91519

Trending Articles