locked
C# Store app. OnGotFocus glitches, need something else to check if a value is changed. And bug report about a method.

    Question

  • Hi all,

    Sorry for the long post in advance (just want to give detailed information)

    I'm making a C# Store app where I use a flyout page to set values in the localsettings. I do it like this:

    private ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;

    bool blocked = Convert.ToBoolean(localSettings.Values["BlockedSetting"]); private void sliderBlocked_Toggled(object sender, RoutedEventArgs e) { localSettings.Values["BlockedSetting"] = sliderBlocked.IsOn; }

    In another page I ask for that variable to check if its true or false. If its false I won't show data, else i do show data. It works fine when i navigate to the page where i want to use this. But when I use the flyout on the page where i want to use that variable, it does not work because the page is not reloaded. I thought I could fix this by using the OnGotFocus method provided by Microsoft. 

    protected override void OnGotFocus(RoutedEventArgs e) { base.OnGotFocus(e);

    if(Convert.ToBoolean(localSettings.Values["BlockedSetting"]) == true{ //work done here.

    } }

    Now it seems that this method is bugged! You can enter the flyout on 2 different ways: 1. press te backbutton on the flyout-page first. then tab the screen. 2.  instantly tab the screen which will force the flyout to close. when i use the first route, the OnGotFocus method is ALWAYS executed. when I use the second route the OnGotFocus method is not always triggered, its triggered in about 60% of the time when deployed on a tablet. So thats the bug report. 

    Now the actual question: how can i force the page to reload so that it will execute everything that written in the LoadState() ? As stated before the OnGotFocus method isn't reliable enough. and I need something to automatically reload the page after the flyout closes. 

    Thanks in advance for your time and I hope you can help me :)

    Rudi Jansen.


    • Edited by Proliges Monday, May 19, 2014 10:57 AM
    Monday, May 19, 2014 9:42 AM

Answers

  • You should be able to call Frame.Navigate back to the same page, but I don't see how this will help.

    As I understand it you need to update the data not the page as a whole. Data binding is designed for this sort of situation.

    Friday, May 23, 2014 2:46 AM
    Owner

All replies

  • I think you're misunderstanding focus. Focus is about individual controls getting keyboard input. It isn't about the fly out being visible. The focus behaviour you describe sounds reasonable since different command paths can legitimately set the focus to different locations. The 60% is based on where the focus was before starting the sequence. Hitting the back button will set the focus to the back button so it's in a known and consistent state.

    I'm not sure what behaviour you do want though. Likely OnNavigatedTo or window activation.

    A better solution may be to use data binding and let the system handle it. The fly out can set properties in a data object and the page can bind to those properties so they'll automatically update.

    --Rob

    Monday, May 19, 2014 1:21 PM
    Owner
  • No the OnGotFocus method is not about keyboard input having focus. It is about pages having focus.
    The OnNavigateTo method won't work either, since you're not navigating to a page when you close a flyout. You're already on that page, so that won't trigger. 

    The app has sliders that can be turned off and on. This sliders are written to the localSettings. The localsettings are currently read in the OnGotFocus method and when i close the flyout the OnGotFocus method is triggered, and the app will show or don't show specific data. Its important that it happens when you navigate away from the flyout since the settings are already set then. 

    i need something like : page.Refresh or something, that refreshes the page.
    Monday, May 19, 2014 2:02 PM
  • Focus is all about keyboard input. The control with focus is the one that will get keyboard input. Focus is not a page based system, although the page may get focus if it is the primary keyboard target and focus events on child controls can bubble up. If no control has the focus or if the focus doesn't change then there will be no GotFocus event.

    There is no refresh method, but you can force a navigation. You still have the problem of knowing when to trigger it.

    From what you describe data binding would be perfect. Otherwise I'd wire the page to the Flyout's Closed event. Disconnect the event handler after it fires so the Flyout can be garbage collected.

     
    Monday, May 19, 2014 2:23 PM
    Owner
  • Ok, you I assume you know what you're talking about so I mark that words as true. In that case i used a wrong method to try and solve my problem. 

    Can you explain how I can force a navigation? I cannot use this.Frame.Navigate(typeof(MainPage)); in the flyouts since it will not accept it, so i can't force a navigation in the flyout. 

    Since I need it to "refresh" after the flyout closes, how can I force a navigation from the flyout closed event? The problem of knowing how to trigger it is not a problem, I can think of some ways to do that :)

    Tuesday, May 20, 2014 6:58 AM
  • You should be able to call Frame.Navigate back to the same page, but I don't see how this will help.

    As I understand it you need to update the data not the page as a whole. Data binding is designed for this sort of situation.

    Friday, May 23, 2014 2:46 AM
    Owner