Windows phone 8.1 BackPressed not working properly - windows-phone-8.1

Windows phone 8.1 new to world. Basic function is back button click. Is that function not working properly is this windows phone 8.1. Is that behavior or i'm made mistake.
Below code using in Homepage but this code calling from all other class too while clicking back. I need to access below method only on Home page .
Please check below code and refer me good solution.
Please look my code:
public HomePage()
{
this.InitializeComponent();
Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
}
Thanks

It is working properly. The BackPressed event is working app-wide. Two options that come to my mind:
write eventhandler that would recognize the Page in which you currently invoke it - simple example can look like this:
private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
Frame frame = Window.Current.Content as Frame;
if (frame == null) return;
if (frame.Content is HomePage)
{
e.Handled = true;
Debug.WriteLine("I'm in HomePage");
}
else if (frame.CanGoBack)
{
frame.GoBack();
e.Handled = true;
}
}
second option - subscribe to Windows.Phone.UI.Input.HardwareButtons.BackPressed when you enter the Page and unsubscribe when you leave the Page. Note that in this way there are some pitfalls - you have to handle properly OnNavigatedTo, OnNavigatedFrom, Suspending and Resuming (more about Lifecycle here). Note also that the subscription should be done before others - for example NavigationHelper.
Some remarks - the above code should work, but it also depends on other circumstances:
if there is something other subscribed to BackPressed before (in App.xaml.cs) - remember that usually events are fired in order they were subscribed
check if you are using NavigationHelper - it also subscribes to BackPressed
remember not to subscribe multiple times
remember to allow the User to leave your HomePage

Related

GPS running in background when Here Drive+ is running

i have a problem running my wp (8.0) app properly in the background when HERE Drive+ is running in the foreground. my app is a location based app.
i've setup a little demo project to reproduce and isolate and simplify the problem.
I have a GeoLocator, which is checking and displaying the current location in the PositionChanged event to a label, when in foreground. when running in background, it displays a toast every 5 seconds (to show me, that the PositionChanged event is still triggered).
pretty forward stuff, that works.
public MainPage()
{
InitializeComponent();
DataContext = this;
App.LocationWatcher.ReportInterval = 5000;
App.LocationWatcher.DesiredAccuracy = PositionAccuracy.High;
App.LocationWatcher.PositionChanged += LocationWatcherOnPositionChanged;
App.LocationWatcher.StatusChanged += LocationWatcherOnStatusChanged;
}
private void LocationWatcherOnStatusChanged(Geolocator sender, StatusChangedEventArgs args)
{
DisplayToast("Status:", args.Status.ToString());
}
private void LocationWatcherOnPositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
if (!App.IsRunningInBackground)
{
Dispatcher.BeginInvoke(() => {
this.tbkLastUpdatedValue.Text = DateTime.Now.Ticks.ToString();
this.tbkLatitudeValue.Text = args.Position.Coordinate.Latitude.ToString();
this.tbkLongitudeValue.Text = args.Position.Coordinate.Longitude.ToString();
});
}
else
{
DisplayToast("Location:", args.Position.Coordinate.Latitude.ToString());
}
}
So, now the problem: When my app runs in background, it displays it's toasts, when i run any other app (including the normal map, which actually uses gps), but when i run HERE Drive+, my PositionChanged and my StatusChanged events dont get triggered anymore.
there is an app on the marketplace that is capable to run in background when here drive+ is running in foreground, as stated in the marketplace comments (in german only)
any ideas how to solve this or what may cause that problem?
The Windows SDK is no longer. An alternative could be to use the HERE Maps API for Javascript, which supports vector map data rendering.

How do you debug PhotoTaskChooser in WP8 on a physical device?

I have the simplest possible app. The UI is a page with nothing on it but a Tap method on a grid.
The code-behind looks like this...
public partial class MainPage : PhoneApplicationPage
{
private PhotoChooserTask _photoChooser;
// Constructor
public MainPage()
{
InitializeComponent();
_photoChooser.Completed += OnPhotoChosen;
}
private void OnTap(object sender, System.Windows.Input.GestureEventArgs e)
{
_photoChooser.Show();
}
private void OnPhotoChosen(object sender, PhotoResult result)
{
}
}
Now, what happens is that when I debug this application ON THE DEVICE, it briefly shows the photo chooser but then immediately deactivates...I assume because the photo chooser has taken focus. But from everything I've read, this should NOT be happening because the PhotoChooserTask's Completed event has been wired up in the constructor for my page, which should explicitly prevent my app from deactivating when the photochooser is active.
What's even more confusing is that the app seems to work when I'm NOT debugging it. Once I've selected a photo in this scenario, my app regains the foreground.
Is this a bug with the debugger or something else?
So it turns out the solution is to debug using "Start New Instance" in Visual Studio rather than just hitting F5.

Windows Phone: How to Navigate another page which is using DispatcherTimer?

