Issues with Badges in Windows Forms not showing up on task bar

Joined
Jun 17, 2021
Messages
8
Programming Experience
10+
Hello. I have an issue with badge notifications using windows forms. The badge icon does not show up in the taskbar, but does show up in the start menu. I made a test UWP app, where the badges show up both in the start menu and in the taskbar no problem. I run the exact same code to send a badge notification in both applications, which is shown below.

C#:
var xmlText = "<badge value=\"alert\"/>";
var xmlDoc = new Windows.Data.Xml.Dom.XmlDocument();
xmlDoc.LoadXml(xmlText);
var badgeNotification = new BadgeNotification(xmlDoc);

var bu = BadgeUpdateManager.CreateBadgeUpdaterForApplication();

bu.Update(badgeNotification);
 
What library are you using to get access to the Badge APIs in WinForms? Is it the same library you were using for the UWP app?
 
I am not quite sure how to figure that out, sorry, but I guess I am using the Windows.Foundation.UniversalApiContract library?

Another development; it turns out if the app has two windows (that both show up when hovering over the task bar), the badge shows up in the taskbar, but when the extra window is closed (so there's just one window again), the badge disappears again.

Edit: The BadgeNotification and BadgeUpdateManager are in both apps from the namespace Windows.UI.Notifications (don't know if there are any alternatives here).
 
I've not looked real closely at how the Windows Badge APIs work, but I would assume that it only applies the badge to the primary UI window. I suppose that window that you are closing is the likely the main window.

The only reason why I was asking what library you were using is because I know that the badge APIs don't come natively in WinForms when it was originally released. The badge API was not in Windows XP way back then. So that means that you needed to have added an assembly reference somewhere to be able have access to the API somehow. I just wanted to make sure that you were not using some wildcat implementation of the wrappers for the badge APIs with your WinForms app, while you were using the MS published libraries for your UWP app. It sounds like you are using the official MS libraries for both, and I'll continue with that assumption.

I think you need to show us more of your WinForms code. We need to put that code that you first showed in post #1 into the context of how your various WinForms forms are being brought up and kept alive.
 
I have a form called MainWindow, which is an instance of a class called MainForm that inherits from Form. This form is also the one that calls the BadgeNotification code seen above, when a button is pressed. It is started and maintained by running Systems.Windows.Forms.Application.Run(MainWindow) inside the Main function in the static class Program that winform projects in Visual Studio have by default. The main form is the first form that is made visible, but it has a few child windows made visible immediately afterwards (that do not show up in the taskbar)

The init code:
C#:
MainWindow = new Forms.MainForm(uri);

Application.Run(MainWindow);

I don't know exactly what other code is of interest. Thank you very much for taking the time to help me.

EDIT: More forms can be spawned by a WebView2 component, which is how I noticed the effect with multiple windows. It does not matter if these extra windows come from WebView2 or not, though, simply spawning a second form that also shows up in the taskbar at startup will also make the badge show up.
 
Last edited:
I tested it today with a completely blank Windows Forms project, and it exhibits the same behavior. I make a new project in windows forms, change the target platform and create a badge notification, and when packaged, the badge shows up in the start menu, but not in the taskbar. As before, opening another form makes the badge show up. Is this expected behavior?
 
Sorry to bug you again, but where exactly did you get your "Windows.Foundation.UniversalApiContract library"? All I'm finding in Nuget is the one made by SilverFang. I'm not seeing an one made by Microsoft.

I wanted to try to reproduce your problem, but I'm not even getting past creating a project to reproduce the problem because I'm not finding the library that you are using.
 
When I made a blank project to create a simple reproduction, I opened the project file and changed <TargetFramework> to net5.0-windows10.0.19041.0, which gave me access to Windows.UI.Notifications. In the previous project, I used the NuGet package Microsoft.Windows.SDK.Contracts. Both projects exhibit the same behavior.
 
Ah. Okay. Thanks. Can you share the manifest file you are using to register your app so that the Badge Update Manager won't throw an exception due to an unregistered app?
 
The file Package.appxmanifest is below
C#:
<?xml version="1.0" encoding="utf-8"?>
<Package
  xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
  xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
  xmlns:uap5="http://schemas.microsoft.com/appx/manifest/uap/windows10/5"
  xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
  IgnorableNamespaces="uap uap5 rescap">
  <Identity <!-- Company stuff here --> Version="2.0.3060.0" />
  <Properties>
    <DisplayName>Viewer3D-debug</DisplayName>
    <PublisherDisplayName> <!-- Company name --> </PublisherDisplayName>
    <Logo>Assets\StoreLogo.png</Logo>
  </Properties>
  <Dependencies>
    <TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" />
    <TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.14393.0" MaxVersionTested="10.0.14393.0" />
  </Dependencies>
  <Resources>
    <Resource Language="x-generate" />
  </Resources>
  <Applications>
    <Application Id="App"
      Executable="$targetnametoken$.exe"
      EntryPoint="$targetentrypoint$">
      <uap:VisualElements DisplayName="Viewer3D-debug" Square44x44Logo="Assets\Square44x44Logo.png" Description="3D Viewer workbench" BackgroundColor="transparent" Square150x150Logo="Assets\Square150x150Logo.png">
        <uap:DefaultTile Square71x71Logo="Assets\SmallTile.png" Wide310x150Logo="Assets\WideTile.png" Square310x310Logo="Assets\LargeTile.png"/>
        <uap:SplashScreen Image="Assets\SplashScreen.png" BackgroundColor="#FFFFFF"/>
      </uap:VisualElements>
      <uap:ApplicationContentUriRules>
        <uap:Rule Match="http://localhost:4200" Type="include" WindowsRuntimeAccess="none" />
        <uap:Rule Match="http://localhost:4201" Type="include" WindowsRuntimeAccess="none" />
        <uap:Rule Type="include" Match="http://localhost:" WindowsRuntimeAccess="none"/>
      </uap:ApplicationContentUriRules>

      <Extensions>
        <uap5:Extension
          Category="windows.appExecutionAlias"
          EntryPoint="Windows.FullTrustApplication">
          <uap5:AppExecutionAlias>
            <uap5:ExecutionAlias Alias="viewer3d-debug.exe" />
          </uap5:AppExecutionAlias>
        </uap5:Extension>
        <uap:Extension Category="windows.protocol">
          <uap:Protocol Name="viewer3d-debug"/>
        </uap:Extension>
        <!--<uap:Extension Category="windows.appService" EntryPoint="AppUpdaterService.UpdateTask">
          <uap:AppService Name="AppUpdaterService.AutoUpdate"/>
        </uap:Extension>-->
      </Extensions>
    </Application>
  </Applications>
  <Capabilities>
    <Capability Name="internetClient" />
    <rescap:Capability Name="packageManagement" />
    <rescap:Capability Name="runFullTrust" />
  </Capabilities>
</Package>
 
Last edited by a moderator:
Thanks for sharing. When I get a chance, I'll try modifying that to register my test app.
 
Sorry, I lost interest in this because it was a total pain trying to register the app on my machine kept on failing.
 
Back
Top Bottom