【问题标题】:Scrolling with mouse Win 8.1 Metro Hub Navigation用鼠标滚动 Win 8.1 Metro Hub Navigation
【发布时间】:2014-01-03 14:31:55
【问题描述】:

我正在尝试找到一种解决方案,以允许集线器在到达左边界或右边界时用鼠标平移。我已经实现了我从各种来源收集到的以下代码。

` private void theHubPointerMoved(object sender, PointerRoutedEventArgs e)
    { Windows.UI.Xaml.Input.Pointer ptr = e.Pointer;

        if (ptr.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Mouse)
        {
            Windows.UI.Input.PointerPoint ptrPt = e.GetCurrentPoint(null);
              if (ptrPt.Position.X < this.ActualWidth - 20)
                if (ptrPt.Position.X > 20)
                {
                    //Do the SCROLLING HERE
                    var xcord = Math.Round(ptrPt.Position.X, 2);
                    var ycord = Math.Round(ptrPt.Position.Y, 2);
                }
        }
           e.Handled = true;
    }`

因此,当鼠标位于屏幕边缘时,相对容易看到。我认为简单地使用 MyHub.ScrollViewer.ScrollToHorizo​​ntalOffset(xcord); 会很容易,但 Hub Scrollviewer 不会公开此 ScrollToHorizo​​ntalOffset 函数。

有人可以帮忙吗?

谢谢。

【问题讨论】:

    标签: windows-8 windows-store-apps scrollview winrt-xaml horizontaloffset


    【解决方案1】:

    哦,暴露了。如果你能处理挖掘它。方法如下:

    http://xaml.codeplex.com/SourceControl/latest#Blog/201401-ScrollHub/MainPage.xaml.cs

    在下面的示例中,它滚动到特定的中心部分。但我希望,您应该能够轻松地调整它以适应您的特定需求。

    private void ScollHubToSection(Hub hub, HubSection section)
    {
        var visual = section.TransformToVisual(this.MyHub);
        var point = visual.TransformPoint(new Point(0, 0));
        var viewer = Helpers.FindChild<ScrollViewer>(hub, "ScrollViewer");
        viewer.ChangeView(point.X, null, null);
    }
    

    使用这个:

    public class Helpers
    {
        public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject
        {
            if (parent == null) return null;
            T foundChild = null;
            int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < childrenCount; i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                T childType = child as T;
                if (childType == null)
                {
                    foundChild = FindChild<T>(child, childName);
                    if (foundChild != null) break;
                }
                else if (!string.IsNullOrEmpty(childName))
                {
                    var frameworkElement = child as FrameworkElement;
                    if (frameworkElement != null && frameworkElement.Name == childName)
                    {
                        foundChild = (T)child;
                        break;
                    }
                }
                else
                {
                    foundChild = (T)child;
                    break;
                }
            }
            return foundChild;
        }
    }
    

    祝你好运!

    【讨论】:

    • 谢谢杰瑞会检查一下。
    • 杰瑞你就是那个男人!这几天我一直在努力解决这个问题。这是一种享受。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多