question

MarcJeeves-9615 avatar image
0 Votes"
MarcJeeves-9615 asked EmonHaque-1485 answered

pack://application relative path definition for Toast notifications

I'm playing around with the notification toast builder

i can get my image to show if i use the full path on the hard drive but i want to put the image in an Images folder, i have the image set to copyifnewer but i cant get the image to show using the Pack://Application

can anybody show me what I'm doing wrong?

thanks

 new ToastContentBuilder()
                 .AddHeroImage(new Uri("pack://application:,,,/Images/Nikola.png"))
                   
                 .AddText("My Title")
                 .AddText("My Description")
                                       
             .Show();
windows-wpf
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

do you need those pack? If you set Build Action to Resource you could do this: img.Source = new BitmapImage(new Uri("Images/Nikola.png", UriKind.Relative));

0 Votes 0 ·

using the Microsoft.Toolkit.Uwp.Notifications does not support 'UriKind.Relative' any other ideas

Thanks

Madaxe

using Microsoft.Toolkit.Uwp.Notifications;

 using System;
 using System.Windows;
    
 namespace WPFSampleApp
 {
     public partial class MainWindow : Window
     {
         public MainWindow()
         {
             InitializeComponent();
    
             new ToastContentBuilder()
                 .AddHeroImage(new Uri("Images/Nikola.png"), UriKind.Relative)
                 .AddText("My Title")
                 .AddText("My Description")
                 .AddCustomTimeStamp(new DateTime(2017, 04, 15, 19, 45, 00, DateTimeKind.Utc))
    
             .Show();
         }
     }
 }

0 Votes 0 ·

No idea about UWP toolkit. For android like Toast you could use Popup and place it wherever you want.

0 Votes 0 ·

1 Answer

EmonHaque-1485 avatar image
0 Votes"
EmonHaque-1485 answered

Here's a sample using Popup:

 class Toast : Popup
 {
     Image image;
     TextBlock title, message;    
     public string Title { get; set; }
     public string Message { get; set; }
     public string Image { get; set; }
     public Toast() {
         StaysOpen = false;
         AllowsTransparency = true;
         PlacementTarget = App.Current.MainWindow;
         Placement = PlacementMode.Center;
         VerticalOffset = App.Current.MainWindow.ActualHeight / 4;
         PopupAnimation = PopupAnimation.Slide;
         image = new Image() { 
             Stretch = Stretch.Uniform,
             Width = 64,
             Height = 64,
             Margin = new Thickness(0,0,5,0)
         };
         title = new TextBlock() { 
             FontWeight = FontWeights.Black,
             FontSize = 14,
             VerticalAlignment = VerticalAlignment.Bottom
         };
         message = new TextBlock() { VerticalAlignment = VerticalAlignment.Top };
         var separator = new Separator() { Background = Brushes.Coral };
         Grid.SetRowSpan(image, 3);
         Grid.SetColumn(title, 1);
         Grid.SetColumn(message, 1);
         Grid.SetColumn(separator, 1);
         Grid.SetRow(separator, 1);
         Grid.SetRow(message, 2);
         var grid = new Grid() {
             RowDefinitions = {
                 new RowDefinition(),
                 new RowDefinition(){ Height = GridLength.Auto },
                 new RowDefinition()
             },
             ColumnDefinitions = {
                 new ColumnDefinition(){ Width = GridLength.Auto },
                 new ColumnDefinition()
             },
             Children = { image, title, separator, message}
         };
         Child = new Border() { 
             Background = Brushes.LightSkyBlue,
             CornerRadius = new CornerRadius(10),
             Padding = new Thickness(3),
             Margin = new Thickness(5),
             Effect = new DropShadowEffect() { BlurRadius = 10, ShadowDepth = 0 },
             Child = grid
         };
     }
     public void Show() { 
         image.Source = new BitmapImage(new Uri(Image, UriKind.Relative));
         title.Text = Title;
         message.Text = Message;
         IsOpen = true;
     }
 }

and in the click handler of the button, it's been used in this way:

 void Button_Click(object sender, RoutedEventArgs e) {
     var toast = new Toast() {
         Title = "Some Title",
         Message = "Some Message",
         Image = "Images/Capture.PNG"
     };
     toast.Show();
 }

and here's how it looks:

109666-test.gif


test.gif (106.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.