NavigationService.GoBack Not working in Windows Phone 8 - xaml

I'm developing windows phone 8 application.
My app page structure
1.WelcomePage(GetLocation)->
2.MainPage.xaml (Contain Six Button Ex:Hotels,Park,Temple...)->
3.Hotels(List of hotels present Click any of the Hotel)->
4.MoreDetailsPage(About the particular hotel In Browser with in the app)
Every thing work well upto reach MoredetailPage.In Moredetailpage I use NavigationService.GoBack(); OnBackkeyPress Event. If I press Back Key It's just close and exit from the App.
My Coding For Every Page:-
WelcomePage.xaml
On revgeocoding_QueryCompleted Event.
NavigationService.Navigate(new Uri("/Mainpage.xaml", UriKind.Relative));
MainPage.xaml
On Click Event.
this.NavigationService.Navigate(new Uri("/Hotels.xaml", UriKind.Relative));
Remove BackEntry for WelcomePage.xaml
protected override void OnNavigatedTo(NavigationEventArgs e)
{
NavigationService.RemoveBackEntry();
}
Hotels.xaml
On Listbox Tap Event.
this.NavigationService.Navigate(new Uri(string.Format("/Moredetailswebpage.xaml?Businessname={0}", passvalue), UriKind.Relative));
Moredetailswebpage.xaml
PhoneApplicationPage_Loaded Event
minbrowser.Navigate(new Uri(site, UriKind.Absolute));
OnBackKeyPress
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
try
{
base.OnBackKeyPress(e);
NavigationService.GoBack();
}
catch (Exception ex)
{
}
}
If I click hardware backkey from the MoreDetailpage.xaml . It's just close and exit from the app.
Plz tell how to solve this problem.Where I made mistake.

You said that on the Back Key Press your Application would quit?
here's what an example to not make it quit.
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
//Forces the app not to quit
e.Cancel = true;
}

Related

mistake with finish an activity in android

I have a main activity with 6 buttons. Every button starts a new activity.
The program worked well but after that I changed some graphical attributes and made some changes in the theme a mistake occurred . I should press back button 3 times to return from an activity or exit from the main activity. Any suggestions?
btn_weeks.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
Intent i = new Intent(MainActivity.this, Weeks.class);
startActivity(i);
return false;
}
});
Buttons should implements View.OnClickListener and not OnTouchListener.

the app keep the loading dialog when goes to stop() codename one

im having a problem when the app is down. the function stop() in the file "my application" save the current Form, but when i try to make a download and i put the app in background task , when try go back to the app , the app is keeping the dialog "loading" forever... i don't know how to finish the current download in background and show the next Form when go back to the app.
the code of "myapplication" file:
private Form current;
public void start() {
if(current != null){
current.show();
return;
}
new StateMachine("/theme");
}
public void stop() {
current = Display.getInstance().getCurrent();
}
Just do something like:
if(current instanceof Dialog) {
current = null;
}

Android WakeLocks not working

Right at the acquire() it fails. Eclipse doesn't say what the error was. It just stops the execution on my emulator and gives me that "Class File Editor" "Source not found" display.
public class MyAppActivity extends Activity {
private PowerManager pManager;
private PowerManager.WakeLock wakeLock;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.main);
allocStructs();
}
private void allocStructs() {
// I've tried this with "getBaseContext()" and with "this"
// same results. I get a pManager and a wakeLock
// Then it crashes when I attempt to acquire
pManager = (PowerManager)getBaseContext().getSystemService(
Context.POWER_SERVICE);
wakeLock = pManager.newWakeLock(
PowerManager.FULL_WAKE_LOCK, "full");
}
public void onWakeLockButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
if (!checked) {
return;
}
if (!wakeLock.isHeld()) {
wakeLock.acquire(); // fails here
}
}
}
OK I got my answer and am embarassed. The quick answer is I didn't get the permission for Wake Locks in the manifest.
I had read the part that you need to get the wakelock permission but I thought in the debug emulator you may not need it. Or it may get settled just by pressing . Then by the way it was stopping I thought it was a crash, not a permission violation. So I added this:
try {
wakeLocks.acquire();
} catch (Exception e) {
e.printStackTrace();
return;
}
And sure enough it was a permission violation. This link told me how to add the permission to my manifest.
How to get an Android WakeLock to work?
I couldn't figure out how to add the permission thru those menus, but I added this line to the xml source directly.
<uses-permission android:name="android.permission.WAKE_LOCK" />
Then it works.

What the right time for registering listener for Share/Search charms

