Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Dynamically Add ToolbarItems from a button tap

$
0
0

I have a page where I need to display toolbar items only when a button on the screen has been tapped. So far I have the following methods:-

private async Task ClearToolBarItems()
        {
            var page = Application.Current?.MainPage as Page;
            Device.BeginInvokeOnMainThread(() =>
            {
                if (page != null)
                {
                    page.ToolbarItems.Clear();
                }
            });
        }

        private async Task AddToolBarItems()
        {
            await ClearToolBarItems();

            if (JobDetails.Status == JobStatus.InProgress || JobDetails.Status == JobStatus.Accepted)
            {
                var master = Application.Current?.MainPage as MasterDetailPage;
                if (master == null) return;

                var page = master.Detail;
                var plan = new ToolbarItem("Plan", "", async () => await NavigateToPlan(JobDetails.Id),
                    ToolbarItemOrder.Secondary);
                var schedule = new ToolbarItem("Schedule", "", async () => await NavigateToSchedule(JobDetails.Id),
                    ToolbarItemOrder.Secondary);
                var elevation = new ToolbarItem("Elevation", "", async () => await NavigateToElevation(JobDetails.Id),
                    ToolbarItemOrder.Secondary);
                var email = new ToolbarItem("Send to Email", "", async () => await SendEmailToDefaultAddress(),
                    ToolbarItemOrder.Secondary);
                var abort = new ToolbarItem("Abort Job", "", async () => await AbortJobAsync(JobDetails.Id),
                    ToolbarItemOrder.Secondary);
                await Task.Run(() =>
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        page.ToolbarItems.Add(plan);
                        page.ToolbarItems.Add(schedule);
                        page.ToolbarItems.Add(elevation);
                        page.ToolbarItems.Add(email);
                        if (JobDetails.Status == JobStatus.InProgress)
                        {
                            page.ToolbarItems.Add(abort);
                        }
                    });
                });
            }
        }

The method of the button tap just calls await AddToolBarItems();

This is working great on Android, but for some reason on iOS the toolbar items do not appear. Am I missing something specific to iOS?

NB I'm following the MVVM pattern, Xamarin Forms 3.5 and .NET Standard shared code.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>