Pages

Thursday, August 9, 2012

CharmFlyout – Supporting sub-flyouts

If you are supporting sub-flyouts (like the Accounts in the Mail application), then this post is for you.

Posts in this series:

flyout

In this example, we will create two flyouts, “My Settings” and “My Sub-Settings”.  Our desired behavior is to allow a settings flyout to open another (sub) settings flyout – see green arrows above.  When the sub flyout is opened, the original (parent) is closed.  Then, when the user presses the back arrow on the sub-flyout, the parent flyout is re-opened – see blue arrows.  However, if the user light-dismisses the sub-flyout (by clicking elsewhere on the application screen), then the parent is not reopened, but the application is displayed without any flyouts – see yellow arrow.

MyCharmFlyouts.xaml
<cfo:CharmFlyout
   x:Name="cfoSettings"
   Heading="My Settings"
   HeadingBackgroundBrush="#FF4E0000">
    <StackPanel>
        <TextBlock
           FontSize="16">Setting A</TextBlock>
        <TextBlock
           FontSize="16">Setting B</TextBlock>
        <TextBlock
           FontSize="16">Setting C</TextBlock>
        <Grid
           Background="#FF4E0000" HorizontalAlignment="Left">
            <Button
               Click="OnViewSubheading"
               Content="View Sub-Settings" />
        </Grid>
    </StackPanel>
</cfo:CharmFlyout>
<cfo:CharmFlyout
   x:Name="cfoSubSettings"
   Heading="My Sub-Settings"
   HeadingBackgroundBrush="#FF4E0000"
   ParentFlyout="{Binding ElementName=cfoSettings}">
    <StackPanel>
        <TextBlock
           FontSize="16">Sub-setting 1</TextBlock>
        <TextBlock
           FontSize="16">Sub-setting 2</TextBlock>
    </StackPanel>
</cfo:CharmFlyout>

The point to notice in the second (sub-flyout) is the property called ParentFlyout.  In this case, the parent of cfoSubSettings is cfoSettings.

ParentFlyout="{Binding ElementName=cfoSettings}"

Here is the code-behind for these two flyouts.

MyCharmFlyouts.xaml.cs
public sealed partial class MyCharmFlyouts : UserControl
{
    public MyCharmFlyouts()
    {
        this.InitializeComponent();
        SettingsPane.GetForCurrentView().CommandsRequested += CommandsRequested;
    }

    private void CommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)
    {
        args.Request.ApplicationCommands.Add(new SettingsCommand("s", "My Settings", (p) => { cfoSettings.IsOpen = true; }));
    }

    private void OnViewSubheading(object sender, RoutedEventArgs e)
    {
        cfoSubSettings.IsOpen = true;
    }
}

That’s it!

CharmFlyout binaries reside here: http://nuget.org/packages/charmflyout/

CharmFlyout source resides here: http://charmflyout.codeplex.com/

15 comments:

  1. Great work.

    The new SubFlyout implementation feels more intuitive and powerful. Thanks again for looking into the issues I had with the previous iteration.

    Regards
    Andreas R.

    ReplyDelete
  2. Hi

    Say you have three CharmFlyout cfoAbout, cfoSettings and cfoSubSettings
    Can you have each CharmFlyout User Control in different XMAL file?

    If yes, how do you initialize different XMAL files in CharmFrame CharmContent?

    Also could you please do example code for MVVM version if you have spare time?

    Regards,

    ReplyDelete
    Replies
    1. David,

      CharmFrame only supports one instance of CharmContent. I could change CharmFrame to support more than one instance, but that seems a bit silly to me.

      Instead, you could place each of your CharmFlyouts into separate XAML files and then include them all in a Grid within the main MyCharmFlyouts. In this way, CharmFrame still has one CharmContent which is a UserControl, which contains a Grid, which includes CharmFlyouts defined in separate XAML files.

      It might be best to keep the sub-flyouts together so it is easier to connect them via the ParentFlyout property.

      My spare time is a bit sparse these days, so I think I will pass on an MVVM version. I will be posting the full source of a neat Windows 8 application that includes ViewModels and Flyouts in the next month or so. Maybe that will help.

      Delete
  3. Great stuff, really appreciate your sharing this. One apparent typo in your article is indicating that new Frame() is in MainPage.xaml.cs. I think you meant to indicate it is in App.xaml.cs

    ReplyDelete
    Replies
    1. Chris,
      Thank you for pointing this out!
      I fixed this in the article "CharmFrame – Adding CharmFlyout to Grid Apps".

      Delete
  4. Awesome! This most definitely fills a hole in the current WinRT space.

    One quick question/observation: the left edge of my flyout is slightly jaggy. It looks like the left edge is skewed one or two pixels from top to bottom, with the top left corner being offset slightly to the right relative to the bottom left corner. Any thoughts on what might be causing this?

    ReplyDelete
    Replies
    1. Mark,

      I'll just confirm what many people think: I am clueless.

      Seriously, do you see this with the demo apps from http://charmflyout.codeplex.com/?

      Would you be so kind as to provide me with source and/or screenshots so I can get a better understanding of what slightly jaggy means?

      Delete
  5. I want to hide setting pane when i press back button in charm flyout....

    or

    Is their any way by which i can disable back button in the charm flyout....

    If any one is possible please suggest me how to do it....

    ReplyDelete
    Replies
    1. Rohit,

      The behavior you are asking for
      (not showing the settings pane when you press the back button)
      is non-standard.

      As a result, I will be slow to add this feature to the library, but I will try in the next few weeks.

      In the mean time, you can do this if you download the source code from charmflyout.codeplex.com and delete the indicated line.

      private void OnBack(object obj)
      {
      IsOpen = false;

      if (ParentFlyout != null)
      {
      ParentFlyout.IsOpen = true;
      }
      else
      {
      SettingsPane.Show(); <- delete this line
      }
      }

      - John

      Delete
  6. You rock, John! Additional thanks for awesome sub-flyouts support.

    ReplyDelete
  7. Hi,

    For some reason when I click the Settings link, the sidebar simply vanishes.

    I can breakpoint on this and it fires:

    cfoSettings.IsOpen = true;

    Any ideas?

    Thanks!

    ReplyDelete
    Replies
    1. Alright, my mistake... not assigning the App.cs as a CharmFrame.

      Thanks for the control!

      Delete
    2. Hi... em having the same problem .can u please elaborate? what did u do ?

      Delete

  8. Hi, i have a question.i want a click event for the items in settingpane

    like when i click on any of the listed items in after going into Setting charm i.e About Us , permission

    as for now there is no Event with which i can work ... tried PointerPressed,Tapped etc but didnt work...please help em stuck here...

    ReplyDelete

Note: Only a member of this blog may post a comment.