Windows Phone 7 Cancel hardware back button not working - c#

I need to cancel the device back button event. I have tried the solution posted in Control press "back button" and disable close the application using a dialog for confirm - wp7, but it is not working for me. Am I doing something wrong? The application always exits whether ok or cancel is selected from the dialog box.
Here is my code...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
namespace GodTools
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
this.CordovaView.Loaded += CordovaView_Loaded;
BackKeyPress += OnBackKeyPressed;
}
private void CordovaView_Loaded(object sender, RoutedEventArgs e)
{
this.CordovaView.Loaded -= CordovaView_Loaded;
// first time load will have an animation
Storyboard _storyBoard = new Storyboard();
DoubleAnimation animation = new DoubleAnimation()
{
From = 0,
Duration = TimeSpan.FromSeconds(0.6),
To = 90
};
Storyboard.SetTarget(animation, SplashProjector);
Storyboard.SetTargetProperty(animation, new PropertyPath("RotationY"));
_storyBoard.Children.Add(animation);
_storyBoard.Begin();
_storyBoard.Completed += Splash_Completed;
}
void Splash_Completed(object sender, EventArgs e)
{
(sender as Storyboard).Completed -= Splash_Completed;
LayoutRoot.Children.Remove(SplashImage);
}
void OnBackKeyPressed(object sender, System.ComponentModel.CancelEventArgs e)
{
var result = MessageBox.Show("Do you want to exit?", "Attention!",
MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
// Do not cancel navigation
return;
}
e.Cancel = true;
}
}
}
see also, same problem from the Cordova side
"backbutton" event won't fire

For your information: If you write an application (not an XNA game) you shoul avoid canceling back button. Otherwise your app will be canceled on passing Marketplace sertification.
Also you can override OnBackKeyPress method with the same code;
protected override void OnBackKeyPress(CancelEventArgs e)
{
var result = MessageBox.Show("Do you want to exit?", "Attention!",
MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
base.OnBackKeyPress(e);
return;
}
e.Cancel = true;
}
update
I've just created a new 'Silverlight for windows phone" solution. Opened MainPage.xaml.cs file and added this code to it:
// Constructor
public MainPage()
{
InitializeComponent();
BackKeyPress += OnBackKeyPressed;
}
void OnBackKeyPressed(object sender, System.ComponentModel.CancelEventArgs e)
{
var result = MessageBox.Show("Do you want to exit?", "Attention!",
MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
// Do not cancel navigation
return;
}
e.Cancel = true;
}
so there is only one page in this project. And it works. The target platform is Windows Phone OS 7.1, I've checked it on Mango device and on the standard emulator. I think the problem is somewhere else. Maybe some code crashes you application while you are trying to cancel back event?
Please, try to check it on new simple project.

Related

Why can't I open a webpage in a new window using C# winforms? [duplicate]

This question already has answers here:
Process.Start to open an URL, getting an Exception?
(4 answers)
Closed 1 year ago.
I am very interested in C# winforms. I decided to make it open a browser window once I click on a link label with the default browser. I searched on the internet and found the following code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GUI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
try
{
VisitLink();
}
catch (Exception ex)
{
MessageBox.Show("Unable to open link that was clicked.");
}
}
private void VisitLink()
{
// Change the color of the link text by setting LinkVisited
// to true.
linkLabel1.LinkVisited = true;
//Call the Process.Start method to open the default browser
//with a URL:
System.Diagnostics.Process.Start("http://www.microsoft.com");
}
}
}
When I tried it in my code, I clicked on the link but nothing shows up. Not even an exception pops up, which made me confused. Can anyone help me? Thanks!
The link is correctly marked as visited after the click ? If not, the event is probably not even got fired. Check if you correctly add the callback on the click event

Trying to override NavigationMode.Back

I'm trying to develop a simple app for Windows Phone 8, and there are many requirements for the use of the Back Button. As I don't want the Back Button to simply GoBack in back stack, I'd like to pop up a message box to warn the user that this action will bring him back to main menu.
Problem is, this page has to be reloaded some times, and the following code stop working properly after 1 reload. The messagebox opens multiple times. And the more times I reload, the more MessageBox appears.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using BackButtonTests.Resources;
namespace BackButtonTests
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
NavigationService.Navigating += NavigationService_Navigating;
}
void NavigationService_Navigating(object sender, NavigatingCancelEventArgs e)
{
if (e.NavigationMode == NavigationMode.Back)
{
e.Cancel = true;
MessageBox.Show("Quit");
}
}
private void Restart_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/MainPage.xaml?reload=" + DateTime.Now.ToString(), UriKind.RelativeOrAbsolute));
//Use this fake reload query with unique value as a way to "deceive" the system, as windowsphone does not support NavigationService.Reload, and using simply the Uri of the same page will not properly load everything
}
private void Quit_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Quit");
}
}
}
This is just a test code I wrote, that shows exactly the problem I'm experiencing in my actual project. Of course there are 2 buttons written in xaml.
And the code won't work until you first reload the page, as it's not NavigatedTo when it's the front page (not a problem in my actual project).
Any clues of what I'm doing wrong?
NOTE: I'm not interested in changing the event handler (to OnBackKeyPress, for instance). I'm interested in understanding what's going on with the handler I chose (NavigationService.Navigating, NavigationMode.Back). Thanks
Updated following additional information that clarifies the questing
Changing your navigating event handler to will mean the event isn't fired on every page in the stack
void NavigationService_Navigating(object sender, NavigatingCancelEventArgs e)
{
NavigationService.Navigating -= NavigationService_Navigating;
if (e.NavigationMode == NavigationMode.Back)
{
e.Cancel = true;
MessageBox.Show("Quit");
}
}
No longer neccessary
Override OnBackKeypress instead of navigating
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
var DoYouWantToQuit = MessageBox.Show("Are you sure you want to Quit", "Quit", MessageBoxButtons.OkCancel);
if (DoYouWantToQuit != MessageBoxButton.Ok)
{
e.Cancel = true
}
base.OnBackKeyPress(e);
}