I need to register different share charm listener for every page. I have 2 pages. I added following code in every one:
DataTransferManager.GetForCurrentView().DataRequested += App_DataRequested;
I added it in constructor of one page and in UserControl_Loaded event of another (first page just doesn't have UserControl_Loaded so why I added it directly to constructor). At the moment when second page tryting to load, I got exception:
WinRT information: An event handler has already been registered
Additional information: A method was called at an unexpected time.
Where should I place it and what is "right" time to do this??
Also it looks confusing that we have different DataTransferManager for every view, but only one is active at current time. Ever more, I noticed, if you add only one listener for first page, other pages will share this listener anyway. If I have only one shared listener for all pages, is it correct register it in app.xaml.cs?
The way I resolved this issue was to deregister the event in the onNavigatedfrom event as below:
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
DataTransferManager.GetForCurrentView().DataRequested -= App_DataRequested;
base.OnNavigatedFrom(e);
}
In BasePage.cs in constructor I added
public BasePage()
{
if (!_isListenToDataRequested)
{
_isListenToDataRequested = true;
DataTransferManager manager = DataTransferManager.GetForCurrentView();
manager.DataRequested += AppDataRequested;
}
}
private async void AppDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
IShareable shareable = Frame.Content as IShareable;
if (shareable != null)
{
DataRequestDeferral deferral = args.Request.GetDeferral();
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => shareable.AppDataRequested(sender, args));
deferral.Complete();
}
}
And all my pages look like
public sealed partial class ContentPage : IShareable
{
public void AppDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{...}
}
Another solution was run this as below
private DataTransferManager dataTransferManager;
Put this in page loaded event
this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new DispatchedHandler(() =>
{
this.dataTransferManager = DataTransferManager.GetForCurrentView();
this.dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.OnDataRequested);
}));
And
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
// Unregister the current page as a share source.
this.dataTransferManager.DataRequested -=
new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>
(this.OnDataRequested);
}
I'd suggest doing it in the navigating events, the OnNavigatingFrom event will be triggered before the OnNavigatingTo of the page you're going to so you won't have this problem.
protected override Task OnNavigatingTo(WinRTXamlToolkit.Controls.AlternativeNavigationEventArgs e)
{
DataTransferManager.GetForCurrentView().DataRequested += dataTransfer_DataRequested;
return base.OnNavigatingTo(e);
}
protected override Task OnNavigatingFrom(WinRTXamlToolkit.Controls.AlternativeNavigatingCancelEventArgs e)
{
DataTransferManager.GetForCurrentView().DataRequested -= dataTransfer_DataRequested;
return base.OnNavigatingFrom(e);
}
//Note: This is the WinRT Xaml Toolkit version of the events, but the standard events will work the same way.

CF keyDown event (timed)

I need an event for my CF application, that would trigger after user has pressed an held his finger on the control for 2 seconds. What event can i use, since keyDown event is already used.
Well, KeyDown is pretty irrelevant for capturing the length of time a finger is pressed. The use of the finger relates to the events Click, MouseDown, MouseUp and MouseMove.
To get the behaviour you're after, the events you should be interested in are MouseDown and MouseUp.
I suggest the best way to do this would be to create your own control base class. Here's one I made earlier (not tested, but should give you a general idea of what to do):
public partial class BaseControl : UserControl
{
public BaseControl()
{
InitializeComponent();
base.MouseDown += new MouseEventHandler(BaseControl_MouseDown);
base.MouseUp += new MouseEventHandler(BaseControl_MouseUp);
MouseHeldTimer = new Timer();
MouseHeldTimer.Interval = 2000;
MouseHeldTimer.Tick += new EventHandler(mouseHeldTimer_Tick);
}
protected Timer MouseHeldTimer;
protected bool MouseIsDown;
void mouseHeldTimer_Tick(object sender, EventArgs e)
{
this.MouseHeldTimer.Enabled = false;
if (this.MouseHeldDown != null)
{
this.MouseHeldDown(sender, e);
}
}
void BaseControl_MouseDown(object sender, MouseEventArgs e)
{
this.MouseHeldTimer.Enabled = true;
}
void BaseControl_MouseUp(object sender, MouseEventArgs e)
{
this.MouseHeldTimer.Enabled = false;
}
public event MouseHeldDownHandler MouseHeldDown;
public delegate void MouseHeldDownHandler(object sender, EventArgs e);
}
Basically, the MouseHeldTimer will start with an interval of 2 seconds the moment the user touches their finger to the screen. If the user lifts their finger the timer is stopped. If the user's finger is down for longer than 2 seconds, the delegate event MouseHeldDown will fire. You can then capture this event on your form by doing the following:
control.MouseHeldDown+= new EventHandler(control_MouseHeldDown);
Alternatively, if you only care about the form, you can just use the Form's DoubleClick event as that will fire after holding the mouse down for a second or two.