vendredi 1 mars 2019

XAMARIN Tizen for Tvs and .NET Standard 2.0 [2019][XAML, C#]

My return of Experience on XAMARIN for TIZEN TVs the 1st of March 2019

Here is documentation where you can find about Xamarin Forms for Tizen:

Having reseted my laptop since last time I had used Xamarin Tizen I needed to go through the all the different steps to re install Tizen, you can find the full steps here:

TIP: took me 10 minutes to figure out why I could not create an emulator image , make sure that in your package manager you install the Tizen 4 TV emulator and the Tizen SDK tools. Under Extensions SDK make sure you also select TV extensions, the download of all of the SDK and emulators can take up quite a long time so in the mean time I had recommended that you head over to Microsoft Docs for Tizen for some light reading =).

My Findings:

Last year when I used the Preview of Xamarin for Tizen here where my initial findings, you had the sensation that the SDK was not fully finish (being a preview that seemed normal), the emulator was very buggy and often crashed, the debugger did not work (unable to set a break point).

Framework versions:

  • Tizen.NET (5.0.0.14562)
  • Tizen.NET.Sdk (1.0.1)
  • Xamarin.Forms(3.6.0.26487)


The application that I built is on one page for now, it receives and formats my data into a a list that contains a list. It will populate a ListView, this first ListView data template will hold another ListView so that we can show data horizontally (imagine the Netflix application).

Universal Windows Platform (UWP) 

When creating this application I first started by the UWP as I thought it would be quicker,  the first issue that I was that the debugger did not want to stop on my break points I had the follow error:

"The breakpoint will not currently be hit. No symbols have been loaded for this document"

which is not practical (at all!).

To fix this issue I removed all of the bin and obj folders in my UWP app and removed this options
Debug=>Options=>General => Remove the check mark for "Enable Just My Code"
and it did the trick for me.

Here is what it looks like on the UWP app:


Not pretty but it gets the job done.

Next on Tizen for TV

For the Tizen, application the debugger is still "weak", it will break on you often unfortunately, another issue I found was that I was not not able to move the debug back when it hit a break point.

After a sometime even Visual Studio was telling me that something was wrong:


To be fair it was not all doom and gloom because the debugger did work sometimes:

And you could go inside the properties and investigate, this is a great improvement over the last time I tested this platform. On other issue that I found was that the debugger did not resurface errors, even when explicitly crating an error it never surfaced it.

When launching the app in the Tizen Emulator it can take a few seconds for everything to lunch.

Here is what the same app looks like in the Tizen emulator:

 It would seem that some of the binding elements are not 100% finished, none of the images will load. Also navigating inside the app is very very slow, from what I am seeing you might better off buying a Samsung TV to debug then using the emulator!

All in all here are a summary of my findings:

  1. Tizen 5 Tv SDK is working much better then the Tizen 4 Tv Sdk
  2. Te emulator really works this time (last year I needed hacks to get it working), however only the version 5 worked for me and not the version 4, also you must arm yourself with patience for the app to load.  Everything is very lagy in the emulator.
  3. the debugger from Visual Studio is now working"ish", it can crash your Visual Studio but at least this time around you cant setup break points in your code.
  4. Binding still seems to be an issue, maybe this is a Xamarin issue ill have to investigate




lundi 14 janvier 2019

[how to] Using Multilingual App Toolkit to translate your UWP application

In this post we are going to see who easy it is to make your application multilingual using Microsoft Multilingual App Toolkit to translate your UWP application.

First you need a class that will allow us to get the translated string by using the ResourceLoader property.


 public class MyLocalizedClass
 {
        private static ResourceLoader _localizedResources;

        public ResourceLoader LocalizedResources
        {
            get
            {
                if (_localizedResources == null)
                {
                    _localizedResources = new ResourceLoader();
                }

                return _localizedResources;
            }
        }


        public string this[string key]
        {
            get
            {
                if (_localizedResources == null)
                {
                    _localizedResources = new ResourceLoader();
                }

                return _localizedResources.GetString(key);
            }
        }
}

You will need to bind all of the text in your application to your resource file, you can do this as follow:


         <TextBlock
                    x:Name="Header"
                    Style="{StaticResource TitleTextBlockStyle}"
                    Text="{Binding Path=[MyApplicationHeader], Source={StaticResource LocalizedStrings}, FallbackValue='MyApplicationHeader'}"                    TextWrapping="NoWrap" />

