Replicate Hardware Back Button Functionality from UI in Windows Phone - windows-phone

I'm trying to write a windows phone application and am wondering if it's possible to have a software button in the UI that does the same thing as the hardware back button (just sends a system back command). I can't seem to find any information on how to do this online.

try this:
XAML:
<Button x:Name="btnBack" Content="Back" Click="btnBack_Click"></Button>
CS:
private void btnBack_Click(object sender, RoutedEventArgs e)
{
if (NavigationService.CanGoBack)
NavigationService.GoBack();
else
App.Current.Terminate();
}
This would work as same Back button. if any back-state is there it would go to that state or if not it would close (Terminate) the Application.

you can override the back button function
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
//Do your work here
base.OnBackKeyPress(e);
}

Related

Helix Toolkit How to use MouseWheelEventHandler to run specific methods in other classes

I would like to run particular methods of a custom Camera class whenever the user zooms in or out in the helix toolkit view that my program is running inside of.
A key feature of this functionality is getting the mouseargs from the event so I can adjust the camera in a way that is proportional to the number of scroll ticks.
I began to try this:
public event PropertyChangedEventHandler PropertyChanged;
public virtual void onMouseWheeled(MouseDevice Mouse, int time,
MouseWheelEventArgs e) {
MouseWheel?.Invoke(this, new MouseWheelEventArgs(Mouse, time,
e.Delta)); }
//This next line goes in a MainWindow_Loaded method that gets called in the
//MainWindowConstructor
void MainWindow_Loaded(object sender, RoutedEventArgs e) {
view1.MouseWheel += new MouseWheelEventHandler(onMouseWheeled(Cursor,
Time.simTime, view1.MouseWheeledEventArgs)); }
but was having a lot of trouble figuring out how to pass a MouseWheelEventArgs object into the onMouseWheeled method when I'm trying to add the onMouseWheeled method to the MouseWheelEventHandler. Assuming nothing is fundamentally wrong with that sentence, which is nothing more than wishful thinking, The last thing I am trying to figure out is how to get mouse wheel event args so that I can pass it into a method.
Then I tried this:
public event MouseWheelEventHandler MouseWheel;
public virtual void onMouseWheeled(object sender, MouseWheelEventArgs e)
{
Console.WriteLine(e.Delta);
}
//In Main Window Loaded method...
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
view1.MouseWheel += onMouseWheeled;
}
But I get no output when i scroll the wheel. I assumed this might actually work because view1 is the helix window I'm attaching all of my objects to, as children of view1.
Basically my main questions are:
What does invoke actually do? I only have this running to try to see if its working because onPropertyChanged methods I always use run the Invoke command like so. I'm actually not sure where I'm going with this.
How does the handler work?
How do the event args get called out so that I can use them and pass them as objects to other methods?
Thank you for your time. And Thank you twice for any and all pointers and advice you may give me.
Try to use preview mouse wheel event

Manipulation events of MediaElement not fire when on FullWindows mode

When I set player not in fullscreen (player.IsFullWindows = false), event work normally but when change player to full screen all manipulation event not work. Anyone have solution?
<MediaElement Name="player"
Margin="10,5" ManipulationCompleted="player_ManipulationCompleted"
ManipulationDelta="Grid_ManipulationDelta"
ManipulationMode="TranslateX"
>
I can reproduce this scenario by enabling both the IsFullWindow="True" and the AreTransportControlsEnabled="True". I think it makes sense, because when we are in the Full Window mode, it will go to the new layer named FullWindowMediaRoot instead of the MediaElement. Inside the FullWindowMediaRoot, it is the MediaTransportControls. You can see that clearly by using the Live Visual Tree as following:
So when we are in the Full Window mode, we need to handle the manipulation event of the TransportControls instead of the manipulation event of the MediaElement as following:
public MainPage()
{
this.InitializeComponent();
player.TransportControls.ManipulationMode = ManipulationModes.TranslateX;
player.TransportControls.ManipulationDelta += TransportControls_ManipulationDelta;
player.TransportControls.ManipulationCompleted += TransportControls_ManipulationCompleted;
}
private void TransportControls_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
}
private void TransportControls_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
}
Thanks.

