Created
May 16, 2019 12:48
-
-
Save yowl/84d78488d40f5b461ae5cfeb7d45f0a0 to your computer and use it in GitHub Desktop.
toast using a popup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<UserControl x:Class="TheHub.Silverlight.Controls.ToastNotification" | |
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" | |
mc:Ignorable="d" | |
x:Name="root"> | |
<Popup x:Name="NotificationPopup" IsHitTestVisible="False" > | |
<VisualStateManager.VisualStateGroups> | |
<VisualStateGroup x:Name="VisualStateGroup"> | |
<VisualStateGroup.Transitions > | |
<VisualTransition GeneratedDuration="5" To="Normal"> | |
</VisualTransition> | |
</VisualStateGroup.Transitions> | |
<VisualState x:Name="Normal" > | |
<Storyboard x:Name="FaderStory"> | |
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="LayoutRoot"> | |
<EasingDoubleKeyFrame KeyTime="0" Value="0"/> | |
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/> | |
</DoubleAnimationUsingKeyFrames> | |
</Storyboard> | |
</VisualState> | |
<VisualState x:Name="Hidden" > | |
<Storyboard x:Name="HideFaderStory"> | |
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="LayoutRoot"> | |
<EasingDoubleKeyFrame KeyTime="0" Value="1"/> | |
</DoubleAnimationUsingKeyFrames> | |
</Storyboard> | |
</VisualState> | |
</VisualStateGroup> | |
</VisualStateManager.VisualStateGroups> | |
<Border BorderBrush="DarkRed" BorderThickness="2"> | |
<Grid x:Name="LayoutRoot" Background="AliceBlue" Height="45" Width="200" VerticalAlignment="Center" HorizontalAlignment="Center" MouseLeftButtonUp="LayoutRoot_MouseLeftButtonUp"> | |
<StackPanel Orientation="Vertical"> | |
<StackPanel Orientation="Horizontal" Visibility="{Binding ElementName=root, Path=FromVisibility}"> | |
<TextBlock FontSize="12">From:</TextBlock> | |
<TextBlock FontSize="12" x:Name="txtFrom"></TextBlock> | |
</StackPanel> | |
<TextBlock x:Name="MessageTB" VerticalAlignment="Center" FontSize="14" Foreground="Black" Text="Your text here" TextWrapping="Wrap" /> | |
<TextBlock x:Name="MessagePaused" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Black" Text="Click to pause." TextAlignment="Center" /> | |
</StackPanel> | |
</Grid> | |
</Border> | |
</Popup> | |
</UserControl> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Windows; | |
using System.Windows.Threading; | |
namespace TheHub.Silverlight.Controls | |
{ | |
public partial class ToastNotification | |
{ | |
private readonly DispatcherTimer timer; | |
public ToastNotification(double timespan) | |
{ | |
PauseMessage = ""; | |
TextMsg = ""; | |
InitializeComponent(); | |
timer = new DispatcherTimer(); | |
timer.Interval = TimeSpan.FromSeconds(timespan); | |
timer.Tick += TimerTick; | |
LayoutRoot.Width = 200; | |
LayoutRoot.Height = 200; | |
NotificationPopup.HorizontalOffset = Application.Current.Host.Content.ActualWidth / Application.Current.Host.Content.ZoomFactor - 200; | |
NotificationPopup.VerticalOffset = Application.Current.Host.Content.ActualHeight / Application.Current.Host.Content.ZoomFactor - 200; | |
} | |
public string TextMsg { get; set; } | |
public string PauseMessage { get; set; } | |
public static readonly DependencyProperty FromVisibilityProperty = DependencyProperty.Register( | |
"FromVisibility", typeof(Visibility), typeof(ToastNotification), new PropertyMetadata(Visibility.Visible)); | |
public Visibility FromVisibility | |
{ | |
get { return (Visibility) GetValue(FromVisibilityProperty); } | |
set { SetValue(FromVisibilityProperty, value); } | |
} | |
/// <summary> | |
/// public constructor | |
/// </summary> | |
/// <param name="timespan"></param> | |
/// <returns></returns> | |
public static ToastNotification CreateToast(double timespan) | |
{ | |
return new ToastNotification(timespan); | |
} | |
/// <summary> | |
/// Diaplay the Toast | |
/// </summary> | |
/// <param name="message"></param> | |
public void ShowMessage(string message, string from) | |
{ | |
PauseMessage = "Click to pause."; | |
MessagePaused.Text = PauseMessage; | |
txtFrom.Text = from; | |
TextMsg = message; | |
MessageTB.Text = TextMsg; | |
NotificationPopup.IsOpen = true; | |
if (timer.IsEnabled) | |
{ | |
timer.Stop(); | |
} | |
timer.Start(); | |
VisualStateManager.GoToState(this, "Normal", false); | |
} | |
private void TimerTick(object sender, EventArgs e) | |
{ | |
VisualStateManager.GoToState(this, "Hidden", false); | |
NotificationPopup.IsOpen = false; | |
timer.Stop(); | |
} | |
/// <summary> | |
/// Click to pause or close toast | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
private void LayoutRoot_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e) | |
{ | |
if (timer.IsEnabled) | |
{ | |
timer.Stop(); | |
PauseMessage = "Message held - Click here to close."; | |
MessagePaused.Text = PauseMessage; | |
} | |
else | |
{ | |
VisualStateManager.GoToState(this, "Hidden", false); | |
NotificationPopup.IsOpen = false; | |
PauseMessage = "Click to pause."; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Known issue - if a toast appears and the window is resized, subsequent toast are misplaced.