WpfHexEditor.Sample.ServiceUsage
📋 Description
This console application demonstrates how to use all 10 services from WPFHexaEditor directly without the HexEditor UI control. This is useful for:
- Understanding the service architecture
- Using services in headless/automated scenarios
- Learning the service APIs
- Testing service functionality
- Building custom tools with WPFHexaEditor services
🎯 Purpose
Shows practical usage of:
- SelectionService - Selection validation and byte retrieval
- FindReplaceService - Searching and replacing patterns
- ClipboardService - Copy/paste operations
- HighlightService - Managing highlighted positions
- ByteModificationService - Inserting, deleting, and modifying bytes
- UndoRedoService - Undo/redo history management
- BookmarkService - Bookmark management and navigation
- CustomBackgroundService - Background color blocks
- PositionService - Position calculations and conversions
- TblService - Character table operations
🚀 Running the Sample
cd Sources/Samples/WpfHexEditor.Sample.ServiceUsage
dotnet run
📖 What It Does
The sample:
- Creates a temporary test file with recognizable patterns
- Opens it with a
ByteProvider - Demonstrates each service with practical examples
- Shows console output explaining each operation
- Cleans up the temporary file
💡 Key Concepts Demonstrated
Service Independence
- Services work without the HexEditor UI
- Each service is instantiated and used independently
- Services only depend on
ByteProvider(for data services)
Stateless vs Stateful Services
- Stateless (6): SelectionService, FindReplaceService, ClipboardService, ByteModificationService, UndoRedoService, PositionService
- Stateful (4): HighlightService, BookmarkService, CustomBackgroundService, TblService
Common Patterns
// Stateless service - no state to maintain
var selectionService = new SelectionService();
var bytes = selectionService.GetSelectionBytes(provider, start, stop);
// Stateful service - maintains internal state
var highlightService = new HighlightService();
highlightService.AddHighLight(100, 4);
bool isHighlighted = highlightService.IsHighlighted(102); // true
📚 Code Highlights
Demo 1: SelectionService
- Validates selections
- Fixes inverted ranges
- Retrieves selected bytes
- Adjusts out-of-bounds selections
Demo 2: FindReplaceService
- Searches for byte patterns
- Finds first, last, and all occurrences
- Uses search cache for performance
- Demonstrates cache management
Demo 3: ClipboardService
- Checks copy/delete permissions
- Prepares copy data
- Supports multiple copy modes
Demo 4: HighlightService
- Adds/removes highlights
- Checks highlight status
- Groups consecutive ranges
- Manages highlight state
Demo 5: ByteModificationService
- Checks modification permissions
- Modifies, inserts, deletes bytes
- Validates operations
Demo 6: UndoRedoService
- Manages undo/redo stack
- Performs undo/redo operations
- Clears history
Demo 7: BookmarkService
- Creates bookmarks with descriptions
- Navigates between bookmarks
- Filters by marker type
- Manages bookmark state
Demo 8: CustomBackgroundService
- Defines colored regions
- Checks for overlaps
- Queries blocks by position/range
- Manages block state
Demo 9: PositionService
- Calculates line/column numbers
- Converts hex strings
- Validates and clamps positions
- Handles position arithmetic
Demo 10: TblService
- Loads character tables
- Converts bytes to strings
- Manages TBL bookmarks
- Handles custom encodings
🔧 Requirements
- .NET 8.0 or later
- Windows (for WPF dependencies like
SolidColorBrush)
📝 Notes
- All services are in the
WpfHexaEditor.Servicesnamespace - Services use dependency injection via method parameters
- No UI thread required - services are thread-safe for read operations
- The sample creates a 1KB test file with recognizable patterns
🎓 Learning Path
- Run the sample to see console output
- Read the code to understand service APIs
- Modify demos to experiment with different operations
- Use services in your own applications
🔗 Related Documentation
- Services/README.md - Complete service documentation
- ARCHITECTURE.md - Architecture overview
- ServiceUsageExample.md - Code examples
✨ Sample by Derek Tremblay and contributors