Invoking Message dialog from Settings Flyout causes Message Dialog to flicker

I'm trying to invoke messagedialog from setting flyout for my Windows 8 Metro app but it's causing the message dialog to flicker. Below is the code.
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
SettingsPane.GetForCurrentView().CommandsRequested+=settings_CommandsRequested;
}
private void Settings_CommandsRequested(SettingsPane sender, SetttingsPaneCommandsRequestedEventArgs args)
{
SettingsCommand cmd = new SettingsCommand("test","test1232",new UICommandInvokedHandler(CreateDialog));
args.Request.ApplicationCommands.Add(cmd);
}
private void CreateDialog(IUICommand command)
{
if (ReferenceEquals(command.Id, "cmd"))
{
MessageDialog md = new MessageDialog("Hi");
md.ShowAsync();
}
}
I've contacted official microsoft dev-support and their response was:
"MessageDialog is not recommended within the SettingsFlyout".
So in case you want to implement something simillar to support user's decision from the SettingsPane, you should either:
1) Enable toggling feature in the Flyout.
2) Desiging the SettingsFlyout so it lets the user make decision (for example in Logout cases, add Yes/no buttons inside the settingsFlyout) - Thats the option I chose.

How to navigate from one page to another in Windows phone 8 using XAML?

I am trying to make my first WP8 app but, i got a problem. I try to navigate to another page using the code below, but VS12 throws a error.
What am I doing wrong?
private void btnBMIBereken_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/BMI_Bereken.xaml", UriKind.Relative));
}
Your code is correct for navigating, just make sure the Page 'BMI_Bereken.xaml' actual exists at the root of your project.
Clean solution first and then rebuild again (right click on project/solution -> clean)
Then if it still crashes, try to use System.Windows.RoutedEventArgs e instead of RoutedEventArgs e
Guys i found the problem.
I use a self made button style for my buttons and i got on the visualstate pressed some wrong code. In fact I was trying to set the background collor of the button in the place of the name target name.
See the code below for what i did wrong.
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="#FF009AD3">
I know very stupid, but I want to thank everyone for the help.
Try this
private void btnBMIBereken_Click(object sender, RoutedEventArgs e)
{
Dispatcher.BeginInvoke(() =>
{
this.NavigationService.Navigate(new Uri("/BMI_Bereken.xaml", UriKind.Relative));
});
}
You could do something like this:
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
if (txtDriverId.Text == "D0001" && txtPassword.Password == "open")
{
Frame.Navigate(typeof(VehicleCondition));
}
}

Wakelock only working with usb-cable connected

I have a rather odd problem. Lately i tried to use a wakelock in my application using the following code:
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
screenLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNotDimScreen");
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
System.out.println("Clicked");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (!locked)
setKeepScreenOn(WakeupLightActivity.this, true);
else setKeepScreenOn(WakeupLightActivity.this, false);
System.out.println("Screen will stay on");
}
});
}
public void setKeepScreenOn(Activity activity, boolean keepScreenOn) {
if(keepScreenOn) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
this.screenLock.acquire();
}
else {
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
this.screenLock.release();
}
}
The wakelock works perfectly: 5 seconds after pressing the button (and locking my phone) the screen turns on. This test, however, was done while my phone was connected to my laptop for debugging using a USB cable.
Now comes the weird part. When I DON'T have my phone connected with the USB cable, the wakelock simply DOES NOT turn on the screen. The device I am using is an HTC desire. I have actually tested it on another HTC desire, which did the same thing. Next, i tried it on an HTC Desire HD. And guess what: It worked perfectly again! I am really confused about this and wondering if the problem is simply a flaw in my phone type, or if I am doing something wrong in my code. Is there anything I can do about it? It would be incredibly awkward if any potential customers with a malfunctioning phone type would encounter the same problem after having purchased my app.
Note that I also tried the application while charging my phone with the cable connected to a wall plug. This leads to the same problem as not having my phone connected at all. Both HTC Desires run the same Android version (2.2.2).
Apparently all of this occured simply because i was using the wrong flags. The problem was solved by creating the WakeLock with the following flags:
PowerManager.SCREEN_BRIGHT_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP