📜 ⬆️ ⬇️

Updating Windows Phone Silverlight 8.0 application to Windows Phone Silverlight 8.1


We continue the series of articles on updating Windows Phone 8.0 applications to version 8.1. Today we will look at the new features of Silverlight 8.1 and the process of upgrading Windows Phone Silverlight 8.0 applications to version 8.1.

Short review

The new version of Windows Phone Silverlight 8.1 provides mobile application developers with access to the features of Windows Phone 8.1 WinRT without significantly changing the code of an existing Silverlight application.

Silverlight 8.1 is a continuation of technology for Silverlight developers.

Upgrading an application to Silverlight 8.1 is very simple using the built-in tool in Visual Studio 2013:

')
It should be noted that Silverlight 8.1 applications work only on Windows Phone 8.1 devices. If you want to keep the application running on previous versions of the operating system, it is recommended that the store also distribute the old version of the application to Silverlight 8.0.

If you are just starting to develop for Windows Phone and would like to have a version of the application for the PC, then it is recommended to start development on the new Windows Phone 8.1 ( WinRT or WinJS ), which provide the possibility of creating universal applications for phones, tablets and PCs. Unfortunately, the application on Silverlight 8.1 cannot be expanded to universal.

How to choose the right technology to develop your mobile application, you can read here .

New features of Windows Phone Silverlight 8.1

Many of the tools and APIs available for Silverlight 8.0 applications will also be available for Silverlight 8.1. Some differences in usage are described in the following article .

Below are some of the new features available for Silverlight 8.1 applications:

Features available only for Windows Phone 8.1 applications, but also supported for Silverlight 8.1 applications:

A more detailed list and information about new features in Silverlight 8.1 is located on MSDN .

How to upgrade from Windows Phone Silverlight 8.0 to Silverlight 8.1

Consider the process of upgrading a Windows Phone application Silverlight 8.0 to 8.1 in the following example. Take the content application - "Cooking book." It sorts recipes by country, each recipe consists of several pages: description, ingredients, photos. The application, in addition to displaying recipes, allows you to add photos of your own dishes and fix your favorite recipe on the phone's start screen.

What you need to develop:

The process of upgrading Silverlight 8.0 applications to 8.1:
  1. Do not forget to backup the ported application;
  2. Open the solution (.sln extension file) in Visual Studio 2013;
  3. Right-click the context menu:
  4. Next, click Yes:
  5. We get the solution in the Windows Phone Silverlight 8.1 browser:

The main icon is in place, but it looks like the images now require much more for comfortable work of the updated application.

Learn more about the requirements for Windows Phone Silverlight 8.1 application images written in this article .

We solve this problem simply by creating the necessary images and placing them in the Assets folder of the current application:


Testing a ported Windows Phone application Silverlight 8.1

Run the emulator and open the application:

The application did not open a new copy of the previously launched application, as it did in Silverlight 8.0, in the case of c returning to work with the application the following happened: the screen blinked, the application threw the user onto the start page and stopped displaying any data, and the current copy of the application was expected to start where it was interrupted.

This behavior is associated with a change in the life cycle of a Windows Phone application. Let's try to fix it.

