Force skipping back button

12 posts, 1 answers
  1. Vitalii
    Vitalii avatar
    104 posts
    Member since:
    Jul 2013

    Posted 12 Oct 2013 Link to this post

    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?
  2. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1512 posts

    Posted 14 Oct 2013 Link to this post

    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 >>
  3. DevCraft banner
  4. Vitalii
    Vitalii avatar
    104 posts
    Member since:
    Jul 2013

    Posted 14 Oct 2013 Link to this post

    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.
  5. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1512 posts

    Posted 14 Oct 2013 Link to this post

    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 >>
  6. Vitalii
    Vitalii avatar
    104 posts
    Member since:
    Jul 2013

    Posted 14 Oct 2013 Link to this post

    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 :)
  7. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1512 posts

    Posted 14 Oct 2013 Link to this post

    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 >>
  8. Vitalii
    Vitalii avatar
    104 posts
    Member since:
    Jul 2013

    Posted 14 Oct 2013 Link to this post

  9. Vitalii
    Vitalii avatar
    104 posts
    Member since:
    Jul 2013

    Posted 14 Oct 2013 Link to this post

    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).
  10. Vitalii
    Vitalii avatar
    104 posts
    Member since:
    Jul 2013

    Posted 14 Oct 2013 Link to this post

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




  11. Answer
    Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1512 posts

    Posted 15 Oct 2013 Link to this post

    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 >>
  12. Vitalii
    Vitalii avatar
    104 posts
    Member since:
    Jul 2013

    Posted 17 Oct 2013 Link to this post

    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?
  13. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1512 posts

    Posted 22 Oct 2013 Link to this post

    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 >>
Back to Top
DevCraft banner