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. haartraining生成.xml过程

    继续向大神学习http://www.cnblogs.com/tornadomeet/archive/2012/03/28/2420936.html

  2. OpenStack 的Nova组件详解

    Open Stack Compute Infrastructure (Nova) Nova是OpenStack云中的计算组织控制器.支持OpenStack云中实例(instances)生命周期的所有活 ...

  3. JDBC之java数据库的连接与简单的sql语句执行

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sq ...

  4. chrome断点续传功能

    刚好找到了一个临时的解决方法,chrome其实已经内部实现了断点续传功能,不过应该还没完善,所以要自己打开.方法:用chrome在地址栏输入chrome://flags用搜索找到resumption( ...

  5. 字符编码浅识:关于Unicode与UTF-8

    参考自阮一峰博客:http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html Unicode只是一个符号集,它只规定了符号的 ...

  6. object-c 继承多态 动态数据类型

    在c#中我们知道有继承的.同样在object-c中也有继承. 例如我们写一个人类(父),一个学生类.我们可以这么写: demo: @interface Person:NSobject{ NSStrin ...

  7. git无法clone远程代码库及git代理设置

    git作为一个版本管理神器,日常工作中自然也就少不了了:特别是Android开发,github和google是逃不过的了.然而很多时候需要用到git克隆远程的代码库,众所周知的原因google.and ...

  8. Android PopupWindow 消失后的回掉方法

    popupwindow 会在点击屏幕其他地方消失,但是这个消失时的回掉方法一直没有找到,困扰了一个多月,差了不少百度,没有很好解决,今天偶然看到了setOnDismissListener这个方法!没想 ...

  9. iOS 关于Layer的疑问

    很久很久以前,就对ios的Layer十分的不解,学习了android后,打算通过android中的相关实现,分析一下ios中layer的作用,结果没有找到android中的对应的内容!十分让人郁闷.于 ...

  10. 8.python笔记之面向对象基础

    title: 8.Python笔记之面向对象基础 date: 2016-02-21 15:10:35 tags: Python categories: Python --- 面向对象思维导图 (来自1 ...