My MyApplicationHeader key will need to be added in resources.resw with a value as follows:



Next we will need to structure our application to with a folder string at the root of the app, this folder will also need to add a folder en and then add a resource file that we will call: Resources.resw

It should look as follows:




Next you will need to install Multilingual App Toolkit v4.0 (VS 2017), which you can find here. Once installed in Visual Studio under Tools your should see Multilingual App Toolkit:

you will need to Enable selection in the Multilingual App Toolkit.


Next you are going to need to install the Multilingual App Toolkit 4.0 Editor.

Now we are going to add translation languages to the application, again make sure you have enable Multilingual App Toolkit in your UWP application next click on the project, select Multilingual App Toolkit and click on Add translation as follows:



I will add french:



A new folder should be created inside you Strings folder:


We are going to right click on the project and use automatic translate from English to French using machine translations:



If you select Generate machine translations and you have errors most likely the issue is that you don't have the correct credentials to use Cognitive Services, you can fix this by reading this.

Once you have fixed your credential issues or if you don't have any issues you should see that the resources file under fr has been updated with french translated strings:



Now to test this new language you can force your application in French by overriding the application language as follows:


 Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = "fr"

Happy coding!




dimanche 9 décembre 2018

Using WinAppDriver to do UI Test Automation on your Windows Application, using the My Radio UWP - Part I

Because test is caring you should always try to test as much as possible your application to make sure that is it running smoothly.

We are going to setup multiples UI tests paths that need to be passed before a new version of the application can be pushed to the store. This will allows us to make sure that our application passes all of the basic quality checks and that all classic user paths are still functional.

We are going to have a deep look into one of these test path so that you too can improve your app by using UI test automation. 

Setup

First I highly recommend to have a look at the github repository on how WinAppDriver can be used:

Installing and Running Windows Application Driver

  1. Download Windows Application Driver installer from https://github.com/Microsoft/WinAppDriver/releases
  2. Run the installer on a Windows 10 machine where your application under test is installed and will be tested
  3. Run WinAppDriver.exe from the installation directory (E.g. C:\Program Files (x86)\Windows Application Driver)
Windows Application Driver will then be running on the test machine listening to requests on the default IP address and port (127.0.0.1:4723). You should have something as follows:




Locating Elements

The latest Visual Studio version by default includes the Windows SDK with a great tool to inspect the application you are testing. This tool allows you to see every UI element/node that you can query using Windows Application Driver
This inspect.exe tool can be found under the Windows SDK folder which is typically C:\Program Files (x86)\Windows Kits\10\bin\

Once you have launched the tool you should have a window as follows:






Here we can see that I have already launch the My Radio application.

We are going to be looking at 3 users Paths:

  • Arrive on Home, check we are on Home, click on favorite check we are on favorite and then go back to home
  • Can we will click on a radio thumbnail and make sure that it is playing
  • Advanced test : In another post we will go over how to create a more complex test in which we will:  We click on discovery, click on a category, click on a radio add it to our favorites, go to favorites and check that it is there.

Can I click on a button

First to be able to have the inspect tool select and highlight your selected item you must have activated these two buttons which are called watch cursor and show highlighted rectangle.


Now we are going to select the item that we wish to click on.  In my application the XAML code for this home button is:


                <NavigationViewItem x:Name="MainView" Tag="MainView">
                    <StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
                        <SymbolIcon
                            Width="30"
                            Height="30"
                            Margin="-5,0,0,0"
                            Symbol="Home" />
                        <TextBlock
                            Margin="12,0,0,0"
                            VerticalAlignment="Center"
                            Text="Home" />
                    </StackPanel>
                </NavigationViewItem>

Here is what it looks like in the inspect tool:


We can see that the automation id is "MainView" and when we look on the favorite button we can see that its id is "FavoriteView" for our first test we are going to be using FindElementByName to check and see if we have a specific text in the visible view and FindElementByAccessibilityId to look for a UIElement that has this specific Id.

Here is the TestMethod:


 [TestMethod]
 public void IsSideNavigationWorking()
 {
            //We arrive on home            
            var homeText = session.FindElementByName("Explore Radios");

            //check to see if we see the explore radios text
            Assert.IsNotNull(homeText);

            //click on categorie
            session.FindElementByAccessibilityId("FavoriteView").Click();

            //check to see if we see the fav radios text
            var favText = session.FindElementByName("My Favorite Radios");
            Assert.IsNotNull(favText);
            
            //go back on home view
            session.FindElementByAccessibilityId("MainView").Click();
            var homeTextV2 = session.FindElementByName("Explore Radios");

            //check to see if we are on correct view
            Assert.IsNotNull(homeTextV2);
 }


Is Radio playing user path

Here is the user path we are going to check:

  • launch the application
  • click on home and check that home is selected
  • click on first radio thumbnail and check that the radio is playing.
    • to check that radio is playing we are going to look for the media element that plays the radio item
Here is the inspect tool on the first radio in our list.


And here is the full test:


Here you have it, we have gone over two easy example on how to use WinAppDriver to do our UI Test Automation on a UWP Application. In the next Article we will do a deepdive on how to use WinAppDriver.




lundi 27 août 2018

[UWP/XAML] How to customize your NavigationView and add NavigationViewItem next to the Settings button

In this tutorial we are going to see how to add NavigationViewItems next to your Settings button in your NavigationView view.

First we need to get the Template of the NavigationView, Document Outline => Your NavigationView => Edit Template => edit a Copy.

Here is mine:


Look for the style that has a target type = Navigation View, here is mine:


 <Style x:Key="NavigationViewStyle1" TargetType="NavigationView">

Next we will need to find the NavigationViewItem that holds our settings button:


<NavigationViewItem x:Name="SettingsNavPaneItem" Grid.Row="7">
               <NavigationViewItem.Icon>
                      <SymbolIcon Symbol="Setting" />
               </NavigationViewItem.Icon>
</NavigationViewItem>


Now we are going to add our 2 new buttons, first we are going to add s stackpanel and move the property Grid.Row=7 to it as follows:



<StackPanel Grid.Row="7">
   <NavigationViewItem x:Name="AddedNavItemOne"    Tag="MyViewOne">
  <NavigationViewItem.Icon>
   <SymbolIcon Symbol="World" />
  </NavigationViewItem.Icon>
</NavigationViewItem> <NavigationViewItem x:Name="AddedNavItemTwo" Tag="MyViewTwo">
   <NavigationViewItem.Icon>
   <SymbolIcon Symbol="Search" />
  </NavigationViewItem.Icon>
</NavigationViewItem> <NavigationViewItem x:Name="SettingsNavPaneItem"> <NavigationViewItem.Icon> <SymbolIcon Symbol="Setting" /> </NavigationViewItem.Icon> </NavigationViewItem> </StackPanel>

Now we need to be able to handle the Click actions of these 2 new NavigationViewItem.

We are going to create a class that inherits from NavigationView that will be called ExtendedNavigationView and we arer going to look for our 2 buttons and set handlers on the Tapped events as follows:

_navItemOne= GetTemplateChild("AddedNavItemOne") as NavigationViewItem;
            
//addign events
_navItemOne.Tapped += NavItem_Tapped;

The full class will look as follows:

 public class ExtendedNavigationView : NavigationView
    {
        private NavigationViewItem _navItemOne;
        private NavigationViewItem _navItemTwo;

        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            SetupExtraNavItems();
        }

        private void SetupExtraNavItems()
        {
            //check to see if not already set
            if (_navItemOne!= null)
            {
                //unload events
                _navItemOne.Tapped -= NavItem_Tapped;
               
            }

            if (_navItemTwo!= null)
            {
                _navItemTwo.Tapped -= NavItem_Tapped;
            }

            _navItemOne= GetTemplateChild("AddedNavItemOne") as NavigationViewItem;
            _navItemTwo = GetTemplateChild("AddedNavItemTwo") as NavigationViewItem;           

            if (_navItemOne== null || _navItemTwo== null)
            {
                return;
            }

            //addign events
            _navItemOne.Tapped += NavItem_Tapped;
            _navItemTwo.Tapped += NavItem_Tapped;            
        }
        

        private void NavItem_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
        {
            var NavView = (sender as NavigationViewItem).Tag;
            //navigate to item
        }


And there you have it you have customized your NavigationView with 2 new NavigationViewItems above the Settings button.

Happy Coding.




mercredi 1 août 2018

[UWP/XAML] using Microsoft.Toolkit for Implicit animations using ScalarAnimation, OpacityAnimation, TranslationAnimation

Do you hate having to use Compositor?


_compositor = new Compositor();
_root = ElementCompositionPreview.GetElementVisual(RootGrid);

Well Microsoft.Toolkit.Uwp.UI.Animations is here to save you (and me).  The toolkit has added implicit animation which will allow you to easily add animations to your elements.  You wont need to use Storyboard animation!

In this example we are going to see how to:

  •  Add a translation to an element
  • Show and hide an element using Opacity
  • Scale an element
The advantage of using an Implicit animation is that it can for example be execute when you change the visibility of a component using:
 <animations:Implicit.ShowAnimations>

Or
 <animations:Implicit.HideAnimations>

Animations

To move an element in your app using implicit animation you have 2 options either you use ScalarAnimation or TranslationAnimation.

TranslationAnimation

Has 3 main properties that you are going to want to set:
  • From
  • To
  • Duration
From and To are based on the x-y-z axis
Duration uses a h:m:s format

 <animations:TranslationAnimation
                    From="500,300,0"
                    To="0"
                    Duration="0:0:1" />

This means we are going to add 500 to the x position, 300 to the y postion and 0 to the z. The To=0 will take the element from its current position and the translation will take 1 second.


OpacityAnimation 

Same as the translation this also takes 3 properties, however the From and To only takes one value as the opacity property is only based on one value.



<animations:OpacityAnimation
                    From="0"
                    To="1"
                    Duration="0:0:2" />


ScalarAnimation

If you wish to have more control on your animation you can also use ScalarAnimation which will allow you to more precisely (I find) to set the values to the property you wish to change.



            <animations:ScalarAnimation
                    Target="Translation.Y"
                    From="100"
                    To="0"
                    Duration="0:0:1"/>


First you need to set your Target, which is the property you are going to want to change.  Then depending on your target like the other animations you will set the From, To, Duration


Combining Animations

Now we are going to combine 2 animations using the ShowAnimations and HideAnimations.  We will use ScalarAnimation and OpacityAnimation to move and change the opacity of our object.

Here is the full XAML:

 <Button Click="Button_Click" Content="Hit me" />
        <Rectangle
            x:Name="MyRec"
            Grid.Row="0"
            Height="45"
            Margin="0,0,0,0"
            VerticalAlignment="Bottom"
            Fill="Red"
            Visibility="Collapsed">
            <animations:Implicit.ShowAnimations>
                <!--<animations:TranslationAnimation
                    From="100,0,0"
                    To="0,0,0"
                    Duration="0:0:1" />-->
                <animations:ScalarAnimation
                    Target="Translation.Y"
                    From="100"
                    To="0"
                    Duration="0:0:1">                  
                </animations:ScalarAnimation>

                <animations:OpacityAnimation
                    From="0"
                    To="1"
                    Duration="0:0:2" />
            </animations:Implicit.ShowAnimations>

            <animations:Implicit.HideAnimations>
                <animations:OpacityAnimation
                    From="1"
                    To="0"
                    Duration="0:0:2" />

                <animations:ScalarAnimation
                    Target="Translation.Y"
                    From="0"
                    To="100"
                    Duration="0:0:1">                
                </animations:ScalarAnimation>

                <!--<animations:TranslationAnimation
                    From="0,0,0"
                    To="100,0,0"
                    Duration="0:0:1" />-->
            </animations:Implicit.HideAnimations>
        </Rectangle>

C# Code:


private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (MyRec != null)
            {
                MyRec.Visibility = MyRec.Visibility == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
            }
        }

Happy Coding.





lundi 25 juin 2018

UWP/XAML using PopupThemeTransition / PopOutThemeAnimation / PopInThemeAnimation to animate elements coming in and out of your app [Part 1]

In UWP we have this great control called Popup this control can allow you to quickly and cleanly have element pop in and pop out of your view. Also we will look at how to do more of less the same thing with only a Grid.

In this post I wont go into details about the popup control itselft however if you are interested in getting more information msdn is the best place to look.