I have two pages Main.xaml which includes only a button and another page timer.xaml which includes a timer. After pressing the button in the main page I want to go to the another page and start timer. I am using following code:
enter code here
**Main Page:**
private void Start_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/timer.xaml", UriKind.Relative));
}
**timer Page:**
public Page1()
{
InitializeComponent();
dispatcherTimer.Interval = new TimeSpan(0, 0, 1, 0, 0);
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Start();
counter=0;
count.Text = counter.ToString();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
counter++;
count.Text = counter.ToString();
}
I can not see time in the timer page however, after pressing the button it will navigate to the timer page correctly but I can not see updates in my timer page. I am debugging my code and it seems that DistpacherTimer() works correctly but my timer page does not update. Do you know how can I fix this problem?
Finally, I could find the problem. the code that you can see above was not whole code that I have written. Actually, in my real code I was sending a list <> from main page to timer page. Passing data from one page to another page is a little tricky. My main problem was that I did not send the data from main page to timer page in correct way, so that caused problem. Thus, there was nothing wrong with my DispatcherTimer() class or Event handler.
In other words, the code above works well if you do not pass data (e.g. list, array and so on) from a page to another page in a wrong way.

How To Close MessageBox Programmatically in WP8?

I'm aware that there are already questions questions asked regarding closing a MessageBox programmatically . But the solution to those questions is to use a timer.
I am trying to develop an NFC application, so when i create a MessageBox, it contains a message Please Tap Your NFC. So technically, the Timer isn't helpful. I need a way to close or dispose a MessageBox.
Please advice.
You can create a custom window yourself as described in the question you linked. However, instead of a timer you can and include a Hide method which you can call once NFC connection event occurs.
Alternatively, you could get Coding4Fun toolkit and use MessagePrompt class which already includes a Hide method.
From lieska at MessageBox.Show in App Closing/Deactivated events
Register BackKeyPress event on RootFrame.
RootFrame.BackKeyPress += BackKeyPressed;
private void BackKeyPressed(object sender, CancelEventArgs e)
{
var result = (MessageBox.Show("Do you want to exit XXXXX?", "Application Closing", MessageBoxButton.OKCancel));
if (result == MessageBoxResult.Cancel)
{
// Cancel default navigation
e.Cancel = true;
}
}

removing mouse events/controls from swing components with glasspane

I have a client-server application and i am using swing in the client side. My swing client has one main window (jframe) and lots of panels, toolbars and menubar in it.
I want to remove all client action/mouse events (or simply grab and do nothing) while client is waiting response from server by means of glasssPane.
Here is the code i wrote:
private final static MouseAdapter mouseAdapter = new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
System.out.println("MouseClicked..!");
}
};
private static Cursor WAIT_CURSOR = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
private static Cursor DEFAULT_CURSOR = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
and
public static void startWaitCursor(JComponent comp)
{
MainWindow root = ((MainWindow) comp.getTopLevelAncestor());
root.getGlassPane().setCursor(WAIT_CURSOR);
root.getGlassPane().addMouseListener(mouseAdapter);
root.getGlassPane().setVisible(true);
}
public static void stopWaitCursor(JComponent comp)
{
MainWindow root = ((MainWindow) comp.getTopLevelAncestor());
root.getGlassPane().setCursor(DEFAULT_CURSOR);
root.getGlassPane().setVisible(false);
}
but i am not able to manage the grab mouse events. Changing cursors at the glassPane is working fine but either i am not able to add mouseAdapter or am not able to make glasssPane become to the top level component.
Any idea?
Thanks.
I realized that my code is working but my problem is threading related. My code was something like:
startWaitCursor();
work(); // server request that takes time
stopWaitCursor();
and changed it to:
startWaitCursor();
SwingUtilities.invokeLater(new Runnable() {
poblic void run() {
try
{
work(); // server request
}
finally
{
stopWaitCursor();
}
by doing this modification i could see the settings i made in the startWaitCursor() method while client is waiting response from the server.
But stil there is a small problem. In startWaitCursor() method i desabled key, mouse and focus events for the glass pane but events are still captured by main frame even glassPane is displayed.
addMouseListener(new MouseAdapter() {});
addMouseMotionListener(new MouseMotionAdapter() {});
addKeyListener(this);
setFocusTraversalKeysEnabled(false);
After server response reached to client and stopWaitCursor() method is invoked the events handled in the main frame.
If i disable the main frame of my application while client is waiting than cursor is not being changed to wait_cursor, if i am not disable the main frame then cursor is being changed but the events are queued.
cheers...
After digging swing threads issues couple of days, i finally found the real answer: SwingWorker
Now my final code is something like,
startWaitCursor();
SwingWorker worker = new SwingWorker() {
public Object doInBackground()
{
doWork(); // time consuming server request
return null;
}
public void done()
{
stopWaitCursor();
}
};
worker.execute();
In startWaitCursor() method i set the glasspane visible (with alpha valued background), display a message to warn the user time consuming job is doing, set the cursor to wait_cursor (hourglass) and consume all the key, mouse events. That is it.
And by using SwingWorker my client is actually responsive (it is working as if no server request is made) but since i display the glasspane and consume all key and mouse events it feels like irresponsive.
What a relief.. SwingWorker rocks...
cheers..