C# 相对于屏幕的触摸位置。windows phone
嘿,我正在使用mvvm模式为WindowsPhone8开发一个应用程序 我想找到的是相对于屏幕的位置。这样我就可以知道用户在屏幕上按了什么位置,无论是缩放还是其他。只是相对于屏幕的位置,因为这样我可以计算屏幕相对于缩放和位置的大小 我想要的和这个一样 这意味着我不能使用TransformToVisual,因为它需要UIElement。有人对这个问题有想法吗 额外的 为了强调这个问题,我知道如何在画布中获得单击的位置。我的问题是一个位置可以在屏幕上的许多地方C# 相对于屏幕的触摸位置。windows phone,c#,mvvm,windows-phone-8,position,screen,C#,Mvvm,Windows Phone 8,Position,Screen,嘿,我正在使用mvvm模式为WindowsPhone8开发一个应用程序 我想找到的是相对于屏幕的位置。这样我就可以知道用户在屏幕上按了什么位置,无论是缩放还是其他。只是相对于屏幕的位置,因为这样我可以计算屏幕相对于缩放和位置的大小 我想要的和这个一样 这意味着我不能使用TransformToVisual,因为它需要UIElement。有人对这个问题有想法吗 额外的 为了强调这个问题,我知道如何在画布中获得单击的位置。我的问题是一个位置可以在屏幕上的许多地方 例如,位置(x,y)可以位于左上角和右
例如,位置(x,y)可以位于左上角和右上角。但我如何知道点相对于屏幕的位置,即在哪个角落 您需要wptoolkit才能获得积分 从Nuget导入WPToolkit Cmd:
安装软件包WPtoolkit
在XAML网格中添加此代码
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener
Tap="GestureListener_Tap"/>
</toolkit:GestureService.GestureListener> <TextBlock x:Name="focusBracket"
Text="*"
FontSize="48"
Visibility="Collapsed" />
我认为您可以尝试使用Xna-它工作得很好,我认为它可以满足您的需求:
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework;
public MainPage()
{
InitializeComponent();
TouchPanel.EnabledGestures = GestureType.Tap;
this.Tap += MainPage_Tap;
}
private void MainPage_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
GestureSample gesture = TouchPanel.ReadGesture();
Vector2 vector = gesture.Position;
int positionX = (int)vector.X;
int positionY = (int)vector.Y;
}
左上角是(0,0),无论应用程序的方向如何,都会得到一个相对于它的位置。编辑-几句话
请注意,您也可以在CompositionTarget.Rendering或Timer中检查TouchInput,这取决于您想要实现的目标。
还请注意,当您使用XNA时,有时可能需要执行以下操作:
FrameworkDispatcher.Update();
编辑2-夹点示例如果您想使用捏或其他手势,可以如下所示:
public MainPage()
{
InitializeComponent();
TouchPanel.EnabledGestures = GestureType.Pinch;
this.ManipulationCompleted+=MainPage_ManipulationCompleted;
}
private void MainPage_ManipulationCompleted(object sender, System.Windows.Input.ManipulationCompletedEventArgs e)
{
if (TouchPanel.IsGestureAvailable)
{
GestureSample gesture = TouchPanel.ReadGesture();
Vector2 vector = gesture.Position;
int positionX = (int)vector.X;
int positionY = (int)vector.Y;
// do what you want with your positin
// there are also some more properties in which you may be interested
// also TouchPanel has properites like TouchPanel.DisplayWidth and DisplayHeight if you need them
}
}
至于FrameworkDispatcher.Update()-我想你可以在OnNavigetedTo()中调用它。谢谢你,我会尝试实现它并返回:)这似乎是相对于画布的?viewfinderCanvas?因为我可以在画布中得到我的位置,但我需要知道这个位置与屏幕的关系。这就是代码的作用?我将测试它,但它似乎是相对于viewfinderCanvas的?是的,它需要Canvas。。很抱歉没有添加代码,好的,所以它给出了相对于此的位置,而不是屏幕?@Siram看起来您是在XAML后面的代码中完成所有这些,但是如果我使用MVVM结构,有没有一种微笑的方式呢?看起来很有趣,我明天会尝试看看。我已经试过你的代码了,看起来它符合我的要求。但这不是我需要它的活动。我想把它用于夹点活动。然而,我一直无法做到这一点,你能帮我把它转换为使用捏代替吗?还有FrameworkDispatcher.Update,什么时候调用它?@JTIM我已经在编辑2中添加了,你问了什么。也可以看看其他的手势类型,也许你会对它们感兴趣。我还为ManiplationCompleted添加了Pinch-注意,它可以是ManiplationStarted,如果需要,也可以是delta。好的,谢谢,我会试试这个。我尝试了操纵三角洲,但没有成功。但我现在就试试,thxit似乎可以工作,不过我正试图使用Icommand将其添加到MVVM模式中。但由于构造函数中包含的代码,您不知道如何执行此操作。有什么想法吗?
public MainPage()
{
InitializeComponent();
TouchPanel.EnabledGestures = GestureType.Pinch;
this.ManipulationCompleted+=MainPage_ManipulationCompleted;
}
private void MainPage_ManipulationCompleted(object sender, System.Windows.Input.ManipulationCompletedEventArgs e)
{
if (TouchPanel.IsGestureAvailable)
{
GestureSample gesture = TouchPanel.ReadGesture();
Vector2 vector = gesture.Position;
int positionX = (int)vector.X;
int positionY = (int)vector.Y;
// do what you want with your positin
// there are also some more properties in which you may be interested
// also TouchPanel has properites like TouchPanel.DisplayWidth and DisplayHeight if you need them
}
}