Tag Archives: WinRT

Iterate installed WinRT (Metro style app) package from console application

 

I had to find whether my application installed and where it got installed for some situation. While investigating that, I found that Microsoft provides the built in class PackageManager class to Deploy, Remove or Find the available or installed package in your machine. This will be a very valuable class for all WinRT developers who wants to control their package through another application instead of default VS installer.

 

Here I have given a small code snippet for iterating the installed package details through a console application.

PackageManager packageManager = new Windows.Management.Deployment.PackageManager();

       IEnumerable<Package> packages = 

           packageManager.FindPackagesForUser(WindowsIdentity.GetCurrent().User.Value);

       

       foreach (Package package in packages)

       {

               Console.Write(package.Id.Name);

 

               try

               {

                   Console.WriteLine("\n   " + package.InstalledLocation.Path + "\n");

               }

               catch (System.IO.FileNotFoundException)

               {

                   Console.WriteLine(" has been deleted from its installation folder...\n");

               }

       }

Exception thrown while trying to access UI element in WinRT Unit Testing

Next step of app is looking to automate the stuff we implemented. So that, we will not need to spend more time for ensuring the apps before we deliver.However, I was getting the error while trying to write unit test cases to check some of the UI Element’s properties. For ex, if I want to check the value of the UI element property after I made change in the ViewModel to ensure whether binding is working fine, unit test doesn’t allow to create UI controls in the background thread as it is supposed to be accessed in UI thread only.

Error message:

Test Name:    TestINRRate
Test FullName:    CurrencyConvertorTests.UnitTest1.TestINRRate
Test Source:    c:\WinRT\CurrencyConvertorTests\UnitTest1.cs : line 18
Test Outcome:    Failed
Test Duration:    0:00:00.0087226

Result Message:    Test method CurrencyConvertorTests.UnitTest1.TestINRRate threw exception:
System.Exception: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
Result StackTrace:
at Windows.UI.Xaml.Controls.Page..ctor()
at CurrencyConvertorApp.MainPage..ctor() in c:\WinRT\CurrencyConvertor\MainPage.xaml.cs:line 25
at CurrencyConvertorTests.UnitTest1.TestINRRate() in c:\WinRT\CurrencyConvertorTests\UnitTest1.cs:line 19

Solution I found it through a MSDN forum is and the below is the code block to resolve this issue.

public IAsyncAction ExecuteOnUIThread(Windows.UI.Core.DispatchedHandler action)

      {

          return Windows.ApplicationModel.Core.CoreApplication

              .MainView.CoreWindow.Dispatcher

              .RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, action);

      }



      [TestMethod]

      public async Task TestINRRate()

      {

          await ExecuteOnUIThread(() =>

           {

               MainPage page = new MainPage();

               page.Background = new SolidColorBrush(Colors.Blue);


               Assert.AreEqual(new SolidColorBrush(Colors.Blue).Color, ((SolidColorBrush)page.Background).Color);

           });


      }

This works great for me to run.

Hope this helps!!!