Need suggestions on improving a very simple c# server restarter

Recently I had an idea for a simple auto restarter for my server exe.
Basically all it does is
Checks if there is a process named WerFault running
If there is that means the server has crashed, so it closes both the server and WerFault
After that opens the server again.
The second thing it checks for is if the wServer is even running, if not it starts it up.
This all is within a timer with a delay of 10 seconds.
However I am very sure that there is a much more efficient way of doing this. I learned C# on my own (have not read a single book, all I know was acquired thru self learning + Google).
Also the reason why I want the server to be open 24/7 is because it will run on a vps.
Note: This is a windows forms application.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
Process[] prs = Process.GetProcesses();
foreach (Process pr in prs)
{
if (pr.ProcessName == "WerFault")
{
pr.Kill();
if (pr.ProcessName == "wServer")
{
pr.Kill();
Process.Start(#"C:\Users\Arturs\Dropbox\ROTMGServer-master\bin\Debug\wServer\wServer.exe");
}
return;
}
}
if (pr.ProcessName == "wServer")
{
return;
}
Process.Start(#"C:\Users\Arturs\Dropbox\ROTMGServer-master\bin\Debug\wServer\wServer.exe");
return;
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void timer2_Tick(object sender, EventArgs e)
{
}
}
}
}
You've written:
if (pr.ProcessName == "WerFault")
{
pr.Kill();
{
if (pr.ProcessName == "wServer")
{
// ...
It is unlikely that there will ever be a process pr whose ProcessName is both the string "WerFault" and the string "wServer".
Perhaps you want to move the second if outside of the first one.

Moving data from Form to Form Silverlight WP7 Expression blend - SketchFlow

I've tried this for one whole day and kinda gave up researching
What i basically need to do is to update the info on the first page after selecting this button on form2
What i did was this :
private void btnContinue_Click(object sender, System.Windows.RoutedEventArgs e)
{
// TODO: Add event handler implementation here.
Screen_1 S1 = new Screen_1();
S1.CO2.Text = "TEXT";
S1.CO3.Visibility = Visibility.Visible;
//NavigationService.Source=new Uri("/Screen_1.xaml", UriKind.Relative);
//NavigationService.Navigate(new Uri("/Screen_1.xaml", UriKind.Relative));
}
For some reason its not updating and navigation on expression blend doesnt work if i have code behind the button :(
Update:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Expression.Prototyping.WindowsPhone.Mockups;
using System.Windows.Navigation;
namespace Analyser2_v1Screens
{
public partial class Select : WindowsPhoneChrome
{
Screen_1 formOne = null;
public Select()
{
// Required to initialize variables
InitializeComponent();
}
public Select(Screen_1 formOneInstance)
{
// Required to initialize variables
InitializeComponent();
formOne = formOneInstance;
}
private void clickedC(object sender, System.Windows.RoutedEventArgs e)
{
// TODO: Add event handler implementation here.
//Screen_1 S1 = new Screen_1();
// S1.CO2.Content = "This is string content of a Button";
// S1.CO3.Visibility = Visibility.Visible;
// S1.test1.Text = "Tet";
//NavigationService.Source=new Uri("/Screen_1.xaml", UriKind.Relative);
//NavigationService.Navigate(new Uri("/Screen_1.xaml", UriKind.Relative));
formOne.test1.Text ="test";
}
}
}
STill not perfect enough
Please share a snippet of your code. From I understand from your question, you can pass form 1's instance to form 2's constructor and alter elements of form1 from there.
// form 1 var
FormOne formOne = null;
// form 2's constructor
public FormTwo(FormOne formOneInstance)
{
/*initialization etc*/
formOne = formOneInstance;
}
// some method to alter an element in form 1
private void AlterSomthingInFormOne()
{
formOne.SomeString = "Whatever value you'll need";
}

Updating parent process GUI on receiving Process.Exited event - WPF

I am trying to launch a process within an application. The code below simply starts notepad on clicking the button of the main GUI. Now when the notepad is launched the button is disabled. I have subscribed to the Process.Exited even to receive notification when the notepad application closes. Once the notification is received I would like to re-enable the button again.
However, code crashed when I call the button1.IsEnabled = true; It seems to that Process.Exit is not a part of the main GUI thread and hence when i try to update GUI within that it crashed. Also when i am debugging I dont receive any exception saying I am trying to access main thread from outside or something.
Is there a way to notify GUI when the child process exits?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Diagnostics;
namespace ProcessWatch
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Process pp = null;
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
pp = new Process();
pp.EnableRaisingEvents = true;
pp.Exited += new EventHandler(pp_Exited);
ProcessStartInfo oStartInfo = new ProcessStartInfo();
oStartInfo.FileName = "Notepad.exe";
oStartInfo.UseShellExecute = false;
pp.StartInfo = oStartInfo;
pp.Start();
button1.IsEnabled = false;
}
void pp_Exited(object sender, EventArgs e)
{
Process p = sender as Process;
button1.IsEnabled = true;
}
}
}
Try the following:
void pp_Exited(object sender, EventArgs e){
Dispatcher.BeginInvoke(new Action(delegate {
button1.IsEnabled = true;
}), System.Windows.Threading.DispatcherPriority.ApplicationIdle, null);
}

Categories

Resources