locked
Click event on backbutton of charm bar flyout RRS feed

  • Question

  • Hi

    I am using CharmFlyoutLibrary and I want to save data on back button of CharmBar so How can I write the event on it.

    Regards

    Rishi

    Saturday, October 20, 2012 1:29 PM

Answers

  • CharmFlyout has a new property called IsOpenChanged. This will allow you to be notified when the flyout opens or closes (whether by hitting the back button or light-dismiss). The sample code at codeplex wires up the event handler for this and displays some text showing if the flyout is open or closed.  Here is a snippet:

    public MainPage()
    {
    	this.InitializeComponent();
    	SettingsPane.GetForCurrentView().CommandsRequested += CommandsRequested;
    	cfoSettings.IsOpenChanged += CfoSettingsIsOpenChanged;
    }
    
    void CfoSettingsIsOpenChanged(object sender, EventArgs e)
    {
    	isOpenTextBlock.Text = "IsOpen is now " + cfoSettings.IsOpen;
    }

    By the way, CharmFlyout now sports a new property called ContentPadding. By default, ContentPadding is "40, 26, 30, 30".  For no padding, set ContentPadding="0".

    Binary: https://nuget.org/packages/CharmFlyout

    Source: http://charmflyout.codeplex.com

    Discussion: http://w8isms.blogspot.com/2012/07/charmflyout-another-charming.html

    - John


    John Michael Hauck

    • Marked as answer by RishiRajIT Thursday, October 25, 2012 6:24 AM
    Wednesday, October 24, 2012 7:09 PM

All replies

  • Why you don't use something like autosave or a Button for sabe data? With mvvm and binding is very Simple.

    See the code Source from Library to expose the event for button Click event.


    Sara Silva

    Saturday, October 20, 2012 1:56 PM
  • CharmFlyout has a new property called IsOpenChanged. This will allow you to be notified when the flyout opens or closes (whether by hitting the back button or light-dismiss). The sample code at codeplex wires up the event handler for this and displays some text showing if the flyout is open or closed.  Here is a snippet:

    public MainPage()
    {
    	this.InitializeComponent();
    	SettingsPane.GetForCurrentView().CommandsRequested += CommandsRequested;
    	cfoSettings.IsOpenChanged += CfoSettingsIsOpenChanged;
    }
    
    void CfoSettingsIsOpenChanged(object sender, EventArgs e)
    {
    	isOpenTextBlock.Text = "IsOpen is now " + cfoSettings.IsOpen;
    }

    By the way, CharmFlyout now sports a new property called ContentPadding. By default, ContentPadding is "40, 26, 30, 30".  For no padding, set ContentPadding="0".

    Binary: https://nuget.org/packages/CharmFlyout

    Source: http://charmflyout.codeplex.com

    Discussion: http://w8isms.blogspot.com/2012/07/charmflyout-another-charming.html

    - John


    John Michael Hauck

    • Marked as answer by RishiRajIT Thursday, October 25, 2012 6:24 AM
    Wednesday, October 24, 2012 7:09 PM
  • Thanks John

    Your project is great, I like it.

    Thursday, October 25, 2012 6:24 AM
  • Hi John

    As mentioned above IsOpenChanged  is used to for both the purpose. Can we differentiate between Opening and closing events of Flyout?
    I want to save the data only on closing event of flyout. What is appropriate way to do this task?

    Regards

    Rishi

    Friday, October 26, 2012 4:52 AM
  • Keep a Boolean variable in your app and set it alternatively during each call.
    Friday, October 26, 2012 7:45 AM
  • you don´t need to do what Usman says, because you have one property that says if the flyout IsOpen.

    If you see the code:

    http://charmflyout.codeplex.com/SourceControl/changeset/view/19182#212834

    public sealed class CharmFlyout : ContentControl
    {
    
    ....
    
      public bool IsOpen
            {
                get { return (bool)GetValue(IsOpenProperty); }
                set { SetValue(IsOpenProperty, value); }
            }
    
    }

    and in

     private void OnIsOpenChanged()
            {
                var isOpenChanged = IsOpenChanged;
                if (isOpenChanged != null)
                {
                    isOpenChanged(this, new EventArgs());
                }
    
                if (ParentFlyout != null && IsOpen)
                {
                    ParentFlyout.IsOpen = false;
                }
            }

    the object that is send in IsOpenChanged event ís a CharmFlyout, you only need to cast the object to access that property :)

    Sara Silva
    My Windows 8 Store Apps Samples
    Follow me in Twitter @saramgsilva

    My Windows 8 Store Apps: Female Pill | Galinho (Tic tac Toe) | 24
    (If my reply answers your question, please propose it as an answer because it will help other users)


    • Edited by saramgsilva Friday, October 26, 2012 8:28 AM
    • Proposed as answer by saramgsilva Friday, October 26, 2012 9:06 AM
    Friday, October 26, 2012 8:27 AM
  • @Sara as you said I did the same,

    I open child flyout in one method using IsOpen property. At that time the IsOpenChanged event get called and the method related to that is executed. Then after complete execution of that method the calling method resumes. Then the complete execution of the calling method again fire IsOpenChanged event. In this way IsOpenChanged event get called twice but I want to call it only one time. I'm not getting it why this is happening. So please help.

    I have observed similar behavior with radio button check event in the same flyout library(CharmFlyoutLibrary).

    I'm adding code for the same.

    Calling method:

    public void btnCalendar_Click(object sender, RoutedEventArgs e)
            {
                
                cfoAddNewCal.IsOpen = true;
                cfoAddNewCal.Heading = ResourceManager.Current.MainResourceMap.GetValue("Resources/strEdit").ValueAsString;
            }

    Called Event:

    private void cfoAddNewCal_IsOpenChanged(object sender, EventArgs e)
            {
                CharmFlyout obj = (CharmFlyout)sender;
                if (obj.IsOpen)
                {
                    objCLogic = new CCalendarLogic();
                    objCLogic.InsertUpdateCalendar(0, gCalId, txtName.Text, iShowWeekNo, iFontSize, FirstDay, 1, 24);
                }
            }

    Regards

    Rishi

    Friday, October 26, 2012 10:42 AM
  • Rishi,

    Would this work for you?

    void CfoSettingsIsOpenChanged(object sender, EventArgs e)
    {
        if( cfoSettings.IsOpen == false )
        {
             // place your code to save data here... because the flyout just closed.
        }
    }

    I suggest you try replacing the code above in MainPage.xaml.cs in CharmDemoApp and see if that would work for you.

        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
                SettingsPane.GetForCurrentView().CommandsRequested += CommandsRequested;
                cfoSettings.IsOpenChanged += CfoSettingsIsOpenChanged;
            }
    
            private void CommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)
            {
                args.Request.ApplicationCommands.Add(new SettingsCommand("s", "My Flyout", (p) => { cfoSettings.IsOpen = true; }));
            }
    
            void CfoSettingsIsOpenChanged(object sender, EventArgs e)
            {
                isOpenTextBlock.Text = "IsOpen is now " + cfoSettings.IsOpen;
            }
    
            private async void OnMailTo(object sender, RoutedEventArgs args)
            {
                var hyperlinkButton = sender as HyperlinkButton;
                if (hyperlinkButton != null)
                {
                    var uri = new Uri("mailto:" + hyperlinkButton.Content);
                    await Windows.System.Launcher.LaunchUriAsync(uri);
                }
            }
        }

    I hope this helps.


    John Michael Hauck

    Friday, October 26, 2012 12:27 PM