diff --git a/Plugin.Maui.AppRating/IAppRating.cs b/Plugin.Maui.AppRating/IAppRating.cs index 79759a0..022fd8a 100644 --- a/Plugin.Maui.AppRating/IAppRating.cs +++ b/Plugin.Maui.AppRating/IAppRating.cs @@ -10,7 +10,8 @@ public interface IAppRating /// /// Perform rating on the current OS store app or open the store page on browser. /// - /// Use this for Android. - /// Use this for iOS. - Task PerformRatingOnStoreAsync(string packageName = "", string applicationId = ""); + /// Use packageName for Android. + /// Use applicationId for iOS. + /// Use productId for Windows + Task PerformRatingOnStoreAsync(string packageName = "", string applicationId = "", string productId = ""); } \ No newline at end of file diff --git a/Plugin.Maui.AppRating/Platforms/Android/AppRating.android.cs b/Plugin.Maui.AppRating/Platforms/Android/AppRating.android.cs index 7d935a5..56b7985 100644 --- a/Plugin.Maui.AppRating/Platforms/Android/AppRating.android.cs +++ b/Plugin.Maui.AppRating/Platforms/Android/AppRating.android.cs @@ -47,9 +47,10 @@ public async Task PerformInAppRateAsync() /// /// Perform rating on the current OS store app or open the store page on browser. /// - /// Use this for Android. - /// Use this for iOS. - public Task PerformRatingOnStoreAsync(string packageName = "", string applicationId = "") + /// Use packageName for Android. + /// Use applicationId for iOS. + /// Use productId for Windows + public Task PerformRatingOnStoreAsync(string packageName = "", string applicationId = "", string productId = "") { var tcs = new TaskCompletionSource(); diff --git a/Plugin.Maui.AppRating/Platforms/Windows/AppRating.windows.cs b/Plugin.Maui.AppRating/Platforms/Windows/AppRating.windows.cs new file mode 100644 index 0000000..2b49043 --- /dev/null +++ b/Plugin.Maui.AppRating/Platforms/Windows/AppRating.windows.cs @@ -0,0 +1,87 @@ +using Microsoft.UI.Xaml.Controls; +using Windows.ApplicationModel.Core; +using Windows.Services.Store; +using Windows.UI.Core; +using Launcher = Windows.System.Launcher; + +namespace Plugin.Maui.AppRating; + +partial class AppRatingImplementation : IAppRating +{ + /// + /// Open an in-app review popup of your current application. + /// + /// To use this method the Target Version must be 10.0.17763 or above. + public async Task PerformInAppRateAsync() + { + var dispatcher = CoreApplication.MainView?.CoreWindow?.Dispatcher; + + await dispatcher?.RunAsync(CoreDispatcherPriority.Normal, async () => + { + StoreContext storeContext = StoreContext.GetDefault(); + + var result = await storeContext.RequestRateAndReviewAppAsync(); + + switch (result.Status) + { + case StoreRateAndReviewStatus.Error: + await ShowErrorMessage("ERROR", "There was an error trying to opening in-app rating."); + + break; + case StoreRateAndReviewStatus.CanceledByUser: + await ShowErrorMessage("ACTION CANCELED", "In-app rating action canceled by user."); + + break; + case StoreRateAndReviewStatus.NetworkError: + await ShowErrorMessage("ERROR", "Please check your internet connection first."); + + break; + } + }); + } + + /// + /// Perform rating on the current OS store app or open the store page on browser. + /// + /// Use packageName for Android. + /// Use applicationId for iOS. + /// Use productId for Windows + public async Task PerformRatingOnStoreAsync(string packageName = "", string applicationId = "", string productId = "") + { + if (string.IsNullOrEmpty(productId)) + { + await ShowErrorMessage("ERROR", "Please, provide the application ProductId for Microsoft Store."); + + return; + } + + try + { + await Launcher.LaunchUriAsync(new Uri($"ms-windows-store://review/?ProductId={productId}")); + } + catch (Exception) + { + await ShowErrorMessage("ERROR", "Cannot open rating because Microsoft Store was unable to launch."); + } + } + + private static async Task ShowErrorMessage(string title, string message) + { + // To create a native dialog there is an issue with the XamlRoot on WinUI, + // so we have to call the MainPage of the MAUI application and use the + // DisplayAlert method. + // In case something goes wrong, we throw an exception. + try + { + if (Application.Current != null) + { + if (Application.Current.MainPage != null) + await Application.Current.MainPage.DisplayAlert(title, message, "OK"); + } + } + catch (Exception) + { + throw; + } + } +} \ No newline at end of file diff --git a/Plugin.Maui.AppRating/Platforms/iOS/AppRating.ios.cs b/Plugin.Maui.AppRating/Platforms/iOS/AppRating.ios.cs index 61c5cfc..6c0a0b4 100644 --- a/Plugin.Maui.AppRating/Platforms/iOS/AppRating.ios.cs +++ b/Plugin.Maui.AppRating/Platforms/iOS/AppRating.ios.cs @@ -48,9 +48,10 @@ public Task PerformInAppRateAsync() /// /// Perform rating on the current OS store app or open the store page on browser. /// - /// Use this for Android. - /// Use this for iOS/macOS - public Task PerformRatingOnStoreAsync(string packageName = "", string applicationId = "") + /// Use packageName for Android. + /// Use applicationId for iOS. + /// Use productId for Windows + public Task PerformRatingOnStoreAsync(string packageName = "", string applicationId = "", string productId = "") { var tcs = new TaskCompletionSource(); diff --git a/Plugin.Maui.AppRating/Plugin.Maui.AppRating.csproj b/Plugin.Maui.AppRating/Plugin.Maui.AppRating.csproj index c0f5dca..f6811d8 100644 --- a/Plugin.Maui.AppRating/Plugin.Maui.AppRating.csproj +++ b/Plugin.Maui.AppRating/Plugin.Maui.AppRating.csproj @@ -1,8 +1,8 @@  - net7.0-android;net7.0-ios + $(TargetFrameworks);net7.0-windows10.0.19041.0 true