Class ViewBehavior<TView, TData>
Definition
Namespace: StardewUI.Framework.Behaviors
Assembly: StardewUI.dll
Base class for a behavior extension, which enables self-contained, stateful behaviors to be "attached" to an arbitrary view without having to extend the view itself.
public class ViewBehavior<TView, TData> :
StardewUI.Framework.Behaviors.IViewBehavior, System.IDisposable
Type Parameters
TView
Base type for all views that support this behavior.
TData
Type of data provided to this behavior as an argument/binding.
Inheritance
Object ⇦ ViewBehavior<TView, TData>
Implements
IViewBehavior, IDisposable
Remarks
Behaviors receive the View which is decorated by the behavior, and some arbitrary Data obtained from the attribute value or binding. They then become part of the UI's update loop, via their Update(TimeSpan) method running every tick.
Members
Constructors
Name | Description |
---|---|
ViewBehavior<TView, TData>() |
Properties
Name | Description |
---|---|
Data | The assigned or bound data. |
View | The currently-attached view. |
ViewState | State overrides for the View. |
Methods
Name | Description |
---|---|
CanUpdate() | Checks whether the behavior is allowed to Update(TimeSpan). |
Dispose() | |
Initialize(BehaviorTarget) | Initializes the target (view, state overrides, etc.) for the behavior. |
OnAttached() | Runs after the behavior is attached to a target. |
OnDetached(IView) | Runs when the behavior is detached from a target. |
OnDispose() | Runs when the behavior is being disposed. |
OnNewData(TData) | Runs when the Data of this behavior is changed. |
PreUpdate(TimeSpan) | Runs on every update tick, before any bindings or views update. |
Update(TimeSpan) | Runs on every update tick. |
Details
Constructors
ViewBehavior<TView, TData>()
Properties
Data
The assigned or bound data.
Property Value
TData
View
The currently-attached view.
Property Value
TView
ViewState
State overrides for the View.
Property Value
Methods
CanUpdate()
Checks whether the behavior is allowed to Update(TimeSpan).
Returns
true
to continue running Update(TimeSpan) ticks, false
to skip updates.
Remarks
Implementations can override this in order to selectively disable updates. Typically, updates will be disabled when the behavior cannot run due to not having an attached view or data.
Dispose()
Initialize(BehaviorTarget)
Initializes the target (view, state overrides, etc.) for the behavior.
Parameters
target
BehaviorTarget
The target of the behavior.
Remarks
The framework guarantees that Update(TimeSpan) will never be called before Initialize
, so views may be implemented with default parameterless constructors and perform initialization in this method.
OnAttached()
Runs after the behavior is attached to a target.
Remarks
Setup code should go in this method to ensure that the values of View and ViewState are actually assigned. If code runs in the behavior's constructor, these are not guaranteed to be populated.
OnDetached(IView)
Runs when the behavior is detached from a target.
Parameters
view
IView
The view that was previously attached.
Remarks
Behaviors may receive new views as part of a "rebind", if the old view is destroyed and recreated, for example as the result of a conditional binding changing states.
OnDetached
is always immediately followed by OnAttached(). A behavior cannot remain in a detached state; however, overriding this method gives behaviors the opportunity to clean up state from the old view (e.g. remove event handlers) before the new one is attached.
Also runs when the behavior is disposed, so detach logic does not need to be duplicated in OnDispose().
OnDispose()
Runs when the behavior is being disposed.
Remarks
The default implementation does nothing. Overriding this allows subclasses to perform their own cleanup, if required by the specific feature.
OnNewData(TData)
Runs when the Data of this behavior is changed.
Parameters
previousData
TData
Remarks
At the time this method runs, Data has already been assigned to the new value. After the method completes, the previousData
will no longer be accessible to this behavior.
PreUpdate(TimeSpan)
Runs on every update tick, before any bindings or views update.
Parameters
elapsed
TimeSpan
Remarks
Typically used to read information about the underlying view as it existed at the beginning of the frame, e.g. to handle a transition.
Update(TimeSpan)
Runs on every update tick.
Parameters
elapsed
TimeSpan
Time elapsed since the last update.