⬆️ ⬇️

Porting a C # / XAML Windows 8.1 application to UWP



If your Windows 8 or 8.1 application is small, then you can create a new universal UWP application project and transfer the XAML and C # code to it. If the application contains enough code, then there are other options.



In order to port the WinRT C # / XAML Windows 8.x application to Windows UWP, you need to change the manifest and the project file. There is no way to do this from the Visual Studio environment yet, but you can use a script that performs some automatic operations, taking over most of the work.



You can find the script at the link: GitHub Win10DevGuideMVA / ProjectUpgradeUtility



From there, download 2 files:

Upgrade_to_uwp.ps1 - the script itself

Run_Upgrade_to_uwp.bat - batch file to run the script automatically.

')

If you want to do everything manually, you can use the official instructions on how to manually transfer the application from Windows 8.1 to the universal Windows platform (UWP):

Migrating applications to the universal Windows platform (UWP)



With the update of the project file, the script copes well for verification, but I will write more about updating the manifest.

You can update the manifest file manually by adding the uap prefix to some elements :

The following manifest code



<Application> <Extension Category="windows.protocol"> <Protocol Name="mailto" DesiredView="useHalf"> <DisplayName>MailTo Protocol</DisplayName> </Protocol> </Extension> </Application> 


Turns into



 <Application> <uap:Extension Category="windows.protocol"> <uap:Protocol Name="mailto" DesiredView="useHalf"> <uap:DisplayName>MailTo Protocol</uap:DisplayName> </uap:Protocol> </uap:Extension> </Application> 


Some attributes need to be removed. Example:



  <m2:VisualElements DisplayName="App1" Square150x150Logo="Assets\Logo.png" Square30x30Logo="Assets\SmallLogo.png" Description="some description" BackgroundColor="#464646" ForegroundText="light" ToastCapable="true"> <m2:SplashScreen Image="Assets\SplashScreen.png" /> <m2:DefaultTile ShortName="MyApp" Wide310x150Logo="310x150.png" Square310x310Logo="310x310.png" Square70x70Logo="70x70.png" DefaultSize="square150x150Logo"> </m2:DefaultTile> <m2:ApplicationView MinWidth="width320" /> </m2:VisualElements> 


From this code we need to remove:

ForegroundText = "light"

ToastCapable = "true"

DefaultSize = "square150x150Logo"

<m2: ApplicationView MinWidth = "width320" />



In addition, you need to change the size of the tiles (do not forget to replace the image files too).

In the Square30x30Logo manifest code, we replace Square44x44Logo , and Square70x70Logo we replace with Square71x71Logo .

For example:



  <m2:VisualElements DisplayName="App1" Square150x150Logo="Assets\Logo.png" Square30x30Logo="Assets\SmallLogo.png" Description="some description" BackgroundColor="#464646"> <m2:SplashScreen Image="Assets\SplashScreen.png" /> <m2:DefaultTile ShortName="MyApp" Wide310x150Logo="310x150.png" Square310x310Logo="310x310.png" Square70x70Logo="70x70.png"> </m2:DefaultTile> </m2:VisualElements> 


Change to



  <uap:VisualElements DisplayName="App1" Square150x150Logo="Assets\Logo.png" Square44x44Logo="Assets\SmallLogo.png" Description="some description" BackgroundColor="#464646"> <uap:SplashScreen Image="Assets\SplashScreen.png" /> <uap:DefaultTile ShortName="MyApp" Wide310x150Logo="310x150.png" Square310x310Logo="310x310.png" Square71x71Logo="71x71.png"> </uap:DefaultTile> </uap:VisualElements> 


You also need to delete all the contents of the Prerequisites, including the title tags:



  <Prerequisites> <OSMinVersion>6.3.0</OSMinVersion> <OSMaxVersionTested>6.3.0</OSMaxVersionTested> </Prerequisites> 


A full list of changes to the manifest can be found on the What's different in Windows 10 page.



It was a theory. Next, I will try to describe how the process of updating the application took place.



Since I have a 32-bit system, in the Upgrade_to_uwp.ps1 script file I changed the 6th line with

$ FolderPath = "C: \ Program Files (x86) \ Windows Kits \ 10 \ Platforms \ UAP"

on

$ FolderPath = "C: \ Program Files \ Windows Kits \ 10 \ Platforms \ UAP"



As a rule, by default, PowerShell is configured to disable execution of scripts. Therefore, it is necessary to temporarily allow the execution of unsigned scripts.

Having opened PowerShell with administrator rights, I ran the command

Set-ExecutionPolicy Unrestricted

