7.9 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
2026-04-16T02:54:00.278627+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | 0ff9757e59948461 |
Interactivity
Documentation: DTS.Common.Interactivity Module
1. Purpose
This module provides a standardized, event-driven pattern for handling user interactions (e.g., confirmations, alerts, dialogs) in a decoupled manner—primarily intended for use with XAML-based UI frameworks (e.g., WPF, UWP) via the Microsoft.Xaml.Behaviors library. It defines core abstractions (IInteractionRequest, INotification, IConfirmation) and concrete implementations (InteractionRequest<T>, Confirmation, Notification) to enable view models to request interactions without direct coupling to the UI layer, while allowing views to respond to those requests using triggers and behaviors. The pattern supports both simple notifications and confirmations with user input (e.g., Yes/No), and ensures proper completion signaling via callbacks.
2. Public Interface
Interfaces
-
INotificationstring Title { get; set; } object Content { get; set; }Represents a generic notification with a title and arbitrary content (e.g., message, view model). Serves as the base interface for all notification types.
-
IConfirmation : INotificationbool Confirmed { get; set; }Extends
INotificationto represent a confirmation request (e.g., "Are you sure?"). TheConfirmedproperty indicates whether the user accepted the action. -
IInteractionRequestevent EventHandler<InteractionRequestedEventArgs> Raised;Represents a request for user interaction. The
Raisedevent is fired when an interaction is needed. -
IInteractionRequestAwareINotification Notification { get; set; } Action FinishInteraction { get; set; }Implemented by UI elements (e.g., views, user controls) that need to be aware of and respond to an interaction request.
Notificationprovides the context;FinishInteractionmust be invoked to signal completion.
Classes
-
Notificationpublic class Notification : INotificationConcrete implementation of
INotification. ProvidesTitleandContentproperties. -
Confirmation : Notification, IConfirmationpublic class Confirmation : Notification, IConfirmationExtends
Notificationto support confirmation semantics. Includes theConfirmedproperty (defaultfalse), set by the UI to reflect user choice.
Event Arguments
InteractionRequestedEventArgs : EventArgsPassed to event handlers whenpublic INotification Context { get; } public Action Callback { get; }IInteractionRequest.Raisedfires.Contextcontains the notification (e.g.,Confirmation).Callbackmust be invoked (typically after user action) to signal interaction completion.
Interaction Request Implementation
InteractionRequest<T> : IInteractionRequestGeneric implementation ofpublic event EventHandler<InteractionRequestedEventArgs> Raised; public void Raise(T context) where T : INotification public void Raise(T context, Action<T> callback) where T : INotificationIInteractionRequest.Raise(context)fires theRaisedevent with a no-op callback.Raise(context, callback)wraps the provided callback in anActionand passes it viaInteractionRequestedEventArgs. Note: Thecallbackparameter in the secondRaiseoverload is of typeAction<T>, but theInteractionRequestedEventArgs.CallbackisAction(non-generic). The wrapper discards theTargument and invokescallback(context).
Trigger
InteractionRequestTrigger : EventTriggerA behavior trigger designed to bind toprotected override string GetEventName() => "Raised";IInteractionRequest.Raised. OverridesGetEventName()to always return"Raised", simplifying XAML configuration (e.g.,<i:Interaction.Triggers><dts:InteractionRequestTrigger ... /></i:Interaction.Triggers>).
3. Invariants
INotificationcontract: EveryINotification(and its derivatives) must provide non-nullTitleandContentafter initialization if they are to be meaningfully displayed. However, the interface itself does not enforce non-nullability—views must handlenullgracefully.IConfirmation.Confirmed: Must be set by the UI before invokingFinishInteraction. Its value reflects the user’s choice (e.g.,truefor "Yes",falsefor "No").- Callback invocation: The
CallbackinInteractionRequestedEventArgsmust be invoked exactly once per interaction request to avoid blocking the UI or leaking resources. TheInteractionRequest<T>.Raisemethod ensures the callback is wrapped and passed correctly. - Generic constraint:
InteractionRequest<T>requiresT : INotification. This ensures only valid notification types can be used as interaction context.
4. Dependencies
Dependencies on this module:
Microsoft.Xaml.Behaviors: Required forEventTriggerbase class used byInteractionRequestTrigger.System: ForEventArgs,Action, andEventHandler.
Dependencies of this module:
- UI Layer: Views (e.g., XAML pages/user controls) must implement
IInteractionRequestAwareor use triggers (e.g.,InteractionRequestTrigger) to handle interactions. - Consuming modules: Any module requiring user interaction (e.g., view models) will reference and instantiate
InteractionRequest<T>(typicallyInteractionRequest<Confirmation>).
Inferred usage:
InteractionRequest<Confirmation>is the most common concrete instantiation (based onConfirmationclass).- Views likely bind
InteractionRequestAware.Notificationto the event args’Contextand invokeInteractionRequestAware.FinishInteractionafter user action.
5. Gotchas
- Callback signature mismatch: The
InteractionRequest<T>.Raise(T context, Action<T> callback)overload acceptsAction<T>, but theInteractionRequestedEventArgs.CallbackisAction. The wrapper() => { if (callback != null) callback(context); }discards theTargument—so the callback cannot inspect thecontextas a parameter (only via closure). This is non-intuitive; developers may expect the callback to receive the context as an argument. - No built-in validation:
InteractionRequestedEventArgsprovides no mechanism to validate or reject the interaction. Views must handle invalid states internally (e.g., by not invokingFinishInteraction). - Thread-safety:
InteractionRequest<T>does not guarantee thread-safety forRaisedevent invocation. If raised from a non-UI thread, views must marshal to the UI thread themselves. FinishInteractionmust be called: If a view fails to invokeFinishInteraction, the interaction remains "open," potentially blocking further interactions or leaving UI in an inconsistent state. This is a common source of bugs.Confirmeddefault value:Confirmation.Confirmeddefaults tofalse. If a view does not explicitly set it (e.g., for a "Cancel" button), the interaction will be treated as unconfirmed.- No cancellation support: There is no explicit cancellation token or mechanism to abort an interaction mid-process. Cancellation must be handled via custom logic (e.g., checking
CancellationRequestedonContent).
None identified from source alone.