Toast doesn't display - c#

I'm trying to display a simple toast, it showed the first the I deployed the app but it doesn't show anymore. I didn't change anything to the code between deployments. It's the basic blank project, except in Mainpage this is the only code
public MainPage()
{
this.InitializeComponent();
}
public static void Notification(string title, string content)
{
// Construct the visuals of the toast
ToastVisual visual = new ToastVisual()
{
TitleText = new ToastText()
{
Text = title
},
BodyTextLine1 = new ToastText()
{
Text = content,
},
};
ToastContent toastContent = new ToastContent()
{
Visual = visual,
};
// And create the toast notification
Windows.Data.Xml.Dom.XmlDocument doc = toastContent.GetXml();
var toast = new ToastNotification(toastContent.GetXml());
ToastNotificationManager.CreateToastNotifier().Show(toast);
}
private void buttonShowToast_Tapped(object sender, TappedRoutedEventArgs e)
{
Notification("a", "b");
}
NuGet package installed:
NotificationExtensions.Win10 (version: 14332.0.2)
This is as simple as it could be, why is it not working? Am I missing some sort of permission?

I figured it out. For some reason Windows 10 disabled the notifications for all apps deployed with Visual Studio. Heading over to "Notifications & Actions" in the settings of the device the permissions were toggled off under "Get notifications from these senders".
Toggling the permissions to "On" for the app solved the issue.

Related

How to create informative toast notification in UWP App