And set the parameter Y.



Launched the script.

I got this report
 PS D:\Porting to Windows 10\TesT10\TesT> D:\Porting to Windows 10\TesT10\TesT\Upgrade_to_uwp.ps1 Converting TesT.csproj... True     <ItemGroup> <AppxManifest Include="Package.appxmanifest"> <SubType>Designer</SubType> </AppxManifest> <None Include="TesT_StoreKey.pfx" /> <None Include="TesT_TemporaryKey.pfx" /> <Page Include="CharmStyle3.xaml"> <Generator>MSBuild:Compile</Generator> <SubType>Designer</SubType> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Page> <Page Include="CharmStyle2.xaml"> <Generator>MSBuild:Compile</Generator> <SubType>Designer</SubType> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Page> <Page Include="CharmStyle1.xaml"> <Generator>MSBuild:Compile</Generator> <SubType>Designer</SubType> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Page> <Content Include="Assets\en-US\redactor.png" /> <Content Include="Assets\en-US\SplashScreen.png" /> <Content Include="Assets\Logo.scale-100.png" /> <Content Include="Assets\Logo.scale-140.png" /> <Content Include="Assets\Logo.scale-180.png" /> <Content Include="Assets\Logo.scale-80.png" /> <Content Include="Assets\LogoImage.png" /> <Content Include="Assets\LogoOpenFile.png" /> <Content Include="Assets\redactor.png" /> <Content Include="Assets\ru-RU\redactor.png" /> <Content Include="Assets\ru-RU\SplashScreen.png" /> <Content Include="Assets\SmallLogo.scale-140.png" /> <Content Include="Assets\SmallLogo.scale-180.png" /> <Content Include="Assets\SmallLogo.scale-80.png" /> <Content Include="Assets\StoreLogo.scale-100.png" /> <Content Include="Assets\StoreLogo.scale-140.png" /> <Content Include="Assets\StoreLogo.scale-180.png" /> <Content Include="Assets\WideLogo.scale-100.png" /> <Content Include="Assets\WideLogo.scale-140.png" /> <Content Include="Assets\WideLogo.scale-180.png" /> <Content Include="Assets\WideLogo.scale-80.png" /> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </Content> <Content Include="ENdemo\image1.jpg"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> <Content Include="imgfolder\press1.png" /> <Content Include="imgfolder\press2.png" /> <Content Include="imgfolder\press3.png" /> <Page Include="Common\StandardStyles.xaml"> <Generator>MSBuild:Compile</Generator> <SubType>Designer</SubType> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Page> <Page Include="FilesPage.xaml"> <Generator>MSBuild:Compile</Generator> <SubType>Designer</SubType> </Page> <Content Include="imgfolder\right1.png" /> <None Include="Package.StoreAssociation.xml"> <SubType>Designer</SubType> </None> <Content Include="updownleftright.jpg" /> <Content Include="imgfolder\wrong1.png" /> <Content Include="demo\????\1.jpg" /> <Content Include="demo\????\2.jpg" /> <PRIResource Include="en-US\Resources.resw" /> <Content Include="ENdemo\demo.tstf"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> <PRIResource Include="ru-RU\Resources.resw" /> <None Include="Common\ReadMe.txt" /> <Page Include="Flyout.xaml"> <Generator>MSBuild:Compile</Generator> <SubType>Designer</SubType> </Page> <Page Include="FlyoutAbout.xaml"> <Generator>MSBuild:Compile</Generator> <SubType>Designer</SubType> </Page> </ItemGroup>. D:\Porting to Windows 10\TesT10\TesT\Upgrade_to_uwp.ps1:53 :3 + $FileContents = $FileContents -replace "$AppManifestIdentityGroup", ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (<ItemGroup> ... </ItemGroup>:String) [], RuntimeException + FullyQualifiedErrorId : InvalidRegularExpression Finished updating csproj file Converting Package.appxmanifest... True Finished updating package.appxmanifest Creating project.json... Done 




I started PowerShell and returned the value

Set-ExecutionPolicy Unrestricted

on n .



As can be seen from the conversion report, the Russian text was not recognized and was replaced by question marks.



 <Content Include="demo\????\2.jpg" /> 


That is, I had to edit the .csproj file a little manually. You can tweak the Upgrade_to_uwp.ps1 file slightly so that it converts the UFT-8 .csproj file. To do this in lines 21 and 57:

$ FileContents | Out-File $ FilePath -Encoding ascii -Force

replace with

$ FileContents | Out-File $ FilePath -Encoding utf8 -Force



In addition, there were still some errors in the manifesto that also needed to be corrected.

