Overview
TheUnitConverterViewModel class manages unit conversions for various categories (Currency, Volume, Length, Weight, Temperature, Energy, etc.). It handles real-time currency data, supplementary results, and bidirectional conversion between units.
Namespace: CalculatorApp.ViewModel
Implements: Windows::UI::Xaml::Data::INotifyPropertyChanged
Declared in: UnitConverterViewModel.h:156
Observable Properties
Category and Units
Categories
CurrentCategory
Units collection and resets the converter.
Units
Mode
Input/Output Values
Unit1
Value1
Value1Active
Unit2
Value2
Value2Active
Currency-Specific Properties
CurrencySymbol1
CurrencySymbol2
CurrencySymbolVisibility
Collapsed if either symbol is empty.
IsCurrencyCurrentCategory
IsCurrencyLoadingVisible
CurrencyDataLoadFailed
CurrencyDataIsWeekOld
CurrencyRatioEquality
CurrencyRatioEqualityAutomationName
CurrencyTimestamp
NetworkBehavior
Normal- Fetch from networkOffline- Use cached data onlyOptIn- Ask user permission
Supplementary Results
SupplementaryResults
SupplementaryVisibility
Visible if supplementary results are available, otherwise Collapsed.
UI State Properties
IsDecimalEnabled
IsDropDownOpen
IsDropDownEnabled
Accessibility
Value1AutomationName
Value2AutomationName
Unit1AutomationName
Unit2AutomationName
Announcement
Commands
CategoryChanged
UnitChanged
SwitchActive
ButtonPressed
CopyCommand
PasteCommand
Key Methods
Constructors
Public Methods
OnPaste
RefreshCurrencyRatios
OnValueActivated
AnnounceConversionResult
SaveUserPreferences
RestoreUserPreferences
Internal Methods
UpdateDisplay
UpdateSupplementaryResults
OnMaxDigitsReached
OnCurrencyDataLoadFinished
OnCurrencyTimestampUpdated
Helper Classes
Category
Unit
SupplementaryResult
INotifyPropertyChanged Implementation
The ViewModel uses theOBSERVABLE_OBJECT_CALLBACK macro:
OnPropertyChanged for custom handling.
Usage Examples
Basic Unit Conversion
Currency Conversion
Switching Active Fields
XAML Binding Example
Persistence
The ViewModel automatically saves and restores user preferences:- Last selected category
- Last selected units for each category
Currency Data Management
Currency rates are fetched from the network and cached:- Initial load shows loading indicator (
IsCurrencyLoadingVisible) - Data is cached locally with timestamp
- If data is over 1 week old,
CurrencyDataIsWeekOldis set - If network fails,
CurrencyDataLoadFailedis set - User can manually refresh with
RefreshCurrencyRatios()
Accessibility Features
- Separate automation names for all values and units
- Live announcements for conversion results
- Support for whimsical units (special units with unique formatting)
- Localized number formatting respects regional settings
See Also
- ApplicationViewModel - Application-level state management
- IUnitConverter - Conversion engine interface