1

当 Visual State 更改为 Flipped 时,我想更改 HubTile 的源(图像),但是我似乎无法让 VisualStateManager.GetVisualStateGroups 从 Windows Phone 工具包中为 HubTile 控件工作。

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

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

Hubtile“重置”时更改图像源

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

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

4

1 回答 1

0

Based on the following blog post :-

http://blogs.msdn.com/b/ptorr/archive/2010/07/23/how-to-detect-when-a-list-is-scrolling-or-not.aspx

I came up with the following :-

    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;
    }
于 2013-10-25T10:21:54.207 回答