XAML代码如下:
<UserControl x:Class="WpfApp1.Views.NotificationService"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" xmlns:ViewModels="clr-namespace:WpfApp1.ViewModels"
mc:Ignorable="d"
DataContext="{dxmvvm:ViewModelSource Type=ViewModels:NotificationServiceViewModel}"
d:DesignHeight="300" d:DesignWidth="300">
<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 x:Name="ServiceWithDefaultNotifications"
ApplicationId="sample_notification_app"
PredefinedNotificationTemplate="ShortHeaderAndTwoTextFields" />
<dxmvvm:NotificationService x:Name="ServiceWithCustomNotifications"
CustomNotificationTemplate="{StaticResource CustomNotificationTemplate}"
CustomNotificationPosition="BottomRight" />
</dxmvvm:Interaction.Behaviors>
<Grid>
<StackPanel VerticalAlignment="Center"
HorizontalAlignment="Center">
<Button Content="Default notification"
Command="{Binding ShowDefaultNotificationCommand}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Margin="2" />
<Button Content="Custom notification"
Command="{Binding ShowCustomNotificationCommand}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Margin="2" />
</StackPanel>
</Grid>
</UserControl>
对应的MODEL VIEW
[POCOViewModel]
public class NotificationServiceViewModel
{
[ServiceProperty(Key = "ServiceWithDefaultNotifications")]
protected virtual INotificationService DefaultNotificationService { get { return null; } }
[ServiceProperty(Key = "ServiceWithCustomNotifications")]
protected virtual INotificationService CustomNotificationService { get { return null; } }
public void ShowDefaultNotification()
{
INotification notification = DefaultNotificationService.CreatePredefinedNotification("Predefined Notification", "First line", String.Format("Second line. Time: {0}", DateTime.Now), null);
notification.ShowAsync();
}
public 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);
notification.ShowAsync();
}
}
[POCOViewModel]
public class CustomNotificationViewModel
{
public virtual string Caption { get; set; }
public virtual string Content { get; set; }
}