Showing posts with label windows 10 tutorials. Show all posts
Showing posts with label windows 10 tutorials. Show all posts

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:




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)