【发布时间】:2013-04-04 09:26:58
【问题描述】:
我有一个项目需要能够在屏幕上拖动用户控件(托管在网格中)。这适用于以下代码:
void MyUserControl_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
var ct = (CompositeTransform) RenderTransform ?? new CompositeTransform {CenterX = 0.5, CenterY = 0.5};
ct.TranslateX += e.Delta.Translation.X;
ct.TranslateY += e.Delta.Translation.Y;
}
问题是用户控件可以一直拖出屏幕区域。为了防止这种情况,我尝试了这里显示的示例:http://msdn.microsoft.com/en-us/library/system.windows.uielement.manipulationdelta.aspx
不幸的是,它使用 containingRect.Contains(shapeBounds) 而在 Windows 8 中,我们需要用 Point 替换 shapeBounds(this is a rect)。我不确定如何使用它。
那么问题是我们如何确保用户控件或任何 UIElement 不能被拖出 windows 8 商店应用程序中的 Window.Current.Bounds 区域?
谢谢。
编辑:有关 xaml 结构的更多详细信息:
主页包含一个水平和垂直对齐设置为拉伸的网格。根据需要将用户控件添加到此网格中。每个用户控件都有一个包含 3 个不同视图(全屏、窗口和小)的父网格。根据用户的选择显示视图。只有在显示窗口网格时才必须应用拖动行为。所以我们有这个
<Grid> <!-- this is the parent grid on mainpage with horizontal and vertical alignment to stretch-->
<Grid> <!-- this is the usercontrol's main grid (added to above grid via code). This grid must be draggable if the below grid is window -->
<Grid /> <!-- this is one of the child grids that is shown based on user's choice (fullscreen, window or small view).-->
</Grid>
</Grid>
在更改布局方面我没有太多选择。在用户控件中使用上面的 ManipulationDelta 事件(根据显示的子网格启用/禁用),我能够获得拖动行为,但控件可能会超出窗口范围。那么有什么方法可以通过代码而不是 xaml 在 WinRTXamlToolkit 中添加以下 FlickBehavior 或根据某些条件启用/禁用行为?
<i:Interaction.Behaviors>
<views:HeavyFlickBehavior />
</i:Interaction.Behaviors>
【问题讨论】:
-
你的用户控件在哪里?在画布上?
-
它在网格内,但如果使用网格有问题,可以移动到画布上。
-
我认为您必须使用
TransformToVisual和TransformPoint手动检查用户控件的边缘是否接近边界 -
您拥有源代码,因此您可以轻松添加或删除该行为。您可以简单地清除或设置控件上的 ManipulationMode 以启用/禁用该行为。顺便说一句 - 不要使用
HeavyFlickBehavior- 这是 WinRT XAML 工具包中用于测试内存泄漏的压力测试类。每个实例使用 100MB 的 RAM! :) -
谢谢菲利普!不知道我怎么没想到启用/禁用操作模式。如果您不介意,最后一件事 - 有没有办法通过代码而不是 xaml 附加行为?
标签: c# windows-8 windows-runtime windows-store-apps winrt-xaml