In my app, I want to inform user when particular action had performed, like record updated successfully or new record added, but there's not inbuilt control to that can display such information. Is there anything similar to Android Toast.makeText for UWP?
Yes, UWP has Toast Notifications :)
Here is sample code to display simple notification:
private void ShowToastNotification(string title, string stringContent)
{
ToastNotifier ToastNotifier = ToastNotificationManager.CreateToastNotifier();
Windows.Data.Xml.Dom.XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
Windows.Data.Xml.Dom.XmlNodeList toastNodeList = toastXml.GetElementsByTagName("text");
toastNodeList.Item(0).AppendChild(toastXml.CreateTextNode(title));
toastNodeList.Item(1).AppendChild(toastXml.CreateTextNode(stringContent));
Windows.Data.Xml.Dom.IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
Windows.Data.Xml.Dom.XmlElement audio = toastXml.CreateElement("audio");
audio.SetAttribute("src", "ms-winsoundevent:Notification.SMS");
ToastNotification toast = new ToastNotification(toastXml);
toast.ExpirationTime = DateTime.Now.AddSeconds(4);
ToastNotifier.Show(toast);
}
In this article you can find how to customize it:
https://learn.microsoft.com/en-us/windows/uwp/controls-and-patterns/tiles-and-notifications-adaptive-interactive-toasts
# CSharpToast
Create a toast in c#
This is a Microsoft Visual Studio project that demonstrates showing a toast message to the user.
A toast message is one that appears, then after a delay, disappears without user intervention.
This is needed when either the default language implementation is lacking, see java Android, or when there is no default language implementation.
The steps below illustrate the steps I took to create the toast,
but you can, if desired, just download the app here with all of these steps already completed.
Usage:
Call: Toast.show ("This is a test toast.");
DownloadURL:
git clone https://github.com/pstorli/CSharpToast
Steps To Create:
1) Create a new application in MS Visual Studio.
1.1) File -> New -> Application -> Windows Forms App (.NET Framework)
1.2) Name app as desired, here I used CSharpToast
2) Adjust initial form/screen
2.1) Set Form1.cs name to MainWindow.cs
2.4) Set the StartPosition to CenterScreen
2.5) Add a button1 to the form. Set text to "Make Toast"
2.6) Double click button. A new method should appear:
private void button1_Click(object sender, EventArgs e)
2.7) Add this code to it:
Toast.show ("Toast is done!");
3) Create the toast form.
3.1) In the solution explorer, Add -> New Item -> Windows Form
3.1.1) Set the name to Toast.cs
3.1.2) Set the toast form width and height to toast size, say 6 inches wide by 1/2" tall.
3.1.3) Set the FormBorderStyle to None
3.1.4) Set the background color to white.
3.1.5) Set the start position to CenterScreen
3.2) Add a label to your form
3.2.1) Set the name to Message
3.2.2) Set autosize to false.
3.2.3) Set textalign to MiddleCenter.
3.2.4) Set the background color to white.
3.2.5) Set Dock to fill.
3.3) Add some processing logic to file Toast.cs
3.3.1) Change Toast.cs from this:
using System.Windows.Forms;
namespace CSharpToast
{
public partial class Toast : Form {
public Toast()
{
InitializeComponent();
}
}
}
3.3.2) to this:
3.3.3) Added DEFAULT_MS_DELAY to control how long, by default toast shoould show up.
3.3.4) Added delegate void SafeOnTimedEvent to call Close from differenet thread.
3.3.5) Added constructor with just message and one with message and delay, to override DEFAULT_MS_DELAY
3.3.6) Created toast and added a timer to call, void OnTimedEvent() , when toast is done
which calls Toast.close(); on correct thread.
using System;
using System.Timers;
using System.Windows.Forms;
namespace CSharpToast
{
public partial class Toast : Form {
public static int DEFAULT_MS_DELAY = 2500;
private delegate void SafeOnTimedEvent(Object source, ElapsedEventArgs e);
public Toast (String message)
{
InitializeComponent();
Message.Text = message;
}
public static Toast show (String message)
{
return show(message, DEFAULT_MS_DELAY);
}
public static Toast show (String message, int ms)
{
Toast toast = new Toast(message);
System.Timers.Timer aTimer = new System.Timers.Timer(ms);
aTimer.Elapsed += toast.OnTimedEvent;
aTimer.AutoReset = false;
aTimer.Enabled = true;
toast.ShowDialog();
return toast;
}
private void OnTimedEvent (Object source, ElapsedEventArgs e)
{
if (this.InvokeRequired)
{
var d = new SafeOnTimedEvent(OnTimedEvent);
Invoke(d, new object[] { source, e });
}
else
{
Close();
}
}
}
}
3.4) Run app
3.4.1) The main screen with the "Make Toast" button should appear.
3.4.2) When you press the "Make Toast" button,
3.4.3) a white toast popup should appear that says "Toast is Done!"
3.4.4) which should disappear in 2500 ms.
"I've looked at toast from both sides now, the win and lose and still somehow
it's toast's illusions I recall, I really don't like toast in c# at all."
~Except my CSharpToast version.
Here how to realize simple makeText like android:
private Windows.UI.Xaml.Controls.Frame frame;
private Windows.UI.Xaml.Controls.Page page;
private Ribo.Smart.App.UserControls.Components.Common.Toast toast;
private DispatcherTimer timer = new DispatcherTimer();
void timer_Tick(object sender, object e)
{
if (toast != null)
((Panel)page.FindName("layoutRoot")).Children.Remove(toast);
toast = null;
timer.Stop();
timer.Tick -= timer_Tick;
}
private void Frame_Navigated(object sender, Windows.UI.Xaml.Navigation.NavigationEventArgs e)
{
if (toast != null)
{
object layoutRoot = page.FindName("layoutRoot");
if (layoutRoot != null)
{
((Panel)layoutRoot).Children.Remove(toast);
page = (Windows.UI.Xaml.Controls.Page)e.Content;
layoutRoot = page.FindName("layoutRoot");
((Panel)layoutRoot).VerticalAlignment = VerticalAlignment.Stretch;
((Panel)layoutRoot).Children.Add(toast);
if (layoutRoot is Grid)
{
toast.SetValue(Grid.RowSpanProperty, 100);
}
}
}
}
public void ShowMessage(string message)
{
frame = (Windows.UI.Xaml.Controls.Frame)Windows.UI.Xaml.Window.Current.Content;
page = (Windows.UI.Xaml.Controls.Page)frame.Content;
frame.Navigated -= Frame_Navigated;
frame.Navigated += Frame_Navigated;
toast = new Ribo.Smart.App.UserControls.Components.Common.Toast();
toast.Message = message;
toast.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Bottom;
toast.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center;
int seconds = message.Length / 30;
if (seconds < 2)
seconds = 2;
timer.Interval = new TimeSpan(0, 0, seconds);
timer.Start();
timer.Tick -= timer_Tick;
timer.Tick += timer_Tick;
object layoutRoot = page.FindName("layoutRoot");
if (layoutRoot != null)
{
if (layoutRoot is Grid)
{
toast.SetValue(Grid.RowSpanProperty, 100);
}
((Panel)layoutRoot).Children.Add(toast);
}
}

Open a WPF window app form VS extension command

