When user press Back key on non-first pivot item, it jumps to first one.
When user press Back key on first pivot item, it closes app.
Problem starts, when i'm showing Window or JumpList HeaderSelection. When user is pressing back, they are disappearing, and pivot jumps to first pivot item.
Is it possible to handle Back inside of the Window in case it is opened?
11 Answers, 1 is accepted
You can override OnBackKeyPress in the page where the Window is.
protected
override
void
OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
base
.OnBackKeyPress(e);
// Prevent from going back
e.Cancel =
true
;
}
Kiril Stanoev
Telerik
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
protected
override
void
OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
base
.OnBackKeyPress(e);
if
(MyWindow.IsOpened)
{
MyWindow.IsOpened=
false
;
e.Cancel =
true
;
}
if
(MyWindow2.IsOpened)
{
MyWindow2.IsOpened=
false
;
e.Cancel =
true
;
}
}
But, i guess, it is per-component responsibility, so it would be nice to have property HideOnBackButton.
As mentioned before, i have a pivot with UserControls as items.
In this case, i need to check UserControl's Window in the main pivot, i totally dont like this.
Now i'm considering to send OnBackkeyPress event to UserControl, but its looking like a bad design.
Thank you for the suggestion.
Another option to research is using commands in your app.
Kiril Stanoev
Telerik
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
I'm not sure, if commands can be helpful here, because i need to get a state of windows.
Kinda
protected
override
void
OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
base
.OnBackKeyPress(e);
var pivotItem = Pivot.Items[Pivot.SelectedIndex]
as
PivotItem;
// pseudocode
if
(pivotItem ==
null
)
return
;
var uc = (pivotItem.Content
as
BaseUserControl);
// get user control
if
(uc ==
null
) retuen;
var vm = (uc.DataContext
as
MyCartVM);
// get user controls vm
if
(vm ==
null
)
return
;
if
(vm.IsWindowOpened)
{
vm.CloseWindow(); // So, you suggest using command here?
e.Cancel =
true
;
}
I guess, Window's HideOnBackButton property can save lots of work :)
What if you inherit from RadWindow and provide the back key press implementation in it. Something like this:
<
local:MyRadWindow
x:Name
=
"myWindow"
IsFullScreen
=
"True"
>
<
ListBox
x:Name
=
"countriesListBox"
Background
=
"Black"
Padding
=
"20"
>
<
ListBoxItem
Content
=
"United Kingdom"
IsSelected
=
"True"
/>
<
ListBoxItem
Content
=
"Germany"
/>
<
ListBoxItem
Content
=
"Russia"
/>
<
ListBoxItem
Content
=
"Japan"
/>
<
ListBoxItem
Content
=
"Bulgaria"
/>
</
ListBox
>
</
local:MyRadWindow
>
public
class
MyRadWindow : RadWindow
{
public
MyRadWindow()
{
this
.Loaded -= MyRadWindow_Loaded;
this
.Loaded += MyRadWindow_Loaded;
}
void
MyRadWindow_Loaded(
object
sender, RoutedEventArgs e)
{
var parentPage = ElementTreeHelper.FindVisualAncestor<PhoneApplicationPage>(
this
);
if
(parentPage !=
null
)
{
parentPage.BackKeyPress -=
this
.ParentPage_BackKeyPress;
parentPage.BackKeyPress +=
this
.ParentPage_BackKeyPress;
}
}
private
void
ParentPage_BackKeyPress(
object
sender, System.ComponentModel.CancelEventArgs e)
{
this
.IsOpen =
false
;
}
}
Do you think this will be helpful in your scenario? Regards,
Kiril Stanoev
Telerik
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Trying to implement something like
http://feedback.telerik.com/Project/112/Feedback/Details/56940-telerik-appbar-submenu
private
void
ParentPage_BackKeyPress(
object
sender, System.ComponentModel.CancelEventArgs e)
{
this
.IsOpen =
false
;
}
Also, Window is not rendering on top of appbar (i'm using Cimbalino).
It would be fair enough: if control is visible, it is hiding on BackButton, and not sending it further.
As for now, i see only one way: catch OnBackKeyPress() in the main pivot and ask all viewmodels about their childs status.
if
(vm1.IsPromptOpened)
{
vm1.ClosePrompt();
e.Cancel =
true
;
}
if
(vm1.IsWindowOpened)
{
vm1.CloseWindow();
e.Cancel =
true
;
}
if
(vm1.IsJumpListPickerOpened)
{
vm1.CloseJumpListPicker();
e.Cancel =
true
;
}
if
(vm2.IsPromptOpened)
{
vm2.ClosePrompt();
e.Cancel =
true
;
}
Indeed there are many conditions to check for in your app and it might seem cumbersome but I don't see anything wrong with that approach. We'll consider with the team your proposition the controls to be aware of the back button press.
Regards,Kiril Stanoev
Telerik
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
It works with windows (i can just ask PivotItem.Window.IsOpen), but it works not that good for InputPrompt, MessageBox (especially without async) and JumpList.GroupPicker.
When they are visible, i'm setting variable to true, and in callback, i'm setting variable to false, but i really don't like this way.
Edit: btw, how can i get, if a GroupPicker is visible? I see GroupPickerItemTap event, it rises when picker is closing. But when it appears? There should be something like GroupHeaderTap?
I cannot bind to a specific release date. First, the idea needs to gather some priority before we consider including it in our future releases. I've added an item in our feedback portal:
http://feedback.telerik.com/Project/112/Feedback/Details/63700-property-to-hide-the-window-when-back-key-is-pressed
I'd suggest you vote for the item in order to increase its priority.
As for your other question, you can use the GroupHeaderItemTap event to determine when the picker opens.
Kiril Stanoev
Telerik
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>