我需要一个可以在屏幕上跟随我的手指(鼠标)的图像...以下代码在纵向模式下工作正常,但它在景观模式下完全陷入困境,是否有人对此进行过处理?
<Image Height="68" HorizontalAlignment="Left" Margin="872,388,0,0" Name="imgStarPoint" Stretch="Fill" VerticalAlignment="Top" Width="54" Source="/GetMousePoint;component/StarT.png" ManipulationCompleted="imgStarPoint_ManipulationCompleted">
<i:Interaction.Behaviors>
<el:MouseDragElementBehavior x:Name="imgStar"/>
</i:Interaction.Behaviors>
</Image>
和代码背后:
void Touch_FrameReported(object sender, TouchFrameEventArgs e)
{
var PrimaryPoint = e.GetPrimaryTouchPoint(null);
imgStar.X = PrimaryPoint.Position.X;
imgStar.Y = PrimaryPoint.Position.Y;
txt1.Text = PrimaryPoint.Position.X + "." + PrimaryPoint.Position.Y;
}
有人有办法在横向模式下设置我的指尖上的图像吗?
答案 0 :(得分:1)
修改强>
好的,出于某种原因,我使用的是WP Toolkit的Gesture监听器,它会在每个方向模式中报告正确的X任意Y.在您的情况下,您需要检测您所处的方向模式并进行必要的调整。
似乎当方向在横向时,轴被切换。在横向左侧模式下,X轴反转,在横向右侧模式下,Y轴反转。以下代码应该可以解决您的问题:
bool _switchAxis;
bool _invertX ;
bool _invertY;
private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e) {
_switchAxis = (e.Orientation | PageOrientation.LandscapeLeft | PageOrientation.LandscapeRight) == (PageOrientation.LandscapeLeft | PageOrientation.LandscapeRight);
_invertX = e.Orientation == PageOrientation.LandscapeLeft;
_invertY = e.Orientation == PageOrientation.LandscapeRight;
}
private void Touch_FrameReported(object sender, System.Windows.Input.TouchFrameEventArgs e) {
var width = Application.Current.Host.Content.ActualWidth;
var height = Application.Current.Host.Content.ActualHeight;
var primaryPoint = e.GetPrimaryTouchPoint(null);
if (_switchAxis) {
if (_invertY) imgStar.X = height - primaryPoint.Position.Y; else imgStar.X = primaryPoint.Position.Y;
if (_invertX) imgStar.Y = width - primaryPoint.Position.X; else imgStar.Y = primaryPoint.Position.X;
} else {
imgStar.X = primaryPoint.Position.X;
imgStar.Y = primaryPoint.Position.Y;
}
}
您需要将OrientationChanged
事件添加到您的页面xaml:
<phone:PhoneApplicationPage
<!-- ... -->
OrientationChanged="PhoneApplicationPage_OrientationChanged"
/>
在横向模式下,可见的应用程序栏和系统托盘会为您弄乱X.
如果您有应用栏,请将其模式设置为最小化
ApplicationBar.Mode = ApplicationBarMode.Minimized
您还需要隐藏系统托盘以避免在X上进行手动调整。在页面加载事件上执行此操作
的Xaml:
<phone:PhoneApplicationPage
<!-- stuff -->
Loaded="PhoneApplicationPage_Loaded"
/>
代码背后:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) {
SystemTray.IsVisible = false;
}
此外,您还需要从X中扣除image.Width / 2,从Y中扣除image.Height / 2,使其完全位于指尖的中心。
imgStar.X = PrimaryPoint.Position.X - (img.Width/2);
imgStar.Y = PrimaryPoint.Position.Y - (img.Height/2);
这应该解决问题。