是否可以处理Windows Phone Toolkit HubTile的Visual State Changed事件?

时间:2013-10-25 09:39:16

标签: windows-phone-7 windows-phone-8

当Visual State更改为Flipped时,我想更改HubTile的Source(Image)但是我似乎无法从Windows Phone Toolkit获取用于HubTile控件的VisualStateManager.GetVisualStateGroup。 / p>

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

我见过以下帖子,遗憾的是不包含代码段: -

Changing image source when Hubtile "resets"

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

如果你能分享一些想法,我将非常感激?

1 个答案:

答案 0 :(得分:0)

基于以下博文: -

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