首页 > 解决方案 > App Center Distribute – Xamarin Forms 的应用内更新

问题描述

我正在阅读有关应用中心应用内更新的文档。我想尝试一下,这样每次发布我的应用程序时,我都不需要卸载我的应用程序并每次都安装新版本。文档中有一个示例代码,但我不知道将它放在哪里或它是如何工作的,文档不清楚。下面的代码是文档中的示例代码。我的问题是如何为我的应用实施应用内更新?

https://docs.microsoft.com/en-us/appcenter/sdk/distribute/xamarin

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 cannot postpone
    if (releaseDetails.MandatoryUpdate)
    {
        answer = Current.MainPage.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)
            // Note that this method call is ignored by the SDK if the update is mandatory
            Distribute.NotifyUpdateAction(UpdateAction.Postpone);
        }
    });

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

标签: xamarinxamarin.formsxamarin.androidvisual-studio-app-centerapp-update

解决方案


安卓:

您通过以下方式将回调设置为您的OnReleaseAvailable方法Distribute.ReleaseAvailable

打开 MainActivity.cs 并在 OnCreate() 方法中添加 Start() 调用

Distribute.ReleaseAvailable = OnReleaseAvailable;
AppCenter.Start(...);

IOS:

打开 AppDelegate.cs 并在 FinishedLaunching() 方法中添加 Start() 调用

Distribute.ReleaseAvailable = OnReleaseAvailable;
AppCenter.Start(...);

回复:https ://docs.microsoft.com/en-us/appcenter/sdk/distribute/xamarin#2-start-app-center-distribute


推荐阅读