https://github.com/Federerer/Notifications.Wpf
Burada MVVM modeli için güzel bir örnek var. Ben MVVM kullanmadan standart özelleştirilmiş bir notification yapımını göstereceğim.
1. Notifications.Wpf paketini projemize nuget ile ekleyelim.
2. NotificationView adında bir User Control ekleyelim.
3. NotificationView.xaml içerisine aşağıdaki xmlns ekleyelim.
xmlns:controls="clr-namespace:Notifications.Wpf.Controls;assembly=Notifications.Wpf"
Ayrıca notification kapatmak için button içerisine controls:Notification.CloseOnClick="True"
property eklendi. Yani bu butona basıldığı zaman Notification kapatılacak.
4. NotificationView.cs içerisine aşağıdaki kodları ekleyin
private readonly INotificationManager _manager;
public string Title { get; set; }
public string Message { get; set; }
public NotificationView(INotificationManager manager)
{
InitializeComponent();
_manager = manager;
}
5. Açmak için is MainWindows içerisinde aşağıdaki NotifcationManager tanımlayalım.
private NotificationManager notificationManager;
MainControl() constructor içerisinde notificationManager'ın bir örneğini oluşturalım
notificationManager = new NotificationManager();
Daha sonra notification ile göstermek için
var content = new NotificationView(notificationManager);
ile gösterilecek notification oluşturulur.
notificationManager.Show(content, expirationTime: TimeSpan.FromSeconds(30));
metodu ile gösterilir.
MainWindow.xaml
<Window x:Class="DesktopNotificationApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:DesktopNotificationApp" mc:Ignorable="d" Title="MainWindow" Height="auto" Width="291.231"> <Grid> <Grid.RowDefinitions> <RowDefinition MinHeight="50" Height="auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Button Content="Bildirim Göster" Click="Button_Click"/> <ListBox x:Name="lbSecimler" Grid.Row="1"/> </Grid> </Window>
MainWindow.xaml.cs
using Notifications.Wpf; using System; using System.Collections.Generic; using System.Windows; namespace DesktopNotificationApp { public partial class MainWindow : Window { private NotificationManager notificationManager; public MainWindow() { InitializeComponent(); notificationManager = new NotificationManager(); } int titleNumber; private void Button_Click(object sender, RoutedEventArgs e) { var content = new NotificationView(notificationManager); content.txbTitle.Text = $"{++titleNumber}. Başlık"; content.txbMessage.Text = $"{titleNumber}. Mesaj"; content.cmbSelectAnswer.ItemsSource = new List<string>() { $"Test A{titleNumber}", $"Test B{titleNumber}", }; notificationManager.Show(content, expirationTime: TimeSpan.FromSeconds(30)); } } }
NotificationView.xaml
<UserControl x:Class="DesktopNotificationApp.NotificationView" 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:local="clr-namespace:DesktopNotificationApp" xmlns:controls="clr-namespace:Notifications.Wpf.Controls;assembly=Notifications.Wpf" mc:Ignorable="d" Height="172.388" Width="auto"> <Grid Background="LightSkyBlue"> <Grid.RowDefinitions> <RowDefinition MinHeight="40" Height="auto"/> <RowDefinition Height="*"/> <RowDefinition MinHeight="40" Height="auto"/> </Grid.RowDefinitions> <TextBlock x:Name="txbTitle" FontSize="20" FontWeight="Medium" Foreground="Black"/> <Grid Grid.Row="1"> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="auto"/> </Grid.RowDefinitions> <TextBlock x:Name="txbMessage" FontSize="12" FontWeight="Medium" Background="White" Foreground="Black" TextWrapping="Wrap"/> <ComboBox x:Name="cmbSelectAnswer" Grid.Row="1" SelectedIndex="0"/> </Grid> <UniformGrid Grid.Row="2" Columns="2"> <Button Content="KAPAT" controls:Notification.CloseOnClick="True" Click="Dismiss"/> <Button Content="SEÇ" controls:Notification.CloseOnClick="True" Grid.Column="1" Click="Select"/> </UniformGrid> </Grid> </UserControl>
NotificationView.xaml.cs
using Notifications.Wpf; using System.Linq; using System.Windows; using System.Windows.Controls; namespace DesktopNotificationApp { public partial class NotificationView : UserControl { private readonly INotificationManager _manager; private MainWindow mainWindow => Application.Current.Windows.OfType<MainWindow>().Single(); public string Title { get; set; } public string Message { get; set; } public NotificationView(INotificationManager manager) { InitializeComponent(); _manager = manager; } private void Dismiss(object sender, RoutedEventArgs e) { var text = $"{txbTitle.Text} seçim yapılmadı."; mainWindow.lbSecimler.Items.Add(new ListBoxItem() { Content = text }); } private void Select(object sender, RoutedEventArgs e) { var text = cmbSelectAnswer.Text; mainWindow.lbSecimler.Items.Add(new ListBoxItem() { Content = text }); } } }