In this post I am going to talk about how to programmatically switch between different HubTile Visual States.  Since the HubTile does not expose any API for changing / switching between its visual states manually it is not obvious how you can do that. However, the solution is pretty simple: you just need to use the VisualStateManager class which manages states and the logic for transitioning between states of controls.

For reference take a look at the official MSDN Documentation.

To begin with, lets first create a new Windows Phone application project and add a reference toMicrosoft.Phone.Controls.Toolkit.dll.

NOTE: For more information about the HubTile control take a look at:

Step1. Add the following code in MainPage.xaml:

1
2
3
4
<phone:PhoneApplicationPage
   x:Class="HubTileDemo.MainPage"
   ...
   xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit">
1
2
3
4
5
6
7
8
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" Orientation="Vertical">
        <toolkit:HubTile x:Name="hubTile" Background="Green" Source="wpglogo.png" Title="Hold Here" Message="This is HubTile message!" Margin="10"/>
 
        <Button x:Name="btnGoToExpanded" Content=" Go To Expanded State" Click="btnGoToExpanded_Click" />
        <Button x:Name="btnGoToSemiexpanded" Content="Go To Semiexpanded State" Click="btnGoToSemiexpanded_Click" />
        <Button x:Name="btnGoToFlipped" Content="Go To Flipped State" Click="btnGoToFlipped_Click" />
        <Button x:Name="btnGoToCollapsed" Content="Go To Collapsed State" Click="btnGoToCollapsed_Click" />
</StackPanel>

Step2.  Set the HubTile "IsFrozen" property to true in order to prevent the HubTile Visual State from being changed automatically. Add the following code in MainPage.xaml.cs:

1
2
3
4
5
6
public MainPage()
{
    InitializeComponent();
 
    this.hubTile.IsFrozen = true;
}

Step3. Go To Expanded State implementation:

1
2
3
4
private void btnGoToExpanded_Click(object sender, RoutedEventArgs e)
{
    VisualStateManager.GoToState(this.hubTile, "Expanded", true);
}

Step4. Go To Semiexpanded State implementation:

1
2
3
4
private void btnGoToSemiexpanded_Click(object sender, RoutedEventArgs e)
{
    VisualStateManager.GoToState(this.hubTile, "Semiexpanded", true);
}

Step5. Go To Flipped State implementation:

1
2
3
4
private void btnGoToFlipped_Click(object sender, RoutedEventArgs e)
{
    VisualStateManager.GoToState(this.hubTile, "Flipped", true);
}

Step6. Go To Collapsed State implementation:

1
2
3
4
private void btnGoToCollapsed_Click(object sender, RoutedEventArgs e)
{
    VisualStateManager.GoToState(this.hubTile, "Collapsed", true);
}

That was all about programmatically switching between the different HubTile Visual States. The source code is available here:

I hope that the article was helpful.

You may also find interesting the following articles:

You can also follow us on Twitter: @winphonegeekfor Windows Phone; @winrtgeekfor Windows 8 / WinRT

Comments

Thanks again

posted by: Steven on 10/15/2011 6:18:40 PM

Thanks again! Very useful for me!

How to Switch on dynamically created HubTiles?

posted by: MSiccDev on 1/30/2012 1:24:46 PM

This works fine with a single HubTile.

But how can I do this if I add my HubTiles as DataTemplate to a ListBox?

The VisualStateManager does not recognize the HubTile as control, and returns alwas null.

Anyone an idea?

Programatically adding and animating

posted by: Nate Radebaugh on 12/21/2012 8:51:14 PM

If you are going to be controlling the states of a HubTile you have programatically added, you must do it after the HubTile is loaded. For instance:

    HubTile tile = new HubTile();
tile.Title = "Title";
tile.Message = "Message";
tile.Loaded += tile_Loaded;

And then have the event handler perform the settings: (for example)

    void tile_Loaded(object sender, RoutedEventArgs e)
{
HubTile tile = (HubTile)sender; string setState = "Semiexpanded"; // set the tile's state
VisualStateManager.GoToState(tile, setState, true); tile.IsFrozen = true;
}

Hope this helps someone else, took me a while to figure out where I was going wrong since the VisualStateManager.GoToState() does nothing and returns false if it doesn't know the state you send it, which it never knows before the HubTile is Loaded.

