basquang™ on clouds

September 28, 2015

UWP: Open Store Rating to request user review and rate from your Windows 10 app

Filed under: UWP,windows 10 — basquang @ 4:32 PM
Tags: , , ,

In Windows Phone 8.x, you can use the Marketplace review task to launch the Store or Marketplace and then display the review page for the current app. The code is simple as below

MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();
marketplaceReviewTask.Show();

Unfortunately, UWP Windows 10 app does not support this kind of Marketplace review task. So the question is “How to open review page for current app to request user to rate your app on Windows 10?” Solution is, you can use Windows.System.Launcher to launch the ms-windows-store: URI scheme. The URI schema to launches the write a review experience for a product for example are:

Product ID: ms-windows-store://review/?ProductId=9WZDNCRFHVJL
Package Family Name (PFN): ms-windows-store://review/?PFN= Microsoft.Office.OneNote_8wekyb3d8bbwe

And then, in your code, using one of the uriString above

await Windows.System.Launcher.LaunchUriAsync(new Uri(uriString));

For Windows app, using the second schema uri is my recommendation, because you can get PFN dynamically from your code by using Windows.ApplicationModel.PackageId.FamilyName
but Windows.ApplicationModel.PackageId.ProductID is only available for Windows Phone app.

Hope this help!

September 26, 2013

Windows Store 8.1: Save visual element to bitmap image file

Filed under: Windows Store 8 — basquang @ 3:56 PM
Tags: , , ,

If you has already familiar with Windows Phone development. You may able to capture screen or visual element to image file using the Render method of WriteableBitmap object. When develop Windows Store app, the Render method will not exist at this time. If you are developing Windows Store 8.0 application, it will not easy for you to create a screen capture programmatically of saving visual element to image file. Fortunately with Windows Store 8.1, Microsoft provides the alternative object to do this case, that is RenderTargetBitmap object.

This method below allow you to saving visual element to file (it would saved to Local Storage or Library)

 

async Task SaveVisualElementToFile(FrameworkElement element, StorageFile file)
        {
            string fileName = "customphoto.jpg";
            var renderTargetBitmap = new RenderTargetBitmap();
            await renderTargetBitmap.RenderAsync(element, (int)element.Width, (int)element.Height);
            var pixels = await renderTargetBitmap.GetPixelsAsync();

            using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                var encoder = await
                    BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
                byte[] bytes = pixels.ToArray();
                encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                     BitmapAlphaMode.Ignore,
                                     (uint)element.Width, (uint)element.Height,
                                     96, 96, bytes);

                await encoder.FlushAsync();
            }
        }

NOTE: You cannot save visual element which in code (offscreen) to image file as mention here http://social.msdn.microsoft.com/Forums/sqlserver/en-US/257a1dda-3b0c-4eec-9b08-85c1b9cda187/rendertargetbitmap-not-working-with-simple-stack-panel

Hope this help!

Windows Store RT: Save game/application state when closing app via close gesture or Alt+F4

Filed under: Windows Store 8 — basquang @ 2:41 PM
Tags: , , ,

If you has already family with Windows Phone development. You may often saving application/game state by overriding MainPage’s OnNavigatedFrom event. But with Windows Store app (Windows RT). The event is not fire when user is closing application by “close gesture” or Alt+F4 keyboard function.

So the question is how to save application/game state in that case?

According to MSDN “When the user closes an app by pressing Alt+F4 or using the close gesture, the app is suspended for 10 seconds and then terminated. Windows notifies your app when it suspends it, but doesn’t provide additional notification when it terminates the app. That means your app must handle the suspended event and use it to save its state and release its exclusive resources and file handles immediately.”

Meaning that you should implement your code in the suspending event. So the process of saving application/game state when use close app via close gesture/Alt+F4 is:

  1. Handle Suspending event in the App.xaml.cs constructor. In the Suspending event, saving the application/game state to Application Data (ex: Windows.Storage.ApplicationData.Current.LocalFolder)
    public App()
            {
                this.InitializeComponent();
                this.Suspending += OnSuspending;
            }
    
    private async void OnSuspending(object sender, SuspendingEventArgs e)
            {
                var deferral = e.SuspendingOperation.GetDeferral();
    
                //TODO: Save application state and stop any background activity
                await SuspensionManager.SaveAsync();
    
                await SaveGameState();
                await UpdateTile();
    
                deferral.Complete();
            }
    
     async System.Threading.Tasks.Task SaveGameState()
            {
            //Save application or game state to Windows.Storage.ApplicationData.Current.LocalFolder here
            }
    
  2. In the MainPage.xaml.cs load the state from Application Data in the OnNavigatedTo event.
     protected async override void OnNavigatedTo(NavigationEventArgs e)
            {
                base.OnNavigatedTo(e);
                //Load application/game state from Windows.Storage.ApplicationData.Current.LocalFolder here
                await LoadGameState();
            }
    

Create a free website or blog at WordPress.com.