C
C#4mo ago
superloubou

Element not found error while trying to use UserNotficationListener

In a .net console application
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Windows.UI.Notifications.Management;
using Windows.UI.Notifications;
using Windows.Foundation.Metadata;

namespace winrttest5
{
internal class Program
{

static private void Listener_NotificationChanged(UserNotificationListener sender, UserNotificationChangedEventArgs args)
{
Console.WriteLine("test");
}
static void Main(string[] args)
{

UserNotificationListener listener = UserNotificationListener.Current;

if (!(ApiInformation.IsTypePresent("Windows.UI.Notifications.Management.UserNotificationListener")))
{
Console.WriteLine("Listener not supported");
}

Console.WriteLine(listener.GetAccessStatus());

Console.ReadKey();

listener.NotificationChanged += Listener_NotificationChanged;

}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Windows.UI.Notifications.Management;
using Windows.UI.Notifications;
using Windows.Foundation.Metadata;

namespace winrttest5
{
internal class Program
{

static private void Listener_NotificationChanged(UserNotificationListener sender, UserNotificationChangedEventArgs args)
{
Console.WriteLine("test");
}
static void Main(string[] args)
{

UserNotificationListener listener = UserNotificationListener.Current;

if (!(ApiInformation.IsTypePresent("Windows.UI.Notifications.Management.UserNotificationListener")))
{
Console.WriteLine("Listener not supported");
}

Console.WriteLine(listener.GetAccessStatus());

Console.ReadKey();

listener.NotificationChanged += Listener_NotificationChanged;

}
}
}
No description
12 Replies
superloubou
superloubou4mo ago
error happens on the listener.NotificationChanged line
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX4mo ago
If you have no further questions, please use /close to mark the forum thread as answered
superloubou
superloubou4mo ago
ive read every relevant stack overflow post on this specific issue and the only one that got me anywhere was running it from within an msix package app but i dont understand why i have to do this to access the api? ive seen other people online access the winrt api straight from a .net console app or even a python app.
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
superloubou
superloubou4mo ago
@TeBeCo sorry to revive the thread but im not trying to use this in a gui. ive managed to get this to work by using a windows packaging project but i still dont understand why its not possible to work in a console app the windows.ui.notifcations.management api isnt listed on the list of apis that only work in packaging projects
superloubou
superloubou4mo ago
Windows Runtime APIs not supported in desktop apps - Windows apps
This article describes WinRT APIs that aren't supported for use in desktop apps, or that have restrictions.
superloubou
superloubou4mo ago
according to this
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
superloubou
superloubou4mo ago
I dont really understand this since windows.ui.notifications.management is part of the windows 10 sdk. UserNotificationListener.GetNotificationsAsync works outside of a packaging project but UserNotificationListener.GetNotification doesnt also it would help me if you could point me to which gui package specifically you are referring to
using Windows.UI.Notifications.Management;
using Windows.UI.Notifications;
using Windows.Foundation.Metadata;



var listener = UserNotificationListener.Current;

if (!ApiInformation.IsTypePresent("Windows.UI.Notifications.Management.UserNotificationListener"))
{
Console.WriteLine("API can't be found");
}

var access_status = listener.GetAccessStatus();

if (access_status == UserNotificationListenerAccessStatus.Allowed)
{
Console.WriteLine("Allowed");
}
else
{
Console.WriteLine("Not allowed");
}

var notifications = await listener.GetNotificationsAsync(NotificationKinds.Toast);

Console.WriteLine(notifications.Count());

try
{
listener.NotificationChanged += Listener_NotificationChanged;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}

void Listener_NotificationChanged(UserNotificationListener sender, UserNotificationChangedEventArgs args)
{
Console.WriteLine("Notification received");
}
using Windows.UI.Notifications.Management;
using Windows.UI.Notifications;
using Windows.Foundation.Metadata;



var listener = UserNotificationListener.Current;

if (!ApiInformation.IsTypePresent("Windows.UI.Notifications.Management.UserNotificationListener"))
{
Console.WriteLine("API can't be found");
}

var access_status = listener.GetAccessStatus();

if (access_status == UserNotificationListenerAccessStatus.Allowed)
{
Console.WriteLine("Allowed");
}
else
{
Console.WriteLine("Not allowed");
}

var notifications = await listener.GetNotificationsAsync(NotificationKinds.Toast);

Console.WriteLine(notifications.Count());

try
{
listener.NotificationChanged += Listener_NotificationChanged;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}

void Listener_NotificationChanged(UserNotificationListener sender, UserNotificationChangedEventArgs args)
{
Console.WriteLine("Notification received");
}
ive changed it to .net 8.0 targeting windows 10.0.19041.0 using the windows sdk it can get the list of notifications perfectly fine but cant seem to bind an event handler
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
superloubou
superloubou4mo ago
nowhere in any documentation does it suggest that i need anything more than the windows 10 sdk my version of windows is above the minimum and shouldnt rely on any ui/gui packages other than windows.ui the windows.ui.notifications.management api is also not listed under the list of apis which require a package identity
Want results from more Discord servers?
Add your server