locked
Simpler way to add several buttons for navigation to new page?

    Question

  • Hi
    Is there a more elegant way to write the code below? So far I got three buttons in my app, and each new button I Will have to add the navigation line and the exception test. Is there a way to do this after the switch statement?

    Thanks, Sigurd F

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                Button btnClicked = sender as Button;
                switch (btnClicked.Name.ToString()) {
                    case "btnFlagQuiz":
                        if (!Frame.Navigate(typeof(FlagQuizPage)))
                        {
                            throw new Exception(this.resourceLoader.GetString("NavigationFailedExceptionMessage"));
                        }
                        break;
    
                    case "btnCapitalQuiz":
                        if (!Frame.Navigate(typeof(CapitolQuizPage)))
                        {
                            throw new Exception(this.resourceLoader.GetString("NavigationFailedExceptionMessage"));
                        }
                        break;
    
                    case "btnFlagOverview":
                        if (!Frame.Navigate( typeof(FlagOverviewPage)))
                        {
                            throw new Exception(this.resourceLoader.GetString("NavigationFailedExceptionMessage"));
                        }
                        break;
                }
            }
    


    Saturday, April 25, 2015 10:53 AM

Answers

  • This seems fine to me. I don't know why you're looking to optimize this piece of simple code.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by Sigurd F Monday, April 27, 2015 6:56 AM
    Saturday, April 25, 2015 12:26 PM
    Moderator

All replies

  • This seems fine to me. I don't know why you're looking to optimize this piece of simple code.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by Sigurd F Monday, April 27, 2015 6:56 AM
    Saturday, April 25, 2015 12:26 PM
    Moderator
  • I hoped it was possible to simplify the code to something like the code below. But if the my is already good i just end my quest for simpler code :-)
    Thanks

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                string sPageName;
                Button btnClicked = sender as Button;
                switch (btnClicked.Name.ToString())
                {
                    case "btnFlagQuiz":
                        sPageName = "FlagQuizPage";
                        break;
    
                    case "btnCapitalQuiz":
                        sPageName = "CapitalQuizPage";
                        break;
    
                    case "btnFlagOverview":
                        sPageName = "FlagOverviewPage";
                        break;
                }
                if (!Frame.Navigate(typeof(sPageName)))
                {
                    throw new Exception(this.resourceLoader.GetString("NavigationFailedExceptionMessage"));
                }
            }
    
    Sigurd F
    Saturday, April 25, 2015 1:33 PM
  • I wouldn't convert the Names to strings and then comparing with string literals. For a minor change compare with the btnFlagQuiz etc. variables. This will be trivially more efficient. More importantly it will be less prone to error since the compiler will catch typos in variable names but not literals.

    You could also set up a table of button to navigation target rather than putting it in a switch statement, encapsulate that in a class the buttons are bound to, or add the navigation target to a property attached to the button.

    Sunday, April 26, 2015 5:55 AM
    Owner