This is a migrated thread and some comments may be shown as answers.

Force skipping back button

11 Answers 89 Views
Window
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Vitalii
Top achievements
Rank 2
Vitalii asked on 12 Oct 2013, 01:31 PM
I have a pivot with several pivot items.
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

Sort by
0
Kiril Stanoev
Telerik team
answered on 14 Oct 2013, 07:52 AM
Hi Vitalii,

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;
}

Regards,
Kiril Stanoev
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINDOWS PHONE 7.
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 >>
0
Vitalii
Top achievements
Rank 2
answered on 14 Oct 2013, 10:02 AM
Well, definitely, i can make something like

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.
0
Kiril Stanoev
Telerik team
answered on 14 Oct 2013, 11:36 AM
Hi Vitalii,

Thank you for the suggestion. 

Another option to research is using commands in your app.

Regards,
Kiril Stanoev
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINDOWS PHONE 7.
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 >>
0
Vitalii
Top achievements
Rank 2
answered on 14 Oct 2013, 11:53 AM
Well, i'm using commands in a straightforward way, for sending events (like button clicks) to VM.
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;
  }
A bit messy, isnt it?
I guess, Window's HideOnBackButton property can save lots of work :)
0
Kiril Stanoev
Telerik team
answered on 14 Oct 2013, 12:23 PM
Hello Vitalii,

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
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINDOWS PHONE 7.
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 >>
0
Vitalii
Top achievements
Rank 2
answered on 14 Oct 2013, 12:53 PM
0
Vitalii
Top achievements
Rank 2
answered on 14 Oct 2013, 01:09 PM
private void ParentPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
    {
        this.IsOpen = false;
    }
Hm, but how can i block BackKey from reaching MainPivot? It is just subscriber.

Also, Window is not rendering on top of appbar (i'm using Cimbalino).
0
Vitalii
Top achievements
Rank 2
answered on 14 Oct 2013, 10:51 PM
The problem is that Pivot is core of my app. I need BackButton cancelling for everything: for InputPrompt, for this Window, even for JumpList group picker.
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;
}




0
Accepted
Kiril Stanoev
Telerik team
answered on 15 Oct 2013, 07:15 AM
Hi Vitalii,

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
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINDOWS PHONE 7.
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 >>
0
Vitalii
Top achievements
Rank 2
answered on 17 Oct 2013, 04:22 PM
Do you have some estimations about it?

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?
0
Kiril Stanoev
Telerik team
answered on 22 Oct 2013, 01:14 PM
Hello Vitalii,

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.

Regards,
Kiril Stanoev
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINDOWS PHONE 7.
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 >>
Tags
Window
Asked by
Vitalii
Top achievements
Rank 2
Answers by
Kiril Stanoev
Telerik team
Vitalii
Top achievements
Rank 2
Share this question
or