Back to Devexpress

NotificationService

wpf-18138-mvvm-framework-services-predefined-set-notificationservice.md

latest6.5 KB
Original Source

NotificationService

  • Feb 26, 2023
  • 4 minutes to read

The NotificationService allows you to display popup notifications in your applications. You can display native Windows 10/11 notifications or create custom ones.

Create a Native Windows 10/11 Notification

To display a native Windows 10/11 notification, do the following:

  1. Create an application shortcut. Do one of the following:

  2. Create an ApplicationActivator instance and register a COM-Server.

  3. Add the NotificationService to the Interaction.Behaviors collection and specify the ApplicationId. Set the UseWin8NotificationsIfAvailable property to true.

When multiple native Windows notifications are displayed simultaneously, the optional Id parameter of the NotificationService.CreatePredefinedNotification method enables the service to identify the notification with which the user interacts. NotificationActivator passes the Id value to the arguments parameter of the OnActivate method.

Example

View Example: Create Interactive Notifications

Create a Custom Notification

You can use the NotificationService to create a custom notification that does not depend on the Windows 10/11 notification API.

Use the NotificationService.CustomNotificationTemplate property to define a custom notification layout.

xaml
<UserControl x:Class="DXSampleNotificationSevice.View.MainView"
    ...
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" 
    xmlns:ViewModel="clr-namespace:DXSampleNotificationSevice.ViewModel"  
    DataContext="{dxmvvm:ViewModelSource Type={x:Type ViewModel:MainViewModel}}">
    <UserControl.Resources>
        <DataTemplate x:Key="CustomNotificationTemplate">
            <Border Background="White" BorderThickness="1" BorderBrush="Black">
                <StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                    <TextBlock HorizontalAlignment="Left" Text="{Binding Caption}" Foreground="Blue" FontSize="20" FontWeight="Bold" Margin="5"/>
                    <TextBlock HorizontalAlignment="Center" Text="{Binding Content}" Foreground="Black" FontSize="16" Margin="3"/>
                </StackPanel>
            </Border>
        </DataTemplate>
    </UserControl.Resources>
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:NotificationService CustomNotificationTemplate="{StaticResource CustomNotificationTemplate}" CustomNotificationPosition="BottomRight" />
    </dxmvvm:Interaction.Behaviors>
    <Grid>
        <Button Content="Show Custom Notification" Command="{Binding ShowCustomNotificationCommand}" VerticalAlignment="Center" Height="30"/>
    </Grid>
</UserControl>

To create a custom notification, use the NotificationService.CreateCustomNotification method. This method requires a notification’s View Model as a parameter.

csharp
[POCOViewModel]
public class MainViewModel {
    protected virtual INotificationService CustomNotificationService { get { return null; } }
    public async void ShowCustomNotification() {
        CustomNotificationViewModel vm = ViewModelSource.Create(() => new CustomNotificationViewModel());
        vm.Caption = "Custom Notification";
        vm.Content = String.Format("Time: {0}", DateTime.Now);
        INotification notification = CustomNotificationService.CreateCustomNotification(vm);
        NotificationResult result = await notification.ShowAsync();
        ProcessNotificationResult(result);
    }
    void ProcessNotificationResult(NotificationResult result) {
        ...
    }
    ...
}
vb
<POCOViewModel>
Public Class MainViewModel
    Protected Overridable ReadOnly Property CustomNotificationService() As INotificationService
        Get
            Return Nothing
        End Get
    End Property
    Public Async Sub ShowCustomNotification()
        Dim vm As CustomNotificationViewModel = ViewModelSource.Create(Function() New CustomNotificationViewModel())
        vm.Caption = "Custom Notification"
        vm.Content = String.Format("Time: {0}", Date.Now)
        Dim notification As INotification = CustomNotificationService.CreateCustomNotification(vm)
        Dim result As NotificationResult = Await notification.ShowAsync()
        ProcessNotificationResult(result)
    End Sub
    Private Sub ProcessNotificationResult(ByVal result As NotificationResult)
        '...
    End Sub
    '...
End Class

Use the INotification.ShowAsync method to display the created notification. This method returns a NotificationResult enumeration value depending on how the notification is displayed and which notification element the user clicks. The ShowAsync method returns the following values depending on the notification result:

  • Activated , if the user clicks the notification.
  • UserCanceled , if the user clicks the notification’s close button.
  • TimedOut , if the user does not click the notification within the time span specified in the NotificationService.CustomNotificationDuration or NotificationService.PredefinedNotificationDuration property.
  • ApplicationHidden , if the INotification.Hide method is called while the notification is displayed.
  • Dropped , if the system’s notification queue is full and the notification cannot be displayed.

Example

View Example: Use the NotificationService to Display Notifications