# WPF Resources, Windows, and Lifecycle Apply these rules when managing resources and styles, windows and dialogs, navigation, memory, or images in WPF. ## Resources and Styles * Use shared resources for repeated styles, brushes, templates, and dimensions, in focused dictionaries at feature level unless truly global; avoid hardcoded colors and sizes when resources exist. * Use `StaticResource` by default and `DynamicResource` only where runtime replacement is required, such as theme switching. ## Windows and Dialogs * Keep window lifecycle and `Owner` relationships explicit, avoid duplicate or hidden windows kept alive without reason, and set `ShutdownMode` deliberately instead of relying on accidental window lifetime. * Show dialogs through the dialog service described in the MVVM rule. ## Navigation * Keep navigation state and history explicit, map view models to views without service locators, and release navigation targets that are no longer needed. ## Memory Management * Watch for memory leaks caused by: * Event subscriptions * Timers * Static references * Long-lived services * Cached views * Closures * Collection views * Binding-related references: binding to a CLR property of a source that does not implement `INotifyPropertyChanged` keeps the source alive; bind only to `INotifyPropertyChanged` or dependency-property sources. * Dispose resources, stop timers and background work, and release closed windows, unloaded views, and visual element references. Profile memory when a long-running app keeps growing. ## Images and Media * Decode images at display size with `BitmapImage.DecodePixelWidth` or `DecodePixelHeight`. * Load file-based images with `BitmapCacheOption.OnLoad` and close the stream, so the file is not locked. * Call `Freeze()` on immutable bitmaps and brushes, and cache images only with a clear memory limit.