我想改变HubTile溯源(图像)时,翻转可视化状态的变化,但是我似乎并没有能够得到从Windows手机工具箱的HubTile控制工作VisualStateManager.GetVisualStateGroups。
我相信,一旦我有VisualStateGroup然后我可以处理CurrentStateChanged事件,但是我似乎没有能够拿到小组第一。
我已经看到了以下螺纹不幸的是不包括代码片段: -
改变图像源时Hubtile“复位”
我也曾尝试使用VisualTreeHelper.GetChild,我并不需要考虑。
我会很感激,如果你能分享一些想法?
主要基于以下博客文章: -
http://blogs.msdn.com/b/ptorr/archive/2010/07/23/how-to-detect-when-a-list-is-scrolling-or-not.aspx
我想出了以下内容: -
bool alreadyHookedEvents = false;
List<string> _images = new List<string>();
int _index = 0;
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
if (_images.Count == 0)
{
_images.Add(@"\Images\1.jpg");
_images.Add(@"\Images\2.jpg");
_images.Add(@"\Images\3.jpg");
_images.Add(@"\Images\4.jpg");
_images.Add(@"\Images\5.jpg");
_images.Add(@"\Images\6.jpg");
_images.Add(@"\Images\7.jpg");
_images.Add(@"\Images\8.jpg");
_images.Add(@"\Images\9.jpg");
_images.Add(@"\Images\10.jpg");
_images.Add(@"\Images\11.jpg");
_images.Add(@"\Images\12.jpg");
}
if (alreadyHookedEvents)
return;
alreadyHookedEvents = true;
// Visual States are always on the first child of the control template
FrameworkElement element = VisualTreeHelper.GetChild(this.MyHubTile, 0) as FrameworkElement;
if (element != null)
{
VisualStateGroup group = FindVisualState(element, "ImageStates");
if (group != null)
{
group.CurrentStateChanged += (s, args) =>
{
if (group.CurrentState.Name == "Flipped")
{
_index++;
this.MyHubTile.Source = new BitmapImage(new Uri(_images[_index], UriKind.Relative));
}
};
}
}
}
VisualStateGroup FindVisualState(FrameworkElement element, string name)
{
if (element == null)
return null;
IList groups = VisualStateManager.GetVisualStateGroups(element);
foreach (VisualStateGroup group in groups)
if (group.Name == name)
return group;
return null;
}
T FindSimpleVisualChild<T>(DependencyObject element) where T : class
{
while (element != null)
{
if (element is T)
return element as T;
element = VisualTreeHelper.GetChild(element, 0);
}
return null;
}
文章来源: Is it possible to handle the Visual State Changed event for the Windows Phone Toolkit HubTile?