Skip to content

)} ,System.String)}

CreatesCommandBinding.BindCommandToObject(ICommand?, TControl?, IObservable) method

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

View source

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

NameDescription
TControlThe type of the target object to which the command will be bound. Must be a class with accessible events and properties.

Parameters

NameTypeDescription
commandICommand?The command to bind to the target object. Can be null to remove an existing binding.
targetTControl?The object whose events will trigger the command. Can be null if no binding should be established.
commandParameterIObservableAn 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

TypeCondition
System.InvalidOperationExceptionA 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

View source

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

NameDescription
TControlThe type of the target object to which the command is bound. Must be a class with accessible events and properties.
TEventArgsThe type of the event arguments associated with the event to bind.

Parameters

NameTypeDescription
commandICommand?The command to execute when the specified event is raised. Can be null to unbind any existing command.
targetTControl?The object that exposes the event to which the command will be bound. Must not be null.
commandParameterIObservableAn observable sequence that provides the parameter to pass to the command when it is executed.
eventNamestringThe 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

TypeCondition
System.InvalidOperationExceptionA suitable command binder cannot be found for the specified target type and event name.