Project Organization
Application Entry Point
App.xaml
App.xaml contains static and theme resources that other XAML files reference:
- StaticResource: Fixed resources loaded once
- ThemeResource: Resources that respond to theme changes
App.xaml.cs
The main entry point navigates toMainPage on startup:
src/Calculator/App.xaml.cs
Page Structure
MainPage.xaml
MainPage is the only concrete Page class in Calculator. It serves as the root container for all UI elements.- Uses
NavigationViewcontrol for the mode menu - Contains empty containers for delay-loaded UI
- Manages mode switching
UserControls
Calculator uses three main UserControls to support all modes:Calculator.xaml
Container for Standard, Scientific, and Programmer modes
DateCalculator.xaml
Date calculation interface
UnitConverter.xaml
All converter modes including currency
Calculator.xaml
This UserControl is a container that hosts three operator panels:- CalculatorStandardOperators.xaml - Basic operations
- CalculatorScientificOperators.xaml - Scientific functions
- CalculatorProgrammerOperators.xaml - Bitwise operations
VisualStates
VisualStates change the size, position, and appearance of UI elements to create responsive, adaptive layouts. Transitions are typically triggered by window size changes.Key VisualState Examples
- History/Memory Panel
- Unit Converter Layout
In Standard, Scientific, and Programmer modes, the History/Memory panel adapts based on window size:
- Small window: Panel appears as a flyout overlay
- Large window: Panel docks along the edge
VisualState Example
VisualState Implementation
VisualStates are defined in XAML usingVisualStateManager:
VisualStateManager
Data Binding
Calculator uses data binding to connect XAML UI elements to ViewModel properties, enabling automatic UI updates.x:Bind vs Binding
Calculator uses two data binding syntaxes:- x:Bind (Preferred)
- Binding (Legacy)
Modern, compiled binding with better performance:Benefits:
x:Bind Example
- Compile-time validation
- Better performance
- Supports function binding
Binding Modes
OneWay
ViewModel → ViewMost common for display
TwoWay
ViewModel ↔ ViewFor editable fields
OneTime
Single initial valueFor static content
Data Binding Example
Here’s how expression tokens are displayed:Expression Token Binding (src/Calculator/Views/Calculator.xaml)
ExpressionTokens property is defined in StandardCalculatorViewModel:
src/CalcViewModel/StandardCalculatorViewModel.h
When
ExpressionTokens changes, the ViewModel raises a PropertyChanged event, and the UI automatically updates.Custom Controls
Calculator includes custom controls for specialized functionality:- OverflowTextBlock - Handles text overflow with scrolling
- CalculatorButton - Customized button with calculator-specific behavior
- CalculationResult - Display component for results
Command Binding
User actions trigger commands defined in ViewModels:Command Binding (src/Calculator/Views/MainPage.xaml)
These buttons are hidden (for keyboard shortcuts) but their commands are bound to ViewModel methods.
Resource Management
Resources are defined at multiple levels:Application-Level (App.xaml)
Global resources like fonts, colors, and styles used throughout the app.
Best Practices
Prefer x:Bind over Binding
Prefer x:Bind over Binding
Use
x:Bind for better performance and compile-time validation. Update legacy Binding expressions when modifying existing code.Test All VisualStates
Test All VisualStates
UI changes should be tested across all defined VisualStates and window sizes.
Keep Code-Behind Minimal
Keep Code-Behind Minimal
Use code-behind only for View-specific logic like animations. Business logic belongs in ViewModels.
Use Data Binding
Use Data Binding
Avoid manual UI updates. Let data binding handle synchronization between View and ViewModel.
Next Steps
ViewModel Layer
Learn how ViewModels provide data to Views
MVVM Pattern
Understand the overall architecture pattern