CreatesCommandBinding.BindCommandToObject(ICommand?, TControl?, IObservable
Defined in
Type: CreatesCommandBinding
Namespace: ReactiveUI
Assembly: ReactiveUI.Core.dll
Applies to
net10.0, net10.0-maccatalyst26.0, net10.0-desktop1.0, net10.0-browserwasm1.0, net10.0-tvos26.0, net10.0-windows10.0.19041, net10.0-macos26.0, net10.0-ios26.0, net10.0-android36.0, net9.0, net9.0-tvos18.0, net9.0-macos15.0, net9.0-maccatalyst18.0, net9.0-windows10.0.19041, net9.0-ios18.0, net9.0-desktop1.0, net8.0, net8.0-windows10.0.19041, net8.0-ios18.0, net8.0-maccatalyst18.0, net8.0-macos15.0, net8.0-tvos18.0, net8.0-ios17.5, net8.0-maccatalyst17.5, net8.0-macos14.5, netstandard2.1, net481, net462, net471
Overloads¶
- 1.
public static IDisposable BindCommandToObject<TControl>(ICommand? command, TControl? target, IObservable<object?> commandParameter) where TControl : class - 2.
public static IDisposable BindCommandToObject<TControl, TEventArgs>(ICommand? command, TControl? target, IObservable<object?> commandParameter, string eventName) where TControl : class
1. Overload¶
Attributes: [RequiresUnreferencedCode("String/reflection-based event binding may require members removed by trimming.")]
public static IDisposable BindCommandToObject<TControl>(ICommand? command, TControl? target, IObservable<object?> commandParameter) where TControl : class
Summary: Binds an ICommand to the specified target object, enabling the command to be executed in response to events on the target. Fully AOT-compatible.
Type parameters
| Name | Description |
|---|---|
TControl | The type of the target object to which the command will be bound. Must be a class with accessible events and properties. |
Parameters
| Name | Type | Description |
|---|---|---|
command | ICommand? | The command to bind to the target object. Can be null to remove an existing binding. |
target | TControl? | The object whose events will trigger the command. Can be null if no binding should be established. |
commandParameter | IObservable | An observable sequence that provides the parameter to pass to the command when it is executed. |
Returns: IDisposable -- An IDisposable that can be used to unbind the command from the target object.
Remarks
This method uses reflection to bind the command to the target object's events and properties. Trimming tools may remove required members, so use caution when linking with trimming enabled.
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | A suitable command binder cannot be found for the specified target type. |
2. Overload¶
Attributes: [RequiresUnreferencedCode("String/reflection-based event binding may require members removed by trimming.")]
public static IDisposable BindCommandToObject<TControl, TEventArgs>(ICommand? command, TControl? target, IObservable<object?> commandParameter, string eventName) where TControl : class
Summary: Binds an ICommand to a specified event on a target object, enabling command execution in response to the event. Fully AOT-compatible.
Type parameters
| Name | Description |
|---|---|
TControl | The type of the target object to which the command is bound. Must be a class with accessible events and properties. |
TEventArgs | The type of the event arguments associated with the event to bind. |
Parameters
| Name | Type | Description |
|---|---|---|
command | ICommand? | The command to execute when the specified event is raised. Can be null to unbind any existing command. |
target | TControl? | The object that exposes the event to which the command will be bound. Must not be null. |
commandParameter | IObservable | An observable sequence that provides the parameter to pass to the command when it is executed. |
eventName | string | The name of the event on the target object that triggers command execution. Must correspond to an event defined on the target. |
Returns: IDisposable -- An IDisposable that can be used to unbind the command from the event.
Remarks
This method uses reflection to bind the command to the specified event. When the event is raised, the command is executed with the latest value from the commandParameter observable. The returned IDisposable should be disposed to detach the event handler and prevent memory leaks. This method may not be compatible with all trimming scenarios due to its use of reflection.
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | A suitable command binder cannot be found for the specified target type and event name. |