locked
How do I insert a calculator in every page?

    Question

  • I've built a simple calculator.  I'll turn it into a scientific calculator later. 

    I want to insert this calculator in a corner of every xaml page.  How would I do this?  Please point me to the right direction.

    I know the alternative is to copy and paste the code in every xaml and every xaml.cpp page.  This is cumbersome and isn't very efficient. 

    Thursday, July 25, 2013 6:17 AM

Answers

  • Make your calculator as a UserControl and then include the UserControl in each page's Xaml.

    --Rob

    • Marked as answer by RandyPete Thursday, July 25, 2013 9:45 PM
    Thursday, July 25, 2013 6:29 AM
    Owner
  • Implement the UserControl as a separate item. Start with Add.New Item... and add a UserControl. This will create new .xaml, .cpp, etc. files for your Calculator UserControl class. Put your calculator markup and code in the new class.

    You can then reference it in your various page Xaml as local:Calculator (or whatever you call the class).

    There is a pretty minimal UserControl sample in the BasicUserControl class in XAML user and custom controls sample (Windows 8) . You can delete the Grid from its Xaml and replace it with the Xaml for your calculator. Remove the ClickMeButtonClicked event handler from the code behind and add in whatever code you need to run your calculator.

    --Rob

    • Marked as answer by RandyPete Thursday, July 25, 2013 9:45 PM
    Thursday, July 25, 2013 7:14 PM
    Owner

All replies

  • Make your calculator as a UserControl and then include the UserControl in each page's Xaml.

    --Rob

    • Marked as answer by RandyPete Thursday, July 25, 2013 9:45 PM
    Thursday, July 25, 2013 6:29 AM
    Owner
  • Ok, I surrender.  I've tried for the last 2 hours to make the usercontrol work.

    I've got an xaml page name calculator.  Inside the xaml page, I've got a stackpanel name calculatrix.  The calculator buttons and textbox exist in calculatrix.

    Say I have an xaml page name probability.  The usercontrol is

    <UserControl x:Name="OutputSection" Grid.Row="0" Grid.ColumnSpan="1" BorderThickness="0" HorizontalAlignment="Right" VerticalAlignment="Top"/>

    What do I put in the cpp file? 

    Been copying from the sample I downloaded.  But in the sample cpp, there are 3 times more code than the calculator.  I can't tell which is necessary and which is just mumbo jumbo by MS coders.  But if all of it are necessary, then I might as well copy and paste the calculator code, since it's only a third the size.

    I'm really sorry for bothering you guys. 

    Thursday, July 25, 2013 5:50 PM
  • Implement the UserControl as a separate item. Start with Add.New Item... and add a UserControl. This will create new .xaml, .cpp, etc. files for your Calculator UserControl class. Put your calculator markup and code in the new class.

    You can then reference it in your various page Xaml as local:Calculator (or whatever you call the class).

    There is a pretty minimal UserControl sample in the BasicUserControl class in XAML user and custom controls sample (Windows 8) . You can delete the Grid from its Xaml and replace it with the Xaml for your calculator. Remove the ClickMeButtonClicked event handler from the code behind and add in whatever code you need to run your calculator.

    --Rob

    • Marked as answer by RandyPete Thursday, July 25, 2013 9:45 PM
    Thursday, July 25, 2013 7:14 PM
    Owner
  • Thank you very much, sir.  I've figured it out.

    Thursday, July 25, 2013 9:45 PM