是否可以处理 Windows Phone Toolkit HubTile 的 Visual State Changed 事件? [英] Is it possible to handle the Visual State Changed event for the Windows Phone Toolkit HubTile?

查看:20
本文介绍了是否可以处理 Windows Phone Toolkit HubTile 的 Visual State Changed 事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当视觉状态更改为翻转时,我想更改 HubTile 的源(图像),但是我似乎无法让 VisualStateManager.GetVisualStateGroups 从 Windows Phone 工具包中为 HubTile 控件工作.

I would like to change the Source (Image) of a HubTile when the Visual State changes to Flipped however I don't seem to be able to get the VisualStateManager.GetVisualStateGroups working for the HubTile control from the Windows Phone Toolkit.

我认为一旦我有了 VisualStateGroup,我就可以处理 CurrentStateChanged 事件,但是我似乎无法获得该组.

I presume that once I have the VisualStateGroup I can then handle the CurrentStateChanged event however I don't seem to be able to get the group.

我看到了以下线程,不幸的是它没有包含代码片段:-

I have seen the following thread which unfortunately doesn't include a code snippet :-

Hubtile重置"时更改图像源

我也尝试过使用 VisualTreeHelper.GetChild,我认为这不是必需的.

I have also tried to use the VisualTreeHelper.GetChild, which I don't think is required.

如果您能分享一些想法,我将不胜感激?

I would be very grateful if you could share some ideas?

推荐答案

基于以下博文 :-

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;
    }

这篇关于是否可以处理 Windows Phone Toolkit HubTile 的 Visual State Changed 事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