Monday, April 16, 2012

Windows 8 Metro PopupMenu Issue

How to create a popup in Windows 8 Metro apps? MessageDialog and PopupMenu seem to be the only two options if you go with XAML/C# approach (at least for current Customer Preview edition). MessageDialog is just the Metro implementation of WinForm MessageBox. PopupMenu represents a context menu for quick commands/actions.

Today I created a Metro application and used PopupMenu to populate a list of hints for user's input. But it's just not showing anything. I digged into the issue and found a big limitation of PopupMenu: the maximum number of popup items (UIComment or UICommandSeparator) is only 6! For example following code would throw a "The operation attempted to access data outside the valid range" exception:
            var popupMenu = new PopupMenu();
            popupMenu.Commands.Add(new UICommand("Hints", null, string.Empty));
            popupMenu.Commands.Add(new UICommandSeparator());
            popupMenu.Commands.Add(new UICommand("Hint 1", null, "Hint 1"));
            popupMenu.Commands.Add(new UICommand("Hint 2", null, "Hint 2"));
            popupMenu.Commands.Add(new UICommand("Hint 3", null, "Hint 3"));
            popupMenu.Commands.Add(new UICommand("Hint 4", null, "Hint 4"));
            popupMenu.Commands.Add(new UICommand("Hint 5", null, "Hint 5")); // <== Throw exception here
I am not sure if it's bug or limitation. There may be some reason behind that but I couldn't find any information about this limit in any MSDN documentation. Another limitation of PopupMenu is that you can't customize the look and feel of it. You may end up implementing your own popup control since the out-of-box popup function is so limited.
2012-8-28 update: the same issue still exists in the latest Windows 8 RTM version. Let's just say this is by design and is an undocumented limitation.