Elgate
Elgate

Reputation: 113

How to send a display alert in the opening activity of a xamarin forms project

I've been working for about a year now on a Xamarin.forms app for my job. I took over from the previous guy with little to no knowledge of the xamarin.forms framework.

I've had a few hiccups, but I generaly manage to get by.

But here I got stuck on something that should be basic. The app is in two part, one being mostly for starting up things (never touched it, since it doesn't really change the function of the app) while the other is the nitty gritty of if.

Well now I'm trying to ge in app update from app center. And to be able to have user download in app I need to make a custom update message. Fine, I thought but here's the issue: I can't manage to get it to use the displayalert function.

Here's the part where I'm working on:

using System;

using Android.Support.V4.App;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Timers;
using Picking.Droid;
using Picking.Droid.Helpers;
using Xamarin.Essentials;
using Plugin.CurrentActivity;
using Android.Support.V4.Content;
using Android;
using Microsoft.AppCenter;
using Microsoft.AppCenter.Analytics;
using Microsoft.AppCenter.Crashes;
using Microsoft.AppCenter.Distribute;
using Android.Content;
using SharpCifs.Smb;
using System.Threading.Tasks;

protected override void OnCreate(Bundle savedInstanceState)
        {
            Distribute.SetEnabledForDebuggableBuild(true);
            Distribute.ReleaseAvailable = OnReleaseAvailable;
            AppCenter.Start("android=[top Secret Code]",
                   typeof(Analytics), typeof(Crashes), typeof(Distribute));
            Distribute.CheckForUpdate();
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
            CrossCurrentActivity.Current.Init(this, savedInstanceState);
            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        }

bool OnReleaseAvailable(ReleaseDetails releaseDetails)
        {
            // Look at releaseDetails public properties to get version information, release notes text or release notes URL
            string versionName = releaseDetails.ShortVersion;
            string versionCodeOrBuildNumber = releaseDetails.Version;
            string releaseNotes = releaseDetails.ReleaseNotes;
            Uri releaseNotesUrl = releaseDetails.ReleaseNotesUrl;

            // custom dialog
            var title = "Version " + versionName + " available!";
            Task answer;

            // On mandatory update, user can't postpone
            if (releaseDetails.MandatoryUpdate)
            {
                answer = DisplayAlert(title, releaseNotes, "Download and Install");
            }
            else
            {
                answer = Current.MainPage.DisplayAlert(title, releaseNotes, "Download and Install", "Maybe tomorrow...");
            }
            answer.ContinueWith((task) =>
            {
                // If mandatory or if answer was positive
                if (releaseDetails.MandatoryUpdate || (task as Task<bool>).Result)
                {
                    // Notify SDK that user selected update
                    Distribute.NotifyUpdateAction(UpdateAction.Update);
                }
                else
                {
                    // Notify SDK that user selected postpone (for 1 day)
                    // This method call is ignored by the SDK if the update is mandatory
                    Distribute.NotifyUpdateAction(UpdateAction.Postpone);
                }
            });

            // Return true if you're using your own dialog, false otherwise
            return true;
        }

My only issue is on the last part of the app, in the "OnReleaseAvailable" I couldn't find the displayAlert for that part. I tried referencing current, mainpage, activity, but nothing.

Thanks for the help.

enter image description here

For clarity: Red is the xamarinforms I usually work with, blue is where I'm working now, which basically just does basic startup then let xamarin forms handle the rest. I'm in MainActivity.cs

Upvotes: 1

Views: 1262

Answers (1)

Jason
Jason

Reputation: 89102

DisplayAlert is Page method so it only works in context of a XF Page. You are trying to execute it from the Android MainActivity. You can try to tie into App.Current.MainPage to get the correct context

Upvotes: 2

Related Questions