Fix bugs in the ported application

  1. In the solution browser open the app.xaml.cs file:


    Check the FAR - Fast app resume (quick resume) feature that allows you to open the copy of the application that is in memory.

    Here in the Phone Application Initialization area we see that the template FAR processing code is available and everything should work correctly:
    #region Phone application initialization // Avoid double-initialization private bool phoneApplicationInitialized = false; // Do not add any additional code to this method private void InitializePhoneApplication() { if (phoneApplicationInitialized) return; // Create the frame but don't set it as RootVisual yet; this allows the splash // screen to remain active until the application is ready to render. RootFrame = new TransitionFrame(); RootFrame.Navigated += CompleteInitializePhoneApplication; // Handle navigation failures RootFrame.NavigationFailed += RootFrame_NavigationFailed; // Handle reset requests for clearing the backstack RootFrame.Navigated += CheckForResetNavigation; // Ensure we don't initialize again phoneApplicationInitialized = true; } // Do not add any additional code to this method private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e) { // Set the root visual to allow the application to render if (RootVisual != RootFrame) RootVisual = RootFrame; // Remove this handler since it is no longer needed RootFrame.Navigated -= CompleteInitializePhoneApplication; } private void CheckForResetNavigation(object sender, NavigationEventArgs e) { // If the app has received a 'reset' navigation, then we need to check // on the next navigation to see if the page stack should be reset if (e.NavigationMode == NavigationMode.Reset) RootFrame.Navigated += ClearBackStackAfterReset; } private void ClearBackStackAfterReset(object sender, NavigationEventArgs e) { // Unregister the event so it doesn't get called again RootFrame.Navigated -= ClearBackStackAfterReset; // Only clear the stack for 'new' (forward) and 'refresh' navigations if (e.NavigationMode != NavigationMode.New && e.NavigationMode != NavigationMode.Refresh) return; // For UI consistency, clear the entire page stack while (RootFrame.RemoveBackEntry() != null) { ; // do nothing } } #endregion 

    Let's check what is happening with the display of data.
  2. Open MainPage.xaml.cs and observe the following:
    In the OnNavigatedTo method, the condition is checked whether the list of recipes is loaded now:
      protected override void OnNavigatedTo(…) { if (!App.Recipes.IsLoaded) { pi = new Microsoft.Phone.Shell.ProgressIndicator(); pi.IsIndeterminate = true; pi.Text = "Loading data, please wait..."; pi.IsVisible = true; Microsoft.Phone.Shell.SystemTray.SetIsVisible(this, true); Microsoft.Phone.Shell.SystemTray.SetProgressIndicator(this, pi); App.Recipes.RecipesLoaded += Recipes_RecipesLoaded; App.Recipes.LoadLocalDataAsync(); } base.OnNavigatedTo(e); } 

    When you call the same application again, the method determines the list of recipes as already loaded and does not perform the actions specified by this condition.
    Add an alternative treatment condition:
      else if (e.NavigationMode == NavigationMode.New) { lstGroups.DataContext = App.Recipes; } 

    Check to make sure everything works correctly.
  3. Errors were discovered when opening files responsible for the application interface.
    Open any file, for example MainPage.xaml, and we get the lack of displaying elements in the designer:


    To solve the problem with the form designer, you need to pay attention to the following error:
    The member "Instance" is not accessible.
    Error means that the property is no longer supported.

    Further in the text of the file we find the line:
     <Grid x:Name="LayoutRoot" Background="Transparent" d:DataContext="{d:DesignData /SampleData/RecipeDataSourceSampleData.xaml, Instance={x:Null}}"> 

    We bring it to this form:
     <Grid x:Name="LayoutRoot" Background="Transparent" d:DataContext="{d:DesignData /SampleData/RecipeDataSourceSampleData.xaml}"> 

    The visual designer is fine:

Also, the compiler reports errors related to the toolkit items:


The new WinRT platform includes both old controls and many new ones. Therefore, the Windows Phone Toolkit library is no longer needed for mobile applications. Comment out or replace them with new ones.

Please note when updating the application for differences in the namespace and classes.

For example, in this application, the ListView class was used, which no longer exists in Windows Phone Silverlight 8.1 and in the application we replaced it with ListBox.
The LongListSelector class is also no longer supported, and we use the option of replacing it:
It was:
 <phone:LongListSelector x:Name="IngredientsListBox" ItemsSource="{Binding Ingredients}"/> 

It became:
 <ListBox x:Name="IngredientsListBox" ItemsSource="{Binding Ingredients}" > <StackPanel Margin="12,0,0,0"> <TextBlock Text="{Binding Mode=OneWay}" Margin="0,0,0,12"/> </StackPanel> </ListBox> 


