Sunday 5 July 2015

Windows 10 Universal Apps (UAD) - Adaptive Triggers



The VisualStateManger has a collection of VisualStateGroups each one being a set of VisualStates that may apply in a given situation. Each of the groups is independent in the sense that one VisualState from each group might be active at any one time. For simplicity, let's just deal with one VisualStateGroup from which only one VisualState is active.
In this case the basic XAML is:

<VisualStateManager.VisualStateGroups>
 <VisualStateGroup>
   list of visual states
 </VisualStateGroup>
</VisualStateManager.VisualStateGroups>

This is how the VisualStateManager worked before Windows 10, but you could only activate a VisualState in code and the changes to the UI were specified as animations. With Windows 10 you can now activate a state using an AdaptiveTrigger object using just XAML and no code. In addition, you no longer have to pretend that your UI is changing via an animation. Now you can use a Setter to change the value of any property on any object.
Each of the VisualState objects can have list of StateTriggers and a list of Setters and of course the order doesn't matter but it is slightly more logical to start with the StateTriggers.
The StateTriggers set conditions for the VisualState to be active and the Setters can be used to set properties on any XAML object when the VisualState is activated.
An AdaptiveTrigger takes the simple form:

<AdaptiveTrigger property=value />

the property is set to the value that you specify and this is usually used in a comparison with some other value to determine when the trigger is fires.
At the moment the only triggers are MinWindowWidth and MinWindowHeight but it is possible to add custom triggers.  These conditions test the current width and height of the window and trigger if the window is larger than the specified value.
For example:
<AdaptiveTrigger MinWindowsWidth=400 />
is active is the window' width is 400 effective pixels or greater.
Notice that what happens is that you set the value of MinWindowsWidth to 400 and this is compared to the actual width of the window when ever it changes. The trigger fires if the actual window width is greater than or equal to MinWindowsWidth.
A Setter takes the form:

<Setter Target="fully qualified property name"
        Value="value" />
So for example:
<Setter Target="Button1.Width"
        Value="200" />

Sets the Width of Button1 to 200 pixels if the state is active.
You can put multiple conditions and multiple setters into a single state.
For example:

<VisualState x:Name="Big">
 <VisualState.StateTriggers>
  <AdaptiveTrigger MinWindowWidth="600"/>  </VisualState.StateTriggers> 
 <VisualState.Setters> 
  <Setter Target="Button1.Content"
          Value="Wide"/> 
 </VisualState.Setters> 
</VisualState>

In this case the trigger is if the window is 600 pixels or wider.
If the trigger fires then the Button's Content is set to "Wide" what is it set to if the trigger isn't active?
The simple answer is that whatever value the property had before is restored when the trigger isn't active. For example, if the Button's original definition was:
<Button Name="Button1" Content="Narrow" />
the label on the Button would change to  "Wide" as the window was resized to bigger than 600 pixels and to "Narrow" when it became smaller than 599 pixels. That is, when none of the VisualStates is active, the system restores the UI to as it was origially specified in the XAML.
If you would rather have the default state included as a part of the VisualStateManager you can include it as:


<VisualState x:Name="Small">
 <VisualState.StateTriggers>
  <AdaptiveTrigger MinWindowWidth="0" />
 </VisualState.StateTriggers>
 <VisualState.Setters>
  <Setter Target="Button1.Content" 
                  Value="Narrow" />
 </VisualState.Setters>
</VisualState>

Putting the whole thing together gives:

<Page
 x:Class="App4.MainPage"
 xmlns="http://schemas.microsoft.com/
                     winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/
                     winfx/2006/xaml"
 xmlns:local="using:App4"
 xmlns:d="http://schemas.microsoft.com/
                     expression/blend/2008"
 xmlns:mc="http://schemas.openxmlformats.org/
                     markup-compatibility/2006"
 mc:Ignorable="d">
 <Grid Background="{ThemeResource 
         ApplicationPageBackgroundThemeBrush}">
  
  <VisualStateManager.VisualStateGroups>
   
   <VisualStateGroup>
    <VisualState x:Name="Small">
     <VisualState.StateTriggers>
      <AdaptiveTrigger MinWindowWidth="0" />
     </VisualState.StateTriggers>
     <VisualState.Setters>
      <Setter Target="Button1.Content" 
              Value="Narrow" />
     </VisualState.Setters>
    </VisualState>

    <VisualState x:Name="Big">
     <VisualState.StateTriggers> 
      <AdaptiveTrigger MinWindowWidth="600" /> 
      </VisualState.StateTriggers>
     <VisualState.Setters>
      <Setter Target="Button1.Content" 
              Value="Wide" />
     </VisualState.Setters>
    </VisualState>
   </VisualStateGroup>
  </VisualStateManager.VisualStateGroups>

  <Button x:Name="Button1" 
          HorizontalAlignment="Left"
          VerticalAlignment="Top" 
          Margin="196,172,0,0"/>
 </Grid>
