How to get the position of touch in a simple windows phone 8 app? - windows-phone-7

I am making an app for windows phone 8.
On one of the pages of the app, I want to get the co-ordinates of the touch.
I tried searching on net but I got results related to games.
I want to know is it possible to get the position of touch on canvas/or any other component in a simple app.
If yes, plz guide me through it.

Hook up to Tap event of the Control, and use GetPosition of event args.
<Canvas Tap="HandleTap/>
private void HandleTap(object sender, GestureEventArgs e)
{
Point point = e.GetPosition(null);
}

Related

Ways of detecting Windows Phone idle

I want to perform some calculations while the phone is in locked screen mode, regardless if the app is on the foreground or the background.
I know that in order to do this, I need to use OnObscured event from App class. However, it is only launched when the app is in the foreground, but not in the background.
So, I would like to know if exist any way to detect the phone state while the App runs the background.
I have thought something that would be crazy, but is to access an API property which is not allowed to use while the phone is in locked screen, and then catch the exception and, with that, get if the phone is active or sleeping.
I am open to hear new ideas.
I figured out a simple thing - maybe it will help you:
I assume that you already disabled Idle Detection to run your calculations in background.
So why not to create the variable in which you hold the state of the App? Since you have to launch first your App, so it goes to foreground and when Obscured is called and IsLocked = true, set the variable. Then you can check it whenever you want:
public MainPage()
{
InitializeComponent();
App.RootFrame.Obscured+=RootFrame_Obscured;
App.RootFrame.Unobscured+=RootFrame_Unobscured;
}
private bool AppIsLocked = false;
private void RootFrame_Unobscured(object sender, EventArgs e)
{
if (AppIsLocked) AppIsLocked = false;
}
private void RootFrame_Obscured(object sender, ObscuredEventArgs e)
{
if (e.IsLocked) AppIsLocked = true;
}
Is this what you searching for Running a Windows Phone Application under the lock screen?
This article describes - how to avoid tombstoning. But you can't do a lot of work under lock screen.

Windows phone calculate distance

I am working on windows phone app.
public void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs e)
{}
Does this event work on every position change when application run in background not in foreground?
I want to calculate traveled distance at every 10 seconds while app run in background.How can i do it?
You can't. Microsoft is very specific on what is allowed to run in the background.
See this article for more info.

Showing message without any buttons in Windows Phone

I would like to create an info message box in Windows Phone.
It should look like a single text message, that's shown on screen for X seconds, and I doesn't have any confirmation buttons
as MessageBox.Show() does. It would be used to show user some messages about the app progress, like "file download started",
or "preferences saved".
I've tried to create something like that, using Popup element, but it seems a bit different of what I want to
make.
With the popup I can create something like that
canvas.MouseLeftButtonUp += new MouseButtonEventHandler(popupCloseEvent);
For example, which closes this popup when user taps it.
or something similar. (Maybe can implement a timer there to close it after a few secs)
Are there better ways to create such thing?
You could use the either the Message Dialog or Overlay controls in the Coding4Fun toolkit.
Found here: http://coding4fun.codeplex.com/
They are really simple and easy to use controls.
Try this
In Xaml:
<Popup Tap="Popup_Tap"></Popup>
In code behind:
private void Popup_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
(sender as Popup).IsOpen = false;
}
I hope it helps.

Is there any way to capture an image using the camera, programatically, without the user pressing a button?

I wish to take a picture on a timer, and process the image at regular intervals, but cannot find anything.
Any help would be appreciated,
Cheers.
There's no way for 3rd party apps to take a picture without the users interaction in this v1 release of the 3rd Party SDK. AR capability is on the radar for the platform team though, so watch this space.
Picture taking capability is currently provided through CameraCaptureTask.
FYI: In Windows Phone OS 7.1 (a.k.a. "Mango"), you can now programmatically capture an image from the camera using the PhotoCamera class. Fire-off a camera capture using the CaptureImage method. When the capture is available, you can access the image (and a thumbnail) from the arguments in the eventhandlers for CaptureImageAvailable and CaptureThumbnailAvailable.
This process is fully described in the following topic:
How to: Create a Base Camera Application for Windows Phone
In that sample, a button is used to trigger the call to CaptureImage, but in a real-world application a timer, like you suggested, would be much more appropriate. (we recommend using the hardware button for user-triggered photos, rather than a UI button. That is described here: How to: Access the Hardware Camera Shutter Button).
Here's the method that actually programmatically triggers the image capture, where cam is the PhotoCamera object:
private void ShutterButton_Click(object sender, RoutedEventArgs e)
{
// Capture a still image. Events are fired as the thumbnail
// and full resolution images become available.
try
{
cam.CaptureImage();
}
catch (Exception ex)
{
this.Dispatcher.BeginInvoke(delegate()
{
// Cannot capture an image until the previous capture has completed.
txtDebug.Text = ex.Message;
});
}
}
Note: PhotoCamera will throw an exception if you try to capture when another capture is in progress. You probably wouldn't have this problem with a timer-based app, but that is why the try/catch is used here. Also, BeginInvoke is used to access the UI thread and display the message in a textBlock on the corresponding page.
Hope that helps. Cheers

ImageMap behavior for tiles

Is it possible to determine the area on which a user clicked on a tile? Just like an image map in HTML. I would like to specify two buttons at an image tile and carry out different actions for each of them.
No. It is not possible to do this.
With respect of lauching your app, the tile works just like a shortcut on your PC desktop.
If you really need the behaviour you describe you'll have to create 2 applications - 1 for each launch action.
You can use something like this for every UI element.
private void ListBox_MouseEnter(object sender, MouseEventArgs e)
{
string pos= e.GetPosition(CanvasForLoop).X.ToString();
}

Resources