I'm writing my first VS extension.
so far i have the code to get selected text and display a message box or manipulate the selection:
the CTOR of the extention..
private StringRefactor(Package package)
{
if (package == null)
{
throw new ArgumentNullException("package");
}
this.package = package;
OleMenuCommandService commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (commandService != null)
{
var menuCommandID = new CommandID(CommandSet, CommandId);
var menuItem = new MenuCommand(this.MenuItemCallback, menuCommandID);
commandService.AddCommand(menuItem);
}
}
The callback:
private void MenuItemCallback(object sender, EventArgs e)
{
var selection = getSelection();
var selectedText = selection == null ? "No text selected..." : selection.StreamSelectionSpan.GetText();
string message = string.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", this.GetType().FullName);
string title = "StringRefactor";
// Show a message box to prove we were here
VsShellUtilities.ShowMessageBox(
this.ServiceProvider,
selectedText,
title,
OLEMSGICON.OLEMSGICON_INFO,
OLEMSGBUTTON.OLEMSGBUTTON_OK,
OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
}
now instead of VsShellUtilities.ShowMessageBox(... i would like to open a prompt window that display several textboxes and ok\cancel button..
I thought of creating another WPF app project and launching it from the callback but I'm not sure this is the right way to write an extension that opens a custom tool ..
so what is the right way to open a custom window with functionality from a VISIX ?
You can create your own WPF dialogs in the VSIX extension. In fact Visual Studio is designed for that (since the UI is WPF).
See this article for further instructions:
Creating and Managing Modal Dialog Boxes

How get navigation control from a button?

I know if I used a view controller I can use this:
var scanner = new MobileBarcodeScanner(this.NavigationController);
How do I know what navigation I am using inside of button I need use?
public class BarReaderButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
// I tried this but don't worked
var scanner = new MobileBarcodeScanner(this.NavigationController);
// I tried this but do
var scanner = new MobileBarcodeScanner(this);
Element.Clicked += async(s_, e_) => {
// Setup our button
// Tell our scanner to use the default overlay
scanner.UseCustomOverlay = false;
//We can customize the top and bottom text of the default overlay
scanner.TopText = "Hold camera up to barcode to scan";
scanner.BottomText = "Barcode will automatically scan";
//Start scanning
var result = await scanner.Scan ();
HandleScanResult(result);
};
}
}
I can't use this code inside a button render. Or did someone this before?
The project is a shared application for iOS and Android.
The code for creating the scanner should stay inside the view controller/activity/page. You can still use the custom button and add the code you need to the Clicked event handler.
var myCustomButton = new BarReaderButton();
myCustomButton.Clicked += async(s, e) => {
var scanner = new MobileBarcodeScanner();
scanner.UseCustomOverlay = false;
//Start scanning
var result = await scanner.Scan ();
//Do something with the result
};
If this is Xamarin.Forms you will also have to use platform specific code inside the Page code as the barcode reader requires a Context on Android:
#if __IOS__
var scanner = new MobileBarcodeScanner();
#elif __ANDROID__
var scanner = new MobileBarcodeScanner(Forms.Context);
#endif

Windows Phone Toast Notification Not Shown [duplicate]

In my application I want to notify the user with the ShellToast.
Just by running...
var toast = new ShellToast
{
Title = "Nom nom nom!",
Content = "More! More! Keep feeding me!",
};
toast.Show();
...makes nothing happen, and as I understand it needs to be run from a ScheduledTaskAgent. But how do I run this on command, and make sure it only run once?
You can't use a ShellToast while the app is the foreground app. It's meant to be invoked from a background service while the app isn't the foreground app.
If you want to have a UX similar to that of ShellToast use the Coding4fun toolkit ToastPrompt control. Here's a code snippet showing how to use it:
private void ToastWrapWithImgAndTitleClick(object sender, RoutedEventArgs e)
{
var toast = GetToastWithImgAndTitle();
toast.TextWrapping = TextWrapping.Wrap;
toast.Show();
}
private static ToastPrompt GetToastWithImgAndTitle()
{
return new ToastPrompt
{
Title = "With Image",
TextOrientation = System.Windows.Controls.Orientation.Vertical,
Message = LongText,
ImageSource = new BitmapImage(new Uri("../../ApplicationIcon.png", UriKind.RelativeOrAbsolute))
};
}
Running this code snippet shows the following:
Just a small update: using ShellToast when the app is in foreground, is now possible, when using Windows Phone 8 Update 3. Though, they are obscured by other activity such as a phone call or the lock screen. Source

How can I create a shelltoast?

In my application I want to notify the user with the ShellToast.
Just by running...
var toast = new ShellToast
{
Title = "Nom nom nom!",
Content = "More! More! Keep feeding me!",
};
toast.Show();
...makes nothing happen, and as I understand it needs to be run from a ScheduledTaskAgent. But how do I run this on command, and make sure it only run once?
You can't use a ShellToast while the app is the foreground app. It's meant to be invoked from a background service while the app isn't the foreground app.
If you want to have a UX similar to that of ShellToast use the Coding4fun toolkit ToastPrompt control. Here's a code snippet showing how to use it:
private void ToastWrapWithImgAndTitleClick(object sender, RoutedEventArgs e)
{
var toast = GetToastWithImgAndTitle();
toast.TextWrapping = TextWrapping.Wrap;
toast.Show();
}
private static ToastPrompt GetToastWithImgAndTitle()
{
return new ToastPrompt
{
Title = "With Image",
TextOrientation = System.Windows.Controls.Orientation.Vertical,
Message = LongText,
ImageSource = new BitmapImage(new Uri("../../ApplicationIcon.png", UriKind.RelativeOrAbsolute))
};
}
Running this code snippet shows the following:
Just a small update: using ShellToast when the app is in foreground, is now possible, when using Windows Phone 8 Update 3. Though, they are obscured by other activity such as a phone call or the lock screen. Source

Categories

Resources