Skip to content

Commit

Permalink
allow to handle HadwareButtons.BackPressed in WPA81
Browse files Browse the repository at this point in the history
  • Loading branch information
tibel committed Jun 5, 2014
1 parent 55423f3 commit 1a71304
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 48 deletions.
2 changes: 1 addition & 1 deletion nuget/Caliburn.Light.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<metadata>
<id>Caliburn.Light</id>
<title>Caliburn.Light</title>
<version>1.2.2</version>
<version>1.2.3</version>
<authors>Thomas Ibel</authors>
<description>The magic-free Caliburn.Micro, a powerful framework designed for building applications across all XAML platforms.</description>
<language>en-US</language>
Expand Down
67 changes: 37 additions & 30 deletions src/Caliburn.Xaml.Universal/Services/FrameAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
#if WINDOWS_PHONE_APP
using Windows.Phone.UI.Input;
#endif

namespace Caliburn.Light
{
Expand All @@ -19,7 +22,6 @@ public class FrameAdapter : INavigationService
private const string ParameterKey = "ParameterKey";

private readonly Frame _frame;
private event NavigatingCancelEventHandler ExternalNavigatingHandler = delegate { };
private object _currentParameter;

/// <summary>
Expand All @@ -34,8 +36,8 @@ public FrameAdapter(Frame frame)
_frame.Navigated += OnNavigated;

#if WINDOWS_PHONE_APP
_frame.Loaded += (sender, args) => { Windows.Phone.UI.Input.HardwareButtons.BackPressed += OnHardwareBackPressed; };
_frame.Unloaded += (sender, args) => { Windows.Phone.UI.Input.HardwareButtons.BackPressed -= OnHardwareBackPressed; };
_frame.Loaded += (sender, args) => { HardwareButtons.BackPressed += OnHardwareBackPressed; };
_frame.Unloaded += (sender, args) => { HardwareButtons.BackPressed -= OnHardwareBackPressed; };
#endif
}

Expand All @@ -46,7 +48,9 @@ public FrameAdapter(Frame frame)
/// <param name="e"> The event args. </param>
protected virtual void OnNavigating(object sender, NavigatingCancelEventArgs e)
{
ExternalNavigatingHandler(sender, e);
var handler = Navigating;
if (handler != null)
handler(sender, e);

if (e.Cancel)
return;
Expand Down Expand Up @@ -85,7 +89,6 @@ protected virtual void OnNavigating(object sender, NavigatingCancelEventArgs e)
/// <param name="e"> The event args. </param>
protected virtual void OnNavigated(object sender, NavigationEventArgs e)
{

if (e.Content == null)
return;

Expand Down Expand Up @@ -139,32 +142,25 @@ protected virtual void TryInjectParameters(object viewModel, object parameter)
{
var uri = new Uri((string) parameter);

if (!String.IsNullOrEmpty(uri.Query))
if (!string.IsNullOrEmpty(uri.Query))
{
var decorder = new WwwFormUrlDecoder(uri.Query);

foreach (var pair in decorder)
{
var property = viewModelType.GetRuntimeProperty(pair.Name);

if (property == null)
{
continue;
}
if (property == null) continue;

property.SetValue(viewModel,
ParameterBinder.CoerceValue(property.PropertyType, pair.Value, null));
ParameterBinder.CoerceValue(property.PropertyType, pair.Value));
}
}
}
else
{
var property = viewModelType.GetRuntimeProperty("Parameter");

if (property == null)
return;

property.SetValue(viewModel, ParameterBinder.CoerceValue(property.PropertyType, parameter, null));
if (property == null) return;
property.SetValue(viewModel, ParameterBinder.CoerceValue(property.PropertyType, parameter));
}
}

Expand All @@ -190,11 +186,7 @@ public event NavigatedEventHandler Navigated
/// <summary>
/// Raised prior to navigation.
/// </summary>
public event NavigatingCancelEventHandler Navigating
{
add { ExternalNavigatingHandler += value; }
remove { ExternalNavigatingHandler -= value; }
}
public event NavigatingCancelEventHandler Navigating;

/// <summary>
/// Raised when navigation fails.
Expand Down Expand Up @@ -332,30 +324,45 @@ public bool ResumeState()
? container.Values[ParameterKey]
: null;

