[Windows 8 App]邀請使用者對應用程式評分

[Windows 8 App]邀請使用者對應用程式評分

 

這裡我是參考Meng-Ru Tsai's Blog的文章來做分享

 

有時候會發現,每次下載量都很多,可是評分總是少少的

這裡我要來分享如何讓使用者在使用您的應用程式時可以邀請他來做評分

 

首先,開啟應用程式專案,在【App.xaml.cs】的檔案下

第一步,先宣告兩個變數


sealed partial class App : Application
{
    private DispatcherTimer m_DispatcherTime;
    private StorageFolder folder = ApplicationData.Current.LocalFolder; 
    /// <summary>
    /// 初始化單一應用程式物件。這是第一行執行之撰寫程式碼,
    /// 而且其邏輯相當於 main() 或 WinMain()。
    /// </summary>
    /// .....
    /// .....
    /// .....
}

 

在宣告此行變數時


private StorageFolder folder = ApplicationData.Current.LocalFolder;

 

需要組合管理using

 


using Windows.Storage;

 

 

然後,在App()中呼叫loadInfo()方法


public App()
{
    this.InitializeComponent();
    this.Suspending += OnSuspending;

    loadInfo();

    // ......
    // ......
    // ......
}

 

再來是輸入我們剛剛呼叫loadInfo()方法的程式碼部分:


private async void loadInfo()
{
    bool isExist = true;
    try
    {
        StorageFile isExistfile = await folder.GetFileAsync("isExist.dat");
    }
    catch (FileNotFoundException ex)
    {
        isExist = false;
    }
    if (!isExist)
    {
        m_DispatcherTime = new DispatcherTimer();
        m_DispatcherTime.Interval = new TimeSpan(0, 0, 0, 5);
        m_DispatcherTime.Tick += m_DispatcherTime_Tick;
         m_DispatcherTime.Start();
    }
}

 

在上面的loadInfo()方法中,我們有使用到m_DispatcherTime_Tick()方法

這裡我們要來輸入m_DispatcherTime_Tick()方法的程式碼


private async void m_DispatcherTime_Tick(object sender, object e)
{
     var dialog = new MessageDialog("請至市集給我們評份及建議。", "評分及建議");
     dialog.Commands.Add(new UICommand("Cancle", CommandInvokedHandler, 0));
     dialog.Commands.Add(new UICommand("Go", CommandInvokedHandler, 1));
     dialog.DefaultCommandIndex = 1;
     await dialog.ShowAsync(); 
     m_DispatcherTime.Stop();
}

 

在上面的m_DispatcherTime_Tick()方法中,我們有使用到CommandInvokedHandler()方法

這裡我們要來輸入CommandInvokedHandler()方法的程式碼


private async void CommandInvokedHandler(IUICommand command)
{
   //Windows.System.Launcher.LaunchUriAsync(new Uri
   //("ms-windows-store:REVIEW?PFN=Your package family name from the manifest"));
   if (command.Id.Equals(1))
   {
      StorageFile file = 
      await folder.CreateFileAsync("isExist.dat", 
      CreationCollisionOption.ReplaceExisting);
      Windows.System.Launcher.LaunchUriAsync
      (new Uri("ms-windows-store:REVIEW?PFN=10588FrankLai.6431541A336C0_2d1acsd3vsqxp"));
   }
}

 

這裡,特別說明一下這一行程式碼


 Windows.System.Launcher.LaunchUriAsync
        (new Uri("ms-windows-store:REVIEW?PFN=10588FrankLai.6431541A336C0_2d1acsd3vsqxp"));

 

在【ms-windows-store:REVIEW?PFN=】這行的等於後面是輸入自己的應用程式套件家族名稱

327

 

其實,邀請使用者評分的關鍵在於,Windows 作業系統預設的【Protocol Activation】

我們可以把

ms-windows-store:REVIEW?PFN=10588FrankLai.6431541A336C0_2d1acsd3vsqxp

貼上瀏覽器後就會跳到該應用程式的評分

 

這是【App.xaml.cs】的完整程式碼:


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.ApplicationSettings;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// 空白應用程式範本已記錄在 http://go.microsoft.com/fwlink/?LinkId=234227

namespace 日本關東景點
{
    /// <summary>
    /// 提供應用程式專屬行為以補充預設的應用程式類別。
    /// </summary>
    sealed partial class App : Application
    {
        /// <summary>
        /// 初始化單一應用程式物件。這是第一行執行之撰寫程式碼,
        /// 而且其邏輯相當於 main() 或 WinMain()。
        /// </summary>
        /// 
        private DispatcherTimer m_DispatcherTime;
        private StorageFolder folder = ApplicationData.Current.LocalFolder; 
        public App()
        {
            this.InitializeComponent();
            this.Suspending += OnSuspending;
            loadInfo();

         }

        private async void loadInfo()
        {
            bool isExist = true;
            try
            {
                StorageFile isExistfile = await folder.GetFileAsync("isExist.dat");
            }
            catch (FileNotFoundException ex)
            {
                isExist = false;
            }
            if (!isExist)
            {
                m_DispatcherTime = new DispatcherTimer();
                m_DispatcherTime.Interval = new TimeSpan(0, 0, 0, 5);
                m_DispatcherTime.Tick += m_DispatcherTime_Tick;
                m_DispatcherTime.Start();
            }
        }

        private async void m_DispatcherTime_Tick(object sender, object e)
        {
            var dialog = new MessageDialog("請至市集給我們評份及建議。", "評分及建議");
            dialog.Commands.Add(new UICommand("Cancle", CommandInvokedHandler, 0));
            dialog.Commands.Add(new UICommand("Go", CommandInvokedHandler, 1));
            dialog.DefaultCommandIndex = 1;
            await dialog.ShowAsync();
            m_DispatcherTime.Stop();
        }

        private async void CommandInvokedHandler(IUICommand command)
        {
            Windows.System.Launcher.LaunchUriAsync(new Uri
            ("ms-windows-store:REVIEW?PFN=Your package family name from the manifest"));
            if (command.Id.Equals(1))
            {
                StorageFile file =
                await folder.CreateFileAsync("isExist.dat",
                CreationCollisionOption.ReplaceExisting);
                Windows.System.Launcher.LaunchUriAsync
                  (new Uri("ms-windows-store:REVIEW?PFN=10588FrankLai.37627409A0294_2d1acsd3vsqxp"));
            }
        }

        public static 景點[] Place { get; set; }

        /// <summary>
        /// 在應用程式由使用者正常啟動時叫用。其他進入點
        /// 將在啟動應用程式以開啟特定檔案時使用,以顯示
        /// 搜尋結果等。
        /// </summary>
        /// <param name="args">關於啟動要求和處理序的詳細資料。</param>
        protected override void OnLaunched(LaunchActivatedEventArgs args)
        {
            Frame rootFrame = Window.Current.Content as Frame;

            // 當視窗已經有內容時,不重複應用程式初始化,
            // 只確定視窗是作用中
            if (rootFrame == null)
            {
                // 建立框架做為巡覽內容,並巡覽至第一頁
                rootFrame = new Frame();

                if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    //TODO: 從之前暫停的應用程式載入狀態
                }

                // 將框架放在目前視窗中
                Window.Current.Content = rootFrame;
            }

            if (rootFrame.Content == null)
            {
                // 在巡覽堆疊未還原時,巡覽至第一頁,
                // 設定新的頁面,方式是透過傳遞必要資訊做為巡覽
                // 參數
                if (!rootFrame.Navigate(typeof(MainPage), args.Arguments))
                {
                    throw new Exception("Failed to create initial page");
                }
            }
            // 確定目前視窗是作用中
            Window.Current.Activate();
            SettingsPane.GetForCurrentView().CommandsRequested += OnCommandsRequested;
        }

        /// <summary>
        /// 在應用程式暫停執行時叫用。應用程式狀態會儲存起來,
        /// 但不知道應用程式即將結束或繼續,而且仍將記憶體
        /// 的內容保持不變。
        /// </summary>
        /// <param name="sender">暫停之要求的來源。</param>
        /// <param name="e">有關暫停之要求的詳細資料。</param>
        private void OnSuspending(object sender, SuspendingEventArgs e)
        {
            var deferral = e.SuspendingOperation.GetDeferral();
            //TODO: 儲存應用程式狀態,並停止任何背景活動
            deferral.Complete();
        }


        protected override void OnSearchActivated(Windows.ApplicationModel.Activation.SearchActivatedEventArgs args)
        {
            string keyWords = args.QueryText;
            Frame frameRoot = Window.Current.Content as Frame;
            if (frameRoot == null)
            {
                frameRoot = new Frame();
                Window.Current.Content = frameRoot;
            }
            frameRoot.Navigate(typeof(searchPage));
            searchPage spg = frameRoot.Content as searchPage;
            if (spg != null)
            {
                spg.SetSearchResult(keyWords);
            }
            Window.Current.Activate();
            base.OnSearchActivated(args);
        }
        private void OnCommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)
        {
            UICommandInvokedHandler handler =
                     new UICommandInvokedHandler(onSettingsCommand);

            SettingsCommand privacy1Command =
                     new SettingsCommand("privacystatementPage", "隱私權原則", handler);

            args.Request.ApplicationCommands.Add(privacy1Command);
        }
        async void onSettingsCommand(IUICommand command)
        {
            SettingsCommand settingsCommand = (SettingsCommand)command;
            if (settingsCommand.Id.ToString().Equals("privacystatementPage"))
            {
                var success =
                     await Windows.System.Launcher.LaunchUriAsync(
                             new Uri(@"http://frank12690.pixnet.net/blog/post/48740923"));
            }
        }
    }
}

 

執行畫面如下:

328

329

 

 

參考資料來源:

邀請使用者對你的 Windows Store App 評分 (App review & rating)

如何啟動 URI 的預設應用程式 (使用 C#/VB/C++ 和 XAML 的 Windows 市集應用程式)