I have a working program which opens Dialog Boxes(WPF) for User input .
Now i want to Refactor and use Toast Notifications. The Notifications need to have the ability of Buttons (Pressing yes or no). The new Win10 interactive Toast Notification seemd perfect.
I got the official tutorial from Windows.
https://learn.microsoft.com/en-us/windows/uwp/controls-and-patterns/tiles-and-notifications-send-local-toast
Displaying the Toasts works fine but.... The Programm is a in Visual Studio WPF App(.NET Framework) written in C#.
It won't allow me to override methods in my App.xaml.cs i need like:
protected override void OnActivated(IActivatedEventArgs e)
protected override async void OnBackgroundActivated(BackgroundActivatedEventArgs args)
"There is no suitable Method for override"
Simply because there is none ...i think?!
As soon as i tried with a new Project "Blank App univeral Windows" exept from "WPF App .NET Framework" overriting is no problem. Also new created WPF Apps won't let me override.
So my question. What can i do to still use interactive Toasts with my WPF Project? Or is there a alternative way of doing it?
Related
Question:
Why does my unpackaged C# application show its icon when I launch it but not when my Windows service launches it? How can I make my app's toasts always show the app's icon?
Details:
I have a C++ Windows Service that launches a C# Win32 application for toast functionality, since toasts cannot be launched directly from a service to a user. It is an absolute requirement that the service launches the toast app. To my frustration, however, the app's icon (i.e. the icon shown on the .exe in Explorer) refuses to show only when launched by my service. Here is an example of what I see when my service launches the app (Note the three squares. This is the Windows 10 default icon):
When I manually launch the app (i.e. click it), this is what I see instead:
The only difference between the above two screenshots is the launch method. The most succinct way I can describe my issue is that I want the launch method (launched from a service) that yields the first screenshot to yield the second screenshot instead.
I can provide the code snippet I used to generate these toasts, although I doubt its usefulness for finding a solution:
var notifier = ToastNotificationManagerCompat.CreateToastNotifier();
var xml = new Windows.Data.Xml.Dom.XmlDocument();
xml.LoadXml("<toast><visual><binding template=\"ToastGeneric\"><text>Foo</text<text>Bar</text></binding></visual></toast>");
var notif = new Windows.UI.Notifications.ToastNotification(xml);
notifier.Show(notif);
The most useful code sample I believe I can provide is the code that the service uses to launch the app:
void SpawnToastApp()
{
constexpr int nProcFlags = DETACHED_PROCESS | NORMAL_PRIORITY_CLASS;
constexpr wchar_t* wcsDesktop = L"WinSta0\\Default";
constexpr wchar_t* wcsToastApp = L"ToastApp\\Toast App (WIP).exe";
HANDLE hUser = NULL;
STARTUPINFOW si{ 0 };
wchar_t wcsCmdLine[MAX_PATH]{ 0 };
_snwprintf_s(wcsCmdLine, _TRUNCATE, L"\"%S\\%s\" %lu", _strInstallDir, wcsToastApp, GetCurrentProcessId());
_sessionCanToast = WTSQueryUserToken(_sessionId, &hUser);
if (_sessionCanToast)
{
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.lpDesktop = wcsDesktop;
_sessionCanToast = CreateProcessAsUserW(hUser, NULL, wcsCmdLine, NULL, NULL, FALSE,
nProcFlags, NULL, NULL,
&si, &_toastHandlerProcessInformation);
}
if(!_sessionCanToast)
{
/// Log it
}
if (hUser) { CloseHandle(hUser); };
}
I include the C++ code because I believe that I have narrowed the problem down to the launch method but am unsure the specific cause beyond that.
Additional Information:
These screenshots utilize Windows.UI.Notifications.ToastNotifications created from raw XML, but I have also tried using the Microsoft.Toolkit.Uwp.Notifications NuGet Package as recommended by Microsoft to the same effect.
I believe this project is a Windows Form App.
I am not using any sort of package--no APPX, MSIX, or sparse package. This is meant to be a lightweight app whose sole function is for toasts. While using a package isn't out of the question, suffice it to say that the number of hurdles and implementation issues make packaging this app undesirable. Indeed, the only reason I would want to package this app is for the icon in the upper left-hand corner of it, which it evidently does already, just not in the way I desire.
Similar to but NOT a duplicate of:
Change toast notification icon in wpf
I have already done this. My issue pertains to the icon's inconsistency rather than the lack of it entirely.
Why is app icon missing for toast notifications in action center on desktop?
I am using a Release build of my app
Cannot override notification app logo for Windows 10/11 VSTO app
Using AppOverrideLogo gets my icon to show under all circumstances, but it's more like a picture in the body of the toast rather than the small icon in the upper left-hand corner of the toast. Essentially, it's not the style I want.
EDIT 1:
I followed a sparse packaging guide found here to more or less the same result, the main difference being that now no icon not shows up at all anywhere. I used the asset generator in Visual Studio and then used the MSIX unpackaging tool to inspect the contents of the sparse package and confirmed it contained the generated assets. I had to comment out the reference to the splash screen because the app failed to register with that line included in the manifest.
EDIT 2:
I have decided to proceed with this app as if I am not having this issue, and so I used Visual Studio's Performance Profiler to analyze my app's resources. The Performance Profiler launched my toast app, and the toasts had the correct icon, so at this point I am 100% certain it has something to do with my service's launch method. Unfortunately, I am no closer to understanding why the icon does not show only when launched from my service.
I am trying to build an unpackaged WPF app with Windows 10 Toast Notifications. I would like to make an AppLogoOverride to get a resource from the Resources class of my application. I have tried to do this so far:
public void ShowToast() {
var toast = new ToastContentBuilder()
.AddText("Foo!")
.AddText("Bar.")
.AddAppLogoOverride(new Uri("pack://application:,,,/Resources/foobar.png"), ToastGenericAppLogoCrop.Circle)
.SetToastScenario(ToastScenario).Reminder);
ToastNoticiationManagerCompat.CreateToastNotifier().Show(toast);
}
When doing so, the notification just says the generic "New Notification" text. So I tried commenting out the part that says AddAppLogoOverride, and it started to work. So I am not sure how to get the image from Resources.
I would also like to mention that there is no way for me to use something like ms-appdata:// or others, I am not sure how to go about doing this, and I really don't want to write the file locally to the disk.
I have not succeeded to include an image into a toast using pack://application:,,, syntax so far. I guess this syntax is not supported by toast notification because it is a part of WinRT and the syntax of UWP to refer resources is different from that of WPF.
The workaround is, you might dislike it, to copy the image in local (in the installation folder of the app or somewhere else) and specify its absolute path. See How can I make a notification in a C# Windows app with a custom image and onclick function?
I have two questions, first: how to let the uwp application perform a function, automatically close the uwp through the code instead of letting the user click the close button on the top right; second: how to minimize the uwp application to the taskbar at startup, not directly on the desktop, thank you.
You can use the static exit method:
public void CloseApp()
{
CoreApplication.Exit();
}
or Use the non-static method ():
public void CloseApp()
{
Application.Current.Exit();
}
One thing you have to keep in mind is that using CoreApplication.Exit and Application.Current.Exit closes the app, but it does so without going through the normal app suspension - for the system it appears as a "unexpected termination". I also think the certification guidelines are against this approach.
As for taskbar minimization - this is currently not supported. If you need this functionality, you will need to build a WPF app packaged as UWP app using the Desktop Bridge.
I want to write my own program that lets me select one Window of ALL open windows and set the state of this window to TOP, so that this selected window will always show on top!
The problem... since Windows 8 there are APPs and actually the process comes up in the process explorer but my selection of my tool doesn't list it. (Its like there is no app)
My source looks like:
private void refreshWindowList(object sender, EventArgs e)
{
windowList.Items.Clear();
foreach (Process p in Process.GetProcesses().Where(pp => pp.MainWindowHandle != IntPtr.Zero && pp.ProcessName != "explorer"))
{
windowList.Items.Add(p.ProcessName);
}
}
This function is called when I open my combobox and actually refresh the Items every time when I view the list.
I find normal programs, but is there a way to find Win 8/10 apps ?
EDIT to clarify: Normal processes like notepad can be found. But Windows Universal apps like e.g. netflix can't. At least I don't know how to find them.
Some applications, e.g. Netflix, are written using HTML and JavaScript. These apps are hosted by WWAHost:
[...] Microsoft explains WWAHost as “an Internet Explorer-based rendering platform.”
You can check if this is the case for an app by right clicking it in the Task Manager and choosing Go to details:
To find out which app is being hosted, you can use MainWindowTitle
Console.WriteLine(p.ProcessName); // WWAHost
Console.WriteLine(p.MainWindowTitle); // Netflix
I am working on a simple program in Visual studio(windows application) and I need to know when the user is changing the keyboard language(I don't care what was the language or what it is now).
I have tried this code:
private void MainForm_InputLanguageChanged(object sender, InputLanguageChangedEventArgs e)
{
}
But it will only get the language change in the form when the form is active and focused! Example: I'll open Google chrome and change the language, this function will not be called.
I need to get the specific windows keyboard changed. Not the form inputlanguage changed..
it's very important for me I am breaking my head about a week for this so please help.
ty guys.
As far s I know, in Windows, input language is specific for process. So you get the InputLanguageChanged event for your application. If you want to be notified of language change globally, you should do it on your own. For example periodically check the active window and get keyboard layout using GetKeyboardLayout API function.