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.

最新文章

  1. alert 替代效果smoke.js
  2. HDU 2732:Leapin&#39; Lizards(最大流)
  3. 多线程环境的UI控件属性更新
  4. TIOBE Index for December 2015(转载)
  5. BUAA1389愤怒的DZY(最大值最小化)
  6. 【转】apache kafka技术分享系列(目录索引)
  7. Install Hive
  8. LPSTR、LPCSTR、LPWSTR、LPCWSTR、LPTSTR、LPCTSTR的来源及意义
  9. callback调用测试
  10. 淘宝JAVA中间件Diamond详解(2)-原理介绍
  11. 有关UITableViewCell的侧滑删除以及使用相关大神框架MGSwipeTableCell遇到的小问题
  12. BestCoder Round #20 部分题解(A,B,C)(hdu5123,5124,5125)
  13. Is it possible to implement a Firebug-like “inspect element” DOM element highlighter with client-side JavaScript?
  14. Java面试09|多线程
  15. Python自动化--语言基础8--接口请求及封装
  16. 初读 c# IL中间语言
  17. JSP连接MySQL时出现--错误:Access denied for user &#39;root&#39;@&#39;localhost&#39; (using password: YES)&#39;解决方案
  18. spark基础知识(1)
  19. Linux命令--tree
  20. Spring MyBatis多数据源(同包)

热门文章

  1. Stanford机器学习---第八讲. 支持向量机SVM
  2. MyBatis多数据源配置(读写分离)
  3. MySQL目录
  4. Spring事务传播、隔离等级
  5. 使用nginx的proxy_cache做网站缓存
  6. eclipse内存设置,tomcat内存设置,查看内存大小
  7. 烦烦烦SharePoint2013 以其他用户登录和修改AD域用户密码
  8. C# 支持多种语言
  9. 封装自己的ajax函数
  10. struts2中各种值栈问题