What I've done:



Added to Square44x44Logo manifesto

<uap: VisualElements Square44x44Logo = "StoreLogo44.png"

Removed from the same line:

ToastCapable = "true" and Square30x30Logo = "Assets \ SmallLogo.png"



Since my application has not yet been published for the phone, the line:

<mp: PhoneIdentity PhoneProductId = "44823AlexejSommer.TesTRedactoR" PhonePublisherId = "00000000-0000-0000-0000-000000000000" />

replaced by:

<mp: PhoneIdentity PhoneProductId = "00000000-0000-0000-0000-000000000000" PhonePublisherId = "00000000-0000-0000-0000-000000000000" />



Removed from manifest:



 <Extensions> <Extension Category="windows.fileOpenPicker"> <FileOpenPicker> <SupportedFileTypes> <FileType>.tstf</FileType> </SupportedFileTypes> </FileOpenPicker> </Extension> <Extension Category="windows.fileTypeAssociation"> <FileTypeAssociation Name="testredactor"> <DisplayName>ms-resource:takeatest</DisplayName> <Logo>Assets\LogoOpenFile.png</Logo> <SupportedFileTypes> <FileType>.tstf</FileType> </SupportedFileTypes> </FileTypeAssociation> </Extension> <Extension Category="windows.contactPicker" /> </Extensions> 


Instead, you could add the code manually:



 <Extensions> <uap:Extension Category="windows.fileOpenPicker"> <uap:FileOpenPicker> <uap:SupportedFileTypes> <uap:FileType>.tstf</uap:FileType> </uap:SupportedFileTypes> </uap:FileOpenPicker> </uap:Extension> <uap:Extension Category="windows.fileTypeAssociation"> <uap:FileTypeAssociation Name="test"> <uap:SupportedFileTypes> <uap:FileType>.tstf</uap:FileType> </uap:SupportedFileTypes> </uap:FileTypeAssociation> </uap:Extension> </Extensions> 


But I used the graphic editor of the manifest and added “announcements” in it. It became possible to use the graphical editor of the manifest after I removed all the erroneous code from the manifest.



I tried to collect and received more than a thousand errors. Including the error:

Couldn't find the required information in the lock file. Make sure you have UAP, Version = v10.0.10240 / win10-anycpu mentioned in your targets.

So yes, because the project type “Any CPU” on the UWP platform is not supported. Typical blooper. Changed the project's target platform to x86 and the number of errors became acceptable - just a couple of dozen.



In particular, there were errors like:

Error CS0104 'XmlDocument' is an ambiguous reference between 'Windows.Data.Xml.Dom.XmlDocument' and 'System.Xml.XmlDocument'

But I quickly corrected all the references to the XmlDocument on Windows.Data.Xml.Dom.XmlDocument.



Additionally, the following code has been deprecated:



 SettingsPane.GetForCurrentView().CommandsRequested += OnCommandsRequested; // ........ void OnCommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args) { try { SettingsCommand sCommand = new SettingsCommand("SettingsPage", "Settings", new UICommandInvokedHandler(onSettingsCommand)); args.Request.ApplicationCommands.Add(sCommand); SettingsCommand shareQCommand = new SettingsCommand("ShareQuestion", "Share", new UICommandInvokedHandler(onShareQCommand)); args.Request.ApplicationCommands.Add(shareQCommand); } catch { } } private void onSettingsCommand(IUICommand command) { // .....   FlyOut   } private void onShareQCommand(IUICommand cmd) { DataTransferManager.ShowShareUI(); } 


After removing it, I received a working draft. This functionality will have to be returned later in another way.



After all this, according to the recommendations, I added to MainPage after this.InitializeComponent (); The following code specifies the minimum window size, as well as the preferred size of the application at startup:



 public MainPage() { this.InitializeComponent(); ApplicationView.GetForCurrentView().SetPreferredMinSize(new Size { Width = 500, Height = 400 }); ApplicationView.PreferredLaunchViewSize = new Size { Height = 800, Width = 600 }; ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize; } 


The rest of the work is the replacement of FlyOut pop-up elements (with ContentDialog, PopUp elements or pages with navigation), working with the code that has become deprecated, adapting the code for different devices using #if and creating a flexible / responsive / sharpened UI suitable for devices with different screen sizes.



Video from the last conference // build /

Moving to the Universal Windows Platform: Porting an App from Windows 8.1 XAML or Windows Phone Silverlight to Windows 10

MVA course videos

A Developer's Guide to Windows 10: (21) Porting 8.1 Apps to Windows 10

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



All Articles