How to Programmatically Switch between the HubTile Visual States的更多相关文章

  1. 零元学Expression Blend 4 - Chapter 33 简单轻松的学会如何使用Visual States(下)

    原文:零元学Expression Blend 4 - Chapter 33 简单轻松的学会如何使用Visual States(下) 上篇提到了Visual State Manager中文翻译为视觉状态 ...

  2. 零元学Expression Blend 4 - Chapter 32 简单轻松的学会如何使用Visual States(上)

    原文:零元学Expression Blend 4 - Chapter 32 简单轻松的学会如何使用Visual States(上) Visual State Manager中文翻译为视觉状态管理器,这 ...

  3. Visual Studio创建跨平台移动应用_02.Cordova Extension

    1简介 本章节是关于Visual Studio Tools for Apache Cordova的,目前此产品只发布了预览版.Visual Studio for Apache Cordova帮助熟悉V ...

  4. 使用 Cordova+Visual Studio 创建跨平台移动应用(1)

    1简介 本章节是关于Visual Studio Tools for Apache Cordova的,目前此产品只发布了预览版.Visual Studio for Apache Cordova帮助熟悉V ...

  5. 论文笔记:Visual Semantic Navigation Using Scene Priors

    Visual Semantic Navigation Using Scene Priors 2018-10-21 19:39:26 Paper:  https://arxiv.org/pdf/1810 ...

  6. Expression Blend实例中文教程(11) - 视觉管理器快速入门Visual State Manager(VSM)

    Visual State Manager,中文又称视觉状态管理器(简称为VSM),是Silverlight 2中引进的一个概念.通过使用VSM,开发人员和设计人员可以轻松的改变项目控件的视觉效果,在项 ...

  7. Expression Blend实例中文教程(11) - 视觉管理器快速入门Visual State Manager(VSM)

    Expression Blend实例中文教程(11) - 视觉管理器快速入门Visual State Manager(V 时间:2010-04-12 16:06来源:SilverlightChina. ...

  8. Chapter 20: Diagnostics

    WHAT'S IN THIS CHAPTER?n Code contractsn Tracingn Event loggingn Performance monitoringWROX.COM CODE ...

  9. UWP VirtualizedVariableSizedGridView 支持可虚拟化可变大小Item的View(二)

    上篇UWP VirtualizedVariableSizedGridView 支持可虚拟化可变大小Item的View(一) 讲到该控件的需要和设计过程. 这篇讲讲开发过程中一些重要问题解决. 1.支持 ...

随机推荐

  1. HDOJ 2546饭卡(01背包问题)

    http://acm.hdu.edu.cn/showproblem.php?pid=2546 Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如 ...

  2. 原创:分享asp.net伪静态成目录形式iis如何设置

    服务器租用详解asp.net伪静态成目录形式iis如何设置: 一.首先介绍一下asp.net伪静态成html后缀iis如何设置的 iis6 伪静态 iis配置方法 图解 1.右键点击 要设置网站的网站 ...

  3. ntpdate公司测试

    [root@i158 ~]# ntpdate -u time.uuwatch.com 9 Jul 11:18:50 ntpdate[853]: no server suitable for synch ...

  4. 【Python】Django 聚合 Count与Sum用法,注意点

    代码示例: from django.db.models import Sum, Count #alarm_sum_group_items = models.FILE_PROTECT_ALARM.obj ...

  5. Apache配置文件中的deny和allow的使用

    Apache配置文件中的deny和allow的使用 由于产品的需要,最近在配置apache的负载均衡功能,但是在配置虚拟主机的访问权限的时候我们遇到了一些问题.主要问题是deny和allow的执行顺序 ...

  6. JavaScript或jQuery模拟点击超链接和按钮

    有时候我们需要页面自动点击超链接或者按钮,可以用js或者jQuery利用程序去点击,方法很简单,按钮或超链接代码如下: <a href="url" target=" ...

  7. AJAX 异步交互基本总结

    AJAX (Asynchronous JavaScript and Xml) 直译中文 - javascript和XML的异步 同步与异步的区别: 同步交互 执行速度相对比较慢 响应的是完整的HTML ...

  8. kindeditor的简单使用

    上传到云: 一.引入kindeditor <%@ page language="java" contentType="text/html; charset=UTF- ...

  9. VS禁止特定警告

    项目右键-属性-配置属性-C/C++-高级-禁用特定警告-填写警告编号例如4996

  10. jQuery常规选择器

    //简单选择器$('div').css('color','red'); //元素选择器,返回多个元素$('#box').css('color','red');//id选择器,返回单个元素$('.box ...