Adding new features

One of the new features for Silverlight 8.1 is the ability to share data with other applications, including images.

Our application can create and store photos of a cooked dish, try to teach it to “share” using the new API features:
  1. In the Solution Explorer, open the RecipeDetailPage.xaml file:

  2. For the Application Bar element, add a new “Share” button:
    • Using the Properties window (Properties) for ApplicationBar, find the Buttons button collection, add the AppBarButton button, select the standard icon icon = share.png and specify its text = ”share” name:
    • Create and write a handler for the Share button:
       private void ShareButton_Click(object sender, EventArgs e)</li> { if (item.UserImages == null || item.UserImages.Count == 0) { MessageBox.Show("You must take a picture first!"); } else { DataTransferManager.ShowShareUI(); } } 

  3. In the OnNavigatedTo method, add a DataRequested event:
     protected async override void OnNavigatedTo(NavigationEventArgs e) { ... DataTransferManager.GetForCurrentView().DataRequested += RecipeDetailPage_DataRequested; base.OnNavigatedTo(e); } 

    Create a function to handle this event:
     async void RecipeDetailPage_DataRequested (DataTransferManager sender, DataRequestedEventArgs args) 
  4. Create and override the OnNavigatedFrom method:
     protected override void OnNavigatedFrom(NavigationEventArgs e) { DataTransferManager.GetForCurrentView().DataRequested -= RecipeDetailPage_DataRequested; } 
  5. Add the code to the RecipeDetailPage_DataRequested function to handle the DataRequested event:
     { DataRequest request = args.Request; var deferral = args.Request.GetDeferral(); try { Uri photoFileUri = new Uri("ms-appdata:///local/" + item.UserImages[0]); var photoFile = await StorageFile.GetFileFromApplicationUriAsync(photoFileUri); request.Data.Properties.Title = "I've been baking!"; request.Data.Properties.Description = "This was my attempt at making " + item.ShortTitle; // It's recommended to use both SetBitmap and SetStorageItems for sharing a single image // since the target app may only support one or the other. List<IStorageItem> imageItems = new List<IStorageItem>(); imageItems.Add(photoFile); request.Data.SetStorageItems(imageItems); RandomAccessStreamReference imageStreamRef = RandomAccessStreamReference.CreateFromFile(photoFile); // It is recommended that you always add a thumbnail image any time you're sharing an image request.Data.Properties.Thumbnail = imageStreamRef; request.Data.SetBitmap(imageStreamRef); // Set Text to share for those targets that can't accept images request.Data.SetText("I just made " + item.ShortTitle + " from Contoso Cookbook!"); } finally { deferral.Complete(); } 

    In order for the code to use all the functions used, let's connect:
     using Windows.ApplicationModel.DataTransfer; using Windows.Storage; using Windows.Storage.Streams; 

  6. We check our innovations. Open the application. We reproduce the process of creating a photo and press the "Share" button:


    Open services in which you can share a photo.

Conclusion

Silverlight 8.1 is not only a continuation of the previous version of Silverlight 8.0, but also combines new features offered by Windows Phone 8.1 technology and WinRT. And the transition to Silverlight 8.1 from applications of the previous version is not a long and laborious process.

useful links

New Windows Phone 8.1. What should an application developer do?
Update Windows Phone 8.0 application to Windows Phone 8.1 (XAML)
We turn the Windows Store application into a universal
Tutorial: Building Apps for windows Phone 8.1
MSDN Library: Supported features for Windows Phone Silverlight 8.1 apps
Microsoft Virtual Academy (MVA) Training Courses
Download free or trial Visual Studio 2013
Become a Windows Phone Application Developer
Download the sample application on Silverlight 8.1 from this article.
Download code samples with basic features of Windows Phone 8.1 (c #, c ++, javascript)

Source: https://habr.com/ru/post/230015/


All Articles