if (String.IsNullOrEmpty(frameState))
if (string.IsNullOrEmpty(frameState))
return false;

_frame.SetNavigationState(frameState);

var view = _frame.Content as Page;
if (view == null)
{
return false;
}
if (view == null) return false;

BindViewModel(view);

if (!ReferenceEquals(Window.Current.Content, _frame))
Window.Current.Content = _frame;

Window.Current.Activate();

return true;
}

#if WINDOWS_PHONE_APP
private void OnHardwareBackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
/// <summary>
/// Occurs when the user presses the hardware Back button.
/// </summary>
public event EventHandler<BackPressedEventArgs> BackPressed;

/// <summary>
/// Occurs when the user presses the hardware Back button. Allows the handlers to cancel the default behavior.
/// </summary>
/// <param name="e">The event arguments</param>
protected virtual void OnBackPressed(BackPressedEventArgs e)
{
var handler = BackPressed;
if (handler != null)
handler(this, e);
}

private void OnHardwareBackPressed(object sender, BackPressedEventArgs e)
{
OnBackPressed(e);
if (e.Handled) return;

if (CanGoBack)
{
e.Handled = true;
Expand All @@ -370,4 +377,4 @@ private static ApplicationDataContainer GetSettingsContainer()
ApplicationDataCreateDisposition.Always);
}
}
}
}
10 changes: 10 additions & 0 deletions src/Caliburn.Xaml.Universal/Services/INavigationService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using Windows.UI.Xaml.Navigation;
#if WINDOWS_PHONE_APP
using Windows.Phone.UI.Input;
#endif

namespace Caliburn.Light
{
Expand Down Expand Up @@ -77,6 +80,13 @@ public interface INavigationService
/// </summary>
IList<PageStackEntry> ForwardStack { get; }

#if WINDOWS_PHONE_APP
/// <summary>
/// Occurs when the user presses the hardware Back button.
/// </summary>
event EventHandler<BackPressedEventArgs> BackPressed;
#endif

/// <summary>
/// Stores the frame navigation state in local settings if it can.
/// </summary>
Expand Down
20 changes: 4 additions & 16 deletions src/Caliburn.Xaml.WP8/Services/FrameAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace Caliburn.Light
public class FrameAdapter : INavigationService
{
private readonly Frame _frame;
private NavigatingCancelEventHandler _externalNavigatingHandler;

/// <summary>
/// Creates an instance of <see cref="FrameAdapter" />
Expand All @@ -33,7 +32,7 @@ public FrameAdapter(Frame frame)
/// <param name="e"> The event args. </param>
protected virtual void OnNavigating(object sender, NavigatingCancelEventArgs e)
{
var handler = _externalNavigatingHandler;
var handler = Navigating;
if (handler != null)
handler(sender, e);

Expand Down Expand Up @@ -82,18 +81,11 @@ protected virtual bool CanCloseOnNavigating(object sender, NavigatingCancelEvent
/// <param name="e"> The event args. </param>
protected virtual void OnNavigated(object sender, NavigationEventArgs e)
{
if (e.Uri.IsAbsoluteUri || e.Content == null)
{
return;
}
if (e.Uri.IsAbsoluteUri || e.Content == null) return;

ViewLocator.InitializeComponent(e.Content);

var viewModel = ViewModelLocator.LocateForView(e.Content);
if (viewModel == null)
{
return;
}
if (viewModel == null) return;

var page = e.Content as PhoneApplicationPage;
if (page == null)
Expand Down Expand Up @@ -241,11 +233,7 @@ public event NavigatedEventHandler Navigated
/// <summary>
/// Raised prior to navigation.
/// </summary>
public event NavigatingCancelEventHandler Navigating
{
add { _externalNavigatingHandler += value; }
remove { _externalNavigatingHandler -= value; }
}
public event NavigatingCancelEventHandler Navigating;

/// <summary>
/// Raised when navigation fails.
Expand Down
2 changes: 1 addition & 1 deletion src/GlobalAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
#endif

[assembly: AssemblyVersion("1.2.0.0")]
[assembly: AssemblyFileVersion("1.2.2.0")]
[assembly: AssemblyFileVersion("1.2.3.0")]

0 comments on commit 1a71304

Please sign in to comment.