First we are going to create our simple popup element, make sure that you set the IsOpen property to false (this will hide your popup),  IsLightDismissEnabled to true will mean that if the user clicks outside of the popup it will disappear.

Here is what the XAML can look like:

        <Popup
            x:Name="PopupFromBottom"
            Grid.Row="0"
            Width="459"
            Height="90"
            Margin="20,0,20,20"
            VerticalAlignment="Bottom"
            IsLightDismissEnabled="True"
            IsOpen="False"/>

To add transition to when the popup will be shown we will add ChildTransitions:

 <Popup.ChildTransitions>
      <TransitionCollection>
           <PopupThemeTransition FromVerticalOffset="100" />
      </TransitionCollection>
 </Popup.ChildTransitions>

This will add an animation, the popup will come from the bottom with a translation of 100px. If you have wanted a Horizontal translation then you could have used FromHorizontalOffset.

For this popup to be displayed all you need is to set the IsOpen to true :

 PopupFromBottom.IsOpen = true;

Now if we wanted the popup to hide it self after 3 seconds all we would need to add is a wait of 3 seconds and again change the IsOpen to false as follows:

 //show
PopupFromBottom.IsOpen = true;

//wait 3 sec
await Task.Delay(TimeSpan.FromSeconds(3));

//close 
PopupFromBottom.IsOpen = false;

In the previous example we used the default popup control which allowed us to use the lightdismiss and directly attach the PopupThemeTransition animations to the popup.  However we always have custome cases where we need custome code and sometimes the default controls just wont cut it.  NOw we are going to see how to use a Grid as a popup.

first we are going to create our Grid as follows:

<Grid
            x:Name="GridPopup"
            Grid.Row="0"
            Width="459"
            Height="90"
            Margin="20,0,20,20"
            VerticalAlignment="Bottom"
            Background="LightGray"
            Opacity="0" />

Next we are going to add storyboards and use PopInThemeAnimation and PopOutThemeAnimation
to pop in and pop out our grid.  Here is the Storyboard XAML code:

 <Storyboard x:Name="PopOutStoryboard">
            <PopOutThemeAnimation SpeedRatio="2" TargetName="GridPopup" />
 </Storyboard>
 <Storyboard x:Name="PopInStoryboard">
            <PopInThemeAnimation
                FromVerticalOffset="150"
                SpeedRatio="0.3"
                TargetName="GridPopup" />
            <DoubleAnimation
                d:IsOptimized="True"
                Storyboard.TargetName="GridPopup"
                Storyboard.TargetProperty="Opacity"
                To="1"
                Duration="0" />
 </Storyboard>

and now to easily call this code all we need to do:

        private void OpenStoryboard_Click(object sender, RoutedEventArgs e)
        {
            PopInStoryboard.Begin();
        }

        private void CloseStoryboard_Click(object sender, RoutedEventArgs e)
        {
            PopOutStoryboard.Begin();
        }

And there you are! You can find the full code here.

jeudi 22 mars 2018

UWP, C# - Retrieve the redirect url using HttpClient from the Headers of the Response - [HttpClient,C#]

I wanted to share this little piece of code because I did not quickly find any great documentation on this.

My needs where as follows: I had a URL lets say http://bit.ly/2pwccfo and I did not have the direct access to the image of the URL.  So I ended up creating a small static class that holds a method GetRedirectedUrl that will get my redirect url.

Here is my code:

public static class CoreTools
{
       public static async Task<string> GetRedirectedUrl(string url)
        {
            //this allows you to set the settings so that we can get the redirect url
            var handler = new HttpClientHandler()
            {
                AllowAutoRedirect = false
            };
            string redirectedUrl = null;


            using (HttpClient client = new HttpClient(handler))
            using (HttpResponseMessage response = await client.GetAsync(url))
            using (HttpContent content = response.Content)
            {
                // ... Read the StatusCode in the response 
                // to see if we have the redirected url
                if (response.StatusCode == System.Net.HttpStatusCode.Found)
                {
                    HttpResponseHeaders headers = response.Headers;
                    // do we have headers 
                    //do we have something in location
if (headers != null && headers.Location != null) { redirectedUrl = headers.Location.AbsoluteUri; } } } return redirectedUrl; } }

You can find my gist here.
Happy Coding