</Page>


If you run this application and resize the width of the window you will see the label on the Button change as you cross the 600 pixel wide limit.
Of course, in a real application it is unlikely that you would be setting the content property of the controls. What you would be doing is setting layout properties to cope with the changing size of the window. This isn't an easy thing to do if the changes to the layout are large. It is much more suited to making small tweaks to essentially the same layout.

Examples:




Windows 10 | Stream Live Using Media Element (HTTP / HLS Streaming)

Windows 10 has really improved not only browser has improved and it stream HTTP /HLS but also Media element of Windows 10 now supports HTTP streaming. If you don't know much about HLS Streaming let me give you a brief overview of that,


Wikipedia Says,


HTTP Live Streaming (also known as HLS) is an HTTP-based media streaming communications protocol implemented by Apple Inc. as part of its QuickTimeSafariOS X, and iOS software. It works by breaking the overall stream into a sequence of small HTTP-based file downloads, each download loading one short chunk of an overall potentially unbounded transport stream. As the stream is played, the client may select from a number of different alternate streams containing the same material encoded at a variety of data rates, allowing the streaming session to adapt to the available data rate. At the start of the streaming session, it downloads an extended M3U playlist containing the metadata for the various sub-streams which are available.


It's extension may be .m3u8 or m3u, before it's support in Windows 10 it was really hard to stream video online. There was different Open Source SDKs available in market and some were paid. Wowza is one of the example for paid SDK where as open source SDK were also available which helped streaming live on Windows Phone and Windows Store. There are very few application on Windows Phone /Store (8/8.1) for live streaming of TV Channels, TVHUB is one of them.


It's a happy news that Microsoft Supports HLS /HTTP protocol in its media element for Universal Applications (Windows 10). Lets go through and example of Streaming Live Channel in Windows 10 Universal App.



Step1:

You need a Live Streaming Link with .m3u8 extension to stream live channel. I am providing one for your testing






http://ott.pakwatantv.com:5000/live/geonews0011/playlist.m3u8
  it would stream Geo New Live ,


Step 2:


Make sure you have successfully set up DEV environment for Windows 10, Open Visual Studio 2015 , Make a new Windows Universal App project for Windows 10, You can name it anything you like and click on create.


Step 3:



You need to work on XAML before you get into coding details of your steaming application, Use a StackPanel and laydown a Media Element with a button in it. Make sure button has a ClickEvent.




<StackPanel>

<MediaElement x:Name="mediaElement" HorizontalAlignment="Left" Height="486" Margin="10,10,0,0" VerticalAlignment="Top" Width="748"/>

    <StackPanel Orientation="Horizontal">

        <Button Content="Play" Margin="5" Height="45" Width="100"         Click="Button_Click"></Button>

    </StackPanel>

</StackPanel>


Step 4:



You just need to write following two lines of code and you would be good to stream.


mediaElement.Source = new Uri("http://ott.pakwatantv.com:5000/live/geonews0011/playlist.m3u8");






mediaElement.Play();
Now you just need to build the project and run it, You would end up with similar results,

NOTE:



My Streaming link might stop working at some point of time, use your own m3u8 link to stream video if this does not work.


Screenshots:




I hope you enjoyed this article , Happy Coding till next time.

Friday 3 July 2015

Windows 10 - Split view | Unleashing Hamburger Menu

Windows 10 is available for development and it's SDK has been released, Split view is new addition to XAML in Windows 10. Let's learn about split view control, The split view control can be used to make a nav pane pattern. To build this pattern, developer needs to add an expand/collapse button which is formally called hamburger control.

Split view control has an expandable pane and a content area. The content area is always visible. The pane can expand and collapse and remain in an open state, and can present itself from either the left side or the right side of an app windows. 

Split View had 3 Modes:


