locked
DelegateCommand creates “Access Violation” on page navigation in Windows 8.1 Universal App

    Question

  • I have an App which binds a series of commands to a Telerik RadialMenu using DelegateCommand (Telerik implementation if that matters). Some of these work OK and just flash up dialogs for now while I test. If I create a DelegateCommand which involves page navigation then the App terminates with an Access Violation error when it tries to navigate pages using the standard W8.1 Universal App navigation (Frame.Navigate(typeof(PageName)). If I navigate to the page off a normal button with an event handler it works just fine.

    Code below is the setup for the buttons on the RadialMenu.

                // Prepare RadialMenu content
                MenuItems = new ObservableCollection<MenuItem>();
                MenuItems.Add(new MenuItem() { Header = "Beer", ImagePath = "Assets/Icons/Beverage-Beer.png", Command = new DelegateCommand(() => RadialCommand("Beer"), true) });
                MenuItems.Add(new MenuItem() { Header = "Wine", ImagePath = "Assets/Icons/Beverage-wine.png", Command = new DelegateCommand(() => RadialCommand("Wine"), true) });
                MenuItems.Add(new MenuItem() { Header = "Cocktail", ImagePath = "Assets/Icons/Beverage-Cocktail.png", Command = new DelegateCommand(() => RadialCommand("Cocktail"), true) });
                MenuItems.Add(new MenuItem() { Header = "Donuts", ImagePath = "Assets/Icons/donut_2_white-128.png", Command = new DelegateCommand(() => RadialCommand("Donuts"), true) });
                MenuItems.Add(new MenuItem() { Header = "Earn", ImagePath = "Assets/Icons/Run.png", Command = new DelegateCommand(() => RadialCommand("Earn"), true) });
                MenuItems.Add(new MenuItem() { Header = "Settings", ImagePath = "Assets/Icons/Settings-01.png", Command = new DelegateCommand(() => GoToPage("SettingsPage"), true) });

    And here's the commands:

            private void GoToPage(string page)
            {
                switch (page)
                {
                    case "SettingsPage":
                        {
                            if (!Frame.Navigate(typeof(SettingsPage), null))
                            {
                                throw new Exception(this.resourceLoader.GetString("NavigationFailedExceptionMessage"));
                            }
                            //MessageDialog message = new MessageDialog("The command is executing with parameter " + PageName);
                            //await message.ShowAsync();
                            break;
                        }
    
                    default: break;
                }
            }
    
            private async void RadialCommand(string radialParameter)
            {
                MessageDialog message = new MessageDialog("The command is executing with parameter " + radialParameter);
                await message.ShowAsync();
            }


    Woke up one day and realised reality television had destroyed too many brain cells so started writing App's to re-start my brain.

    Thursday, November 13, 2014 9:54 AM

All replies

  • Run in the debugger and break in the command handler to make sure everything you think is valid is valid. You don't say where the GoToPage implementation is defined: Is it in a page with a valid Frame? Is setting page valid? Does the same Frame.Navigate call work directly from a button click?
    Friday, November 14, 2014 2:14 AM
    Owner
  • Thanks,

    To get to this point I have placed the GoToPage function, the MenuItems collection and everything else all into HubPage.xaml.cs and created a second page called SettingsPage (.xaml and .xaml.cs all done from standard templates).

    I can get Frame.Navigate to work if I place it in a normal button click event handler and it goes to SettingsPage without issue.

    The difference seems to be when GoToPage is called from within a DelegateCommand and I'm wondering if this has something to do with GoToPage executing, changing the page for the Frame and trying to dispose the HubPage before the DelegateCommand finishes (or this could be completely on the wrong track).

    If I step through in the Debugger the code runs through GoToPage and back to the DelegateCommand before causing the Access Violation.

    I don't think it has to do with the Telerik RadialMenu control directly as I could recreate the issue using a similar command binding to a button.


    Woke up one day and realised reality television had destroyed too many brain cells so started writing App's to re-start my brain.

    Friday, November 14, 2014 2:36 AM
  • So it gets past the Frame.Navigate successfully and then AVs?

    Can you reproduce without Telerik's DelegateCommand? Try the RelayCommand from Visual Studio's templates.

    Does the exception give any useful information to see where the AV occurs? What is the stack at the first chance exception?

    Saturday, November 15, 2014 3:19 AM
    Owner