I'm developing a program in C# (Visual Studio 2015) and I want to show a toast message to the user at a certain situation. I downloaded this code from the MSDN and it runs fine:
// Get a toast XML template
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04);
// Fill in the text elements
XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
for (int i = 0; i < stringElements.Length; i++)
{
stringElements[i].AppendChild(toastXml.CreateTextNode("Line " + i));
}
// Specify the absolute path to an image
String imagePath = "file:///" + Path.GetFullPath("toastImageAndText.png");
XmlNodeList imageElements = toastXml.GetElementsByTagName("image");
imageElements[0].Attributes.GetNamedItem("src").NodeValue = imagePath;
// Create the toast and attach event listeners
ToastNotification toast = new ToastNotification(toastXml);
toast.Activated += ToastActivated;
toast.Dismissed += ToastDismissed;
toast.Failed += ToastFailed;
// Show the toast. Be sure to specify the AppUserModelId on your application's shortcut!
ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast);
After testing this code I wanted to implement it into my application. So I changed it up a little bit and tried to run it. The error messages:
The type "IReadOnlyList<>" is defined in a not referenced assembly. Add a reference to System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
(translated)
Same goes for IEnumerable<> and IReadOnlyList<>
The error come from these two lines:
for (int i = 0; i < stringElements.Length; i++)
{
stringElements[i].AppendChild(toastXml.CreateTextNode("Line " + i));
I also tried adding the reference to System.Runtime. I downloaded it with NuGet (https://www.nuget.org/packages/System.Runtime/4.0.0/).
After that the errors were gone, but now literaly every word in my code is cringled red with error like "System.Object is not defined" and so on (but it still runs when I start it!).
The only possible solution I can think of is that System.Runtime is already installed somewhere on my computer, and that 4.0.0 is the wrong version for my program. But I can't find it anywhere.
PS: It's a desktop-application, not a Windows-Store application.
I think it is the same problem as in this question
You must add a reference to
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\Facades\System.Runtime.dll
PS : If you have a Windows 10 only desktop app, you might want to use the new toast system, the code sample on MSDN uses the Windows 8 one. it works on W10 but does not have all the new features (Microsoft released an official NuGet package).
Edit : Since I can't comment, I will post the answer here :
The exception is because you need to provide an applicationId in CreateToastNotifier()
ToastNotificationManager.CreateToastNotifier("MyApplicationId").Show(toast);
It is the name that will be used in the action center to group your toasts (so in general, you put the name of your app). In Windows 8.1 it was needed to register your application Id (I think this was in the sample from the MSDN) but now you can just put the name of your app.
And the GetXml() is only for WinRT. In desktop you need to do like you did with the GetContent().
Related
I've searched through a number of different posts to do with creating toast notifications from a Win Form however when these through I get an error when generating the toast notification.
System.Exception: Element not found. (Exception from
HRESULT:0x80070490).
I have edited the csproj file and added the following:
<PropertyGroup>
<TargetPlatformVersion>10.0.10586</TargetPlatformVersion>
</PropertyGroup>
and added the references to Windows.Data and Windows.UI and also a reference to System.Runtime.dll as per the suggestions in Windows.UI.Notifications is missing
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
using System.Windows.Forms;
using System;
namespace ToastNotify
{
class Notify
{
public void GenerateToast(string header, string content)
{
ToastTemplateType toastTemplate = ToastTemplateType.ToastImageAndText02;
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
toastTextElements[0].AppendChild(toastXml.CreateTextNode(header));
toastTextElements[1].AppendChild(toastXml.CreateTextNode(content));
XmlNodeList toastImageElements = toastXml.GetElementsByTagName("image");
((XmlElement)toastImageElements[0]).SetAttribute("src", "..\\..\\Resources\\icon.ico");
IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
((XmlElement)toastNode).SetAttribute("duration", "long");
ToastNotification toast = new ToastNotification(toastXml);
try
{
ToastNotificationManager.CreateToastNotifier().Show(toast);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
Any suggestions as to where I am going wrong?
You should explicitly provide applicationId for CreateToastNotifier.
Like this:
private const String APP_ID = "Microsoft.Samples.DesktopToastsSample";
...
ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast);
But I have bad news. Starting from Windows 10 1709 WinForms applications just does not show toast notifications. Before that Show(toast) was working, but now it neither throw an exception, nor show any toast notification.
I'm still figuring this out.
As noted by Prateek Shrivastava there are (new) limitations.
Have a look here https://learn.microsoft.com/en-us/uwp/api/windows.ui.notifications.toastnotificationmanager.createtoastnotifier
Update:
Here is the step by step guide, to create a setup with APP_ID so notifications will work on all Windows 10 versions:
Send a local toast notification from desktop C# apps
Update:
It works again in Windows 10 1903 without setup.
Use This and make sure you are setting the Image (Icon) full path if you want to show an icon otherwise just pass null.
public static void GenerateToast(string appid, string imageFullPath, string h1, string h2, string p1)
{
var template = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04);
var textNodes = template.GetElementsByTagName("text");
textNodes[0].AppendChild(template.CreateTextNode(h1));
textNodes[1].AppendChild(template.CreateTextNode(h2));
textNodes[2].AppendChild(template.CreateTextNode(p1));
if (File.Exists(imageFullPath))
{
XmlNodeList toastImageElements = template.GetElementsByTagName("image");
((XmlElement)toastImageElements[0]).SetAttribute("src", imageFullPath);
}
IXmlNode toastNode = template.SelectSingleNode("/toast");
((XmlElement)toastNode).SetAttribute("duration", "long");
var notifier = ToastNotificationManager.CreateToastNotifier(appid);
var notification = new ToastNotification(template);
notifier.Show(notification);
}
I need to chime in here, because I'm working on a WinForms app (.Net Framework 4.7*) that's failing to build on a GitHub build worker because at some point, support for toast notifications was hacked in with some weird references to these WinMetadata\Windows.*.winmd files.
Woe be to anyone who has found this StackOverflow question.
You're probably targeting the .Net Framework 4.x because you're working on a Win Forms app. .Net Framework 4.8 is supported on Windows 7, and Windows 7 does not have ToastNotifications which is probably the class you're looking for. Hence the problem.
I think the only solution is the one in the updated answer that's most upvoted, which is just target the more modern UWP framework that will only work on Windows 10 and up. But you're still trying to send toast notifications from a framework that's not designed to support it. At some point in your code, you're probably doing a check if the platform you're running on is Windows 10 or newer. Not even a compiler statement, just straight up C# or VB.Net. That's not how the .Net Framework is designed to work - it's meant to be platform-agnostic.
My only advice is; setup a new build target for Windows 10+ and setup your project following the advice from Stephen. That should be the least-hackish way of making WinForms do neat Windows 10 tricks.
I am trying to add a MapIcon to my Bing maps control. When I am running the application in my debugging environment on visual studio I don't have any issues. However once I build my app package and run it, adding a map Icon crashes the application and throws this an exception that reads:
"Unable to cast object of type Windows.UI.Xaml.Controls.Maps.MapIcon to type Windows.UI.Xaml.Controls.Maps.IMapElement4"
Here is the simple code that instantiates the MapIcon. It is important to know that I do not have any issues adding these map Icons in debug mode. The problem only exists once the app package has been built and run. I was able to pinpoint the exception using a try/catch to display the exception while the released app is running. If anyone can help me to get rid of this exception, or have any advice for a work around, it would be much appreciated
MapIcon messageicon2_2 = new MapIcon
{
Location = message_position,
NormalizedAnchorPoint = new Point(0.5, 1.0),
ZIndex = 0,
Title = "msg " + count + "(2.2)",
IsEnabled = true,
CollisionBehaviorDesired = MapElementCollisionBehavior.RemainVisible
};
MyWaypoints.Add(messageicon2_2);
messageLayer.MapElements = MyWaypoints;
[EDIT To Clarify]: I know that this bug is coming specifically from the instantiation of the Mapicon. This code works perfectly fine when it is released and run on a Windows 10 17134 machine.... The machine that I need it to run is Windows 10 16299. This machine can not be updated to a newer version of windows. I am in need of a way to display these map icons on this older release of windows.
The exception points to the IsEnabled property which was added to MapElement for Windows OS Version 1803 (17134). See MapElement.IsEnabled Property.
I know the exception is not the most helpful, but maybe there was some other warning about it that got missed?
For errors like this, you can either remove all uses of the missing API and/or make sure to write version adaptive code that can run on the lowest OS version, while also taking advantage of the selected features that are available only on certain OS versions.
Specifically you can do something like:
var messageicon2_2= new MapIcon
{
Location = message_position,
NormalizedAnchorPoint = new Point(0.5, 1.0),
ZIndex = 0,
Title = "msg " + count + "(2.2)",
CollisionBehaviorDesired = MapElementCollisionBehavior.RemainVisible
};
if (ApiInformation.IsPropertyPresent("Windows.UI.Xaml.Controls.Maps.MapElement", "IsEnabled"))
{
messageicon2_2.IsEnabled = true;
}
MyWaypoints.Add(messageicon2_2);
messageLayer.MapElements = MyWaypoints;
I want to open a file's location and select the file in explorer on Mac, Ubuntu from MonoDevelop.
This code is working on Windows (but it is not working on Mac and Ubuntu):
System.Diagnostics.Process.Start("explorer.exe", "/select, " + fileaddress);
Dim dir_path As String = "/media/os/test"
' Windows path example: dir_path = "C:\test"
Process.Start("file://" & dir_path)
Tested and worked on Ubuntu and Windows XP.
Source: http://www.stevenbrown.ca/blog/archives/156
By 2020-10, in mono 6.10, the above method didn't work on Ubuntu 20.04. The below approach solved the problem.
System.Diagnostics.Process.Start("mimeopen", "/var/tmp");
You can use 'open' on Mac, like this
System.Diagnostics.Process.Start("open", $"-R \"{File_Path_You_Wanna_Select}\"");
Here -R means reveal, to select in the Finder instead of opening.
To find more usage for open, just type open in terminal.
Using Process.Start() you bypass the .NET framework and move into the platform you're running onto, executing an arbitrary process.
On Windows you want to open the Windows Explorer, on Mac you want to open Finder and on Ubuntu it's simply called File Browser.
There is no Environment.OpenFileBrowser(string path) method in the framework, so you will have to let your program determine which platform it is running on, and open the approperiate file viewer.
See How to check the OS version at runtime e.g. windows or linux without using a conditional compilation statement to perform the former.
You are calling an OS specific (Windows) method. That won't work cross-platform.
Try the following inside a function/method:
Example - inside click event:
protected void OnOpen (object sender, EventArgs e)
{
using(FileChooserDialog chooser =
new FileChooserDialog(null,
"Select document to open...",
null,
FileChooserAction.Open,
"Open Selected File",
ResponseType.Accept,
"Discard & Return to Main Page",
ResponseType.Cancel))
{
if (chooser.Run () == (int)ResponseType.Accept)
{
System.IO.StreamReader file = System.IO.File.OpenText (chooser.Filename);
/* Copy the contents to editableTxtView <- This is the Widget Name */
editableTxtView.Buffer.Text = file.ReadToEnd ();
/* If you want to read the file into explorer, thunar, Notepad, etc.,
* you'll have to research that yourself. */
//Close file - - KEEP IT CLEAN - - & deAllocated memory!!
file.Close ();
}
}
}
The file has now been copied into an editable (Default) or read only (set in properties pad) textviewer Gtk widget. From there you should be able to manipulate it as you so choose.
I'm using White for UI automation and it was working fine until I met an app that was created in Java. In that case I'm unable to find any windows. If I get all Windows from the desktop then I can see the window's name I'm looking for:
List<White.Core.UIItems.WindowItems.Window> windows = new List<White.Core.UIItems.WindowItems.Window>();
windows = White.Core.Desktop.Instance.Windows();
int a = 0;
for (int i = 0; i < windows.Count; i++)
if (windows[i].Name == "HP Service Manager Client") a = i;
This is working, but if I attach the running process and try to get its windows, then I get nothing, the list will be empty:
windows=app.GetWindows();
This is the code that should find the main window:
var processes = Process.GetProcessesByName("ServiceManager");
White.Core.Application app = White.Core.Application.Attach(processes[0]);
White.Core.UIItems.WindowItems.Window main = app.GetWindow(SearchCriteria.ByText("HP Service Manager Client"), InitializeOption.NoCache);
The error I get: Additional information: Couldn't find window with SearchCriteria Name=HP Service Manager Client in process 7396, after waiting for 5000 ms
I can see the Window in VisualUIAVerify and I know that the name of it is correct.
This is the only app which is not working, IE, Firefox, etc. are all working fine, I can always identify the main window, but this time I can't find any windows at all.
I wonder if someone could explain me why it is doing this and help me to resolve this issue (or find a workaround). Many thanks in advance.
Scenario:
Start MonoDevelop
new Android Application
Replace button click delegate with
string fullPath = "/data/misc.mvvmcross.customermanagement/files/_Caches/Pictures.MvvmCross/";
if (System.IO.Directory.Exists(fullPath))
{
button.Text = "exists";
}
else
{
button.Text = "not found";
Directory.CreateDirectory(fullPath);
}
run and click the button.
Directory.CreateDirectory will fail with
System.UnauthorizedAccessException
Have tried creating a new emulator image with different API levels, but problem is still here
Any thoughts anyone?
Ok, Have created new Android Emulator image and used that.
Everything now works.
Solution:
Create a new android emulator image for the API level you want (don't forget Google API support)
I'm wondering if this is caused by some kind of assembly/package naming issue.
I've tested this code:
string fullPath = Path.Combine(FilesDir.Path, "_Caches2/Pictures.MvvmCross/2/");
if (System.IO.Directory.Exists(fullPath))
{
button.Text = "exists";
}
else
{
button.Text = fullPath;
Directory.CreateDirectory(fullPath);
}
...and it works fine in a 2.3.3 emulator.
The data folder pattern is: /data/ * package name * /files/
And it is correct (I think) for one package not to be able to access the data of another.
So I'm wondering if somehow your package names are wrong - check the manifest tab and the manifest.xml file for your application?