Overlay

           The pane is hidden until opened. When open, the pane overlays the content area.

Inline

           The pane is always visible and doesn't overlay the content area. The pane and content areas divide the available screen.

Compact

           The pane is always visible in this mode, which is just wide enough to show icons,
NOTE: usually 48 epx wide

The pane and the content area divide the available screen real estate. Although the standard compact mode doesn't overlay the content area, it can transform to a wider pane to show more content which will overlay the content area.


Let's try to use split view to create a hamburger menu.

Step 1:

Make sure the you have Development environment all set. The first thing you need to do it make sure you are running  some version of Windows 10, which you can find on Windows Insider. You need to have Visual Studio 2015 community edition installed as well. Those are only two things that ensure that you have installed.

Step 2:

Open Visual Studio Community Edition 2015 , now that you have opened the Visual Studio , you need to select Windows 10 section. In that you need to click on Blank Application and create that.


Step 3 : 


Visual Studio would create the project and now you can see the project listings in Solution Explorer. You would notice that this project does not consist for Windows Phone and Windows Store. There is only one project for both Windows Phone store and other.

Step 4:


Go to MainPage.xaml and let's edit some XAML, You would see Grid tag here, you just need to remove that and lets start our work by adding SplitView control. Split view has two main elements, the pane and the content. The pane represents the menu that you want to be able to interect with and the content represents the main content of your page.

Here is the code,

<SplitView>
       <SplitView.Pane>

       </SplitView.Pane>
       <SplitView.Content>

       </SplitView.Content>
</SplitView>


Before you add content , You should mention length of pane in compact form and in open form and also some other properties just like I have done here,

<SplitView x:Name="MySplitView" DisplayMode="CompactOverlay"  IsPaneOpen="False" 
               CompactPaneLength="50" OpenPaneLength="150">

Now each of the part needs a container that might be of any type, Grid , StackPanel or anything.
Lets add the grid and a simple textblock now.

 <SplitView.Content>
        
            <Grid>
               <TextBlock Text="Content of SplitView " FontSize="45" Foreground="White"
                          HorizontalAlignment="Center" VerticalAlignment="Center"/>
           </Grid>
       
</SplitView.Content>


Step 5:

Now it's time to create Hamburger Button that would be used to expand or contract the pane and would be used as Menu Button, now hamburger is quiet famous and is used in android and iOS. It's sign is 3 horizontal line.

Let's add that, remember you need to add that in <SplitView.Pane> tag and don't forget to add a container in that before you add Hamburger Menu Button. (StackPanel)

<Button x:Name="HamburgerButton" FontFamily="Segoe MDL2 Assets" Content="&#xE700;"
                    Width="50" Height="50" Background="Transparent" Click="HamburgerButton_Click"/>

You would notice that here and click event handler is attached to the xaml of hamburger menu, I would tell you later the purpose of this click event

Step 6:


You may add some more buttons below hamburger menu button, just like this,

<StackPanel Orientation="Horizontal">
                   <Button x:Name="MenuButton1" FontFamily="Segoe MDL2 Assets" Content="&#xE825;"
                   Width="50" Height="50" Background="Transparent"/>
                   <TextBlock Text="Button 1" FontSize="18" VerticalAlignment="Center" />
               </StackPanel>
               <StackPanel Orientation="Horizontal">
                   <Button x:Name="MenuButton2" FontFamily="Segoe MDL2 Assets" Content="&#xE10F;"
                       Width="50" Height="50" Background="Transparent"/>
                   <TextBlock Text="Button 2" FontSize="18" VerticalAlignment="Center" />
               </StackPanel>
               <StackPanel Orientation="Horizontal">
                   <Button x:Name="MenuButton3" FontFamily="Segoe MDL2 Assets" Content="&#xE1D6;"
                       Width="50" Height="50" Background="Transparent"/>
                   <TextBlock Text="Button 3" FontSize="18" VerticalAlignment="Center" />
               </StackPanel>

Now our xaml is ready, you just need one more line in C# to make you Hamburger Menu working

Step 7:

  You just need to add this simple line to the click event of hamburger menu button.

MySplitView.IsPaneOpen = !MySplitView.IsPaneOpen;

Conclusion:


This is everything you need to make your own hamburger menu. I hope you learnt something good today, till next article happy coding!

ScreenShots:




References:

https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.splitview.aspx

CODE (GITHUB)