DevExpress MVVM UI Services for .NET MAUI
- 3 minutes to read
UIServiceContainer and UIServiceContainerExtensions classes allow you to manage UI-related services at the ViewModel level. Use these classes to maintain a full UI service lifecycle without direct dependencies between the ViewModel and the View.
UIServiceContainer
The UIServiceContainer class implements the IUIServiceContainer interface and serves as a central registry for UI-related services. The class allows a ViewModel to access UI services defined in XAML or registered manually.
You can use the following methods to manage services in the container:
- Register(Object, String)
- Registers the specified service in the container.
- Unregister(Object)
- Removes the specified service from the container.
- GetService(Type, String)
- Gets a registered UI service by its type and key in the container.
- Clear()
- Remove all registered UI services from the container.
UIServiceContainerExtensions
The UIServiceContainerExtensions class provides access to extension methods that complement the IUIServiceContainer interface.
You can use the following extension methods to get a service from the specified IUIServiceContainer:
- GetService<T>(IUIServiceContainer, String)
- Gets the service from the specified IUIServiceContainer.
- GetRequiredService<T>(IUIServiceContainer, String)
- Gets the service from the specified IUIServiceContainer. If the required service is not found, the method throws an exception.
- GetRequiredService(IUIServiceContainer, Type, String)
- Gets the service from the specified IUIServiceContainer. If the requested service is not found, the method throws an exception.
Register Service in XAML
You can attach UI Services directly to visual controls to access these controls.
The following example uses the UIServiceBase<T> class to create a custom ScrollService
. The service calls the DXCollectionView.ScrollTo() method to scroll the DXCollectionView to the last element.
You need to create the IScrollService
interface that defines methods designed to interact with a UI element. Implement this interface in the ScrollService
class derived from UIServiceBase<T>.
public interface IScrollService {
void ScrollToEnd();
}
public class ScrollService : UIServiceBase<DXCollectionView>, IScrollService
{
public void ScrollToEnd()
{
AssociatedObject.ScrollTo(AssociatedObject.VisibleItemCount, DXScrollToPosition.End);
}
}
To access the UI service at the ViewModel and call ViewModel methods without direct access to the control in the View, add the ScrollService
class to the Behaviors
collection of the target control. If the ViewModel implements the IUIServiceClient interface, the ScrollService
is registered in the ViewModel stored in the BindingContext
.
<dx:DXCollectionView>
<dx:DXCollectionView.Behaviors>
<local:ScrollService/>
</dx:DXCollectionView.Behaviors>
</dx:DXCollectionView>
In the ViewModel, you can access the registered service through the service container and trigger UI-related actions through the service interface.
public partial class ViewModel : IUIServiceClient {
public IUIServiceContainer ServiceContainer { get; } = new UIServiceContainer();
void ShowLastItem() {
var scrollService = ServiceContainer.GetRequiredService<IScrollService>();
scrollService.ScrollToEnd();
}
}
Register Service in Code-Behind
The following example uses UIServiceContainer.Register(Object, String) and UIServiceContainerExtensions.GetService<T>(IUIServiceContainer, String) methods to register and retrieve a UI service:
public interface IMessageService
{
void ShowMessage(string message);
}
public class MessageService : IMessageService
{
public void ShowMessage(string message)
{
Console.WriteLine($"Message: {message}");
}
}
public class MainViewModel : DXObservableObject
{
private readonly IUIServiceContainer serviceContainer;
public ICommand ShowMessageCommand { get; }
// Parameterless constructor for XAML compatibility
public MainViewModel() : this(new UIServiceContainer())
{
}
public MainViewModel(IUIServiceContainer serviceContainer)
{
this.serviceContainer = serviceContainer;
((IUIServiceContainer)this.serviceContainer).Register(new MessageService());
ShowMessageCommand = new Command(DisplayMessage);
}
private void DisplayMessage()
{
var messageService = serviceContainer.GetService<IMessageService>();
messageService?.ShowMessage("Hello from UIServiceContainer!");
}
}
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
// Initialize the service container and ViewModel manually
var serviceContainer = new UIServiceContainer();
((IUIServiceContainer)serviceContainer).Register(new MessageService());
BindingContext = new MainViewModel(serviceContainer);
}
}