HavenDV / H.NotifyIcon

TrayIcon for WPF/WinUI/Uno/MAUI
MIT License
568 stars 46 forks source link

[WinUI] The property `ContextMenu` was not found in type `TaskbarIcon` #80

Open symonxdd opened 1 year ago

symonxdd commented 1 year ago

Describe the bug

The XAML compiler gives following errors when using the template code from the README:

Steps to reproduce the bug

  1. Follow the installation / basic setup from the README
  2. The Error List view should display 2 errors. If not, try to build / run the app

Expected behavior

The app is supposed to compile / build fine.

Screenshots

image

NuGet package version

The Microsoft.WindowsAppSDK library needed to updated as instructed in the Error List view when trying to install this repo's library H.NotifyIcon.WinUI

Platform

WinUI

IDE

Visual Studio 2022

Windows Version

Windows 11

WindowsAppSDK Version

Other

WindowsAppSDK Type

Packaged

Manifest

I don't know what is meant by this, but I assume the content's of my app.manifest? I didn't change it.

app.manifest:

<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
  <assemblyIdentity version="1.0.0.0" name="HNotifyIconAttempt2.app"/>

  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
      <!--The ID below informs the system that this application is compatible with OS features first introduced in Windows 8. 
      For more info see https://docs.microsoft.com/windows/win32/sysinfo/targeting-your-application-at-windows-8-1 

      It is also necessary to support features in unpackaged applications, for example the custom titlebar implementation.-->
      <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />
    </application>
  </compatibility>

  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <!-- The combination of below two tags have the following effect:
           1) Per-Monitor for >= Windows 10 Anniversary Update
           2) System < Windows 10 Anniversary Update
      -->
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/PM</dpiAware>
      <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2, PerMonitor</dpiAwareness>
    </windowsSettings>
  </application>
</assembly>

Additional context

Here's my full MainWindow XAML -and code-behind code:

<!-- Copyright (c) Microsoft Corporation and Contributors. -->
<!-- Licensed under the MIT License. -->

<Window
    x:Class="HNotifyIconAttempt2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:HNotifyIconAttempt2"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:tb="using:H.NotifyIcon">

    <tb:TaskbarIcon
        ToolTipText="ToolTip"
        IconSource="/Images/TrayIcons/Logo.ico"
        ContextMenu="{StaticResource TrayMenu}"
        MenuActivation="LeftOrRightClick"
        TrayPopup="{StaticResource TrayStatusPopup}"
        PopupActivation="DoubleClick"
        TrayToolTip="{StaticResource TrayToolTip}"
        />
</Window>
using Microsoft.UI.Xaml;

namespace HNotifyIconAttempt2;

public sealed partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
}
HavenDV commented 1 year ago

WinUI uses ContextFlyout instead ContextMenu. README contains example for WPF.

symonxdd commented 1 year ago

Thank for getting back so quickly! Though I still can't get it to work. I'm getting following error message when running:

Cannot find a Resource with the Name/Key TrayMenu

I'm aware this is probably because I didn't define that resource nowhere. I searched the Internet and found some sample code for TrayMenu, as well as TrayStatusPopup. I put it in my App.xaml like so:

App.xaml:

<Application
    x:Class="WinUITrayTest.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WinUITrayTest">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
                <!-- Other merged dictionaries here -->
            </ResourceDictionary.MergedDictionaries>
            <!-- Other app resources here -->
            <MenuFlyout x:Key="TrayMenu">
                <MenuFlyoutItem Text="Red" Tag="red" />
                <MenuFlyoutItem Text="Green" Tag="green"/>
            </MenuFlyout>

            <Border x:Key="TrayStatusPopup" Background="White" BorderBrush="Gray" BorderThickness="1" CornerRadius="3" Width="auto" Height="auto">
                <StackPanel>
                    <Button Name="btnSetupTray" Content="Setup" Margin="5" Width="70"></Button>
                    <Button Name="btnExitTray" Content="Exit" Margin="5" Width="50"></Button>
                </StackPanel>
            </Border>
        </ResourceDictionary>
    </Application.Resources>
</Application>

As for the last resource, I didn't define anything, because I just wanted to test it out quickly. So this exception appears when building the code:

Failed to assign to property 'H.NotifyIcon.TaskbarIcon.TrayPopup'

Also, if I can suggest. Wouldn't it be much clearer if in the README it'd be specified that for WinUI 3 apps, the property ContextFlyout should be used? I could be wrong, but I didn't find it nowhere in the repo description?

Also, I think that it'd be very useful to include some sample code for all three resources: TrayMenu, TrayStatusPopup and TrayToolTip. What do you think?

steam3d commented 1 year ago

@symonxdd If you already know how to use the H.NotifyIcon you could add examples or improve README.

I thought to add examples for WinUI3 because I use it.

HavenDV commented 1 year ago

There are two applications here as examples: https://github.com/HavenDV/H.NotifyIcon/tree/master/src/apps/H.NotifyIcon.Apps.WinUI.Windowless https://github.com/HavenDV/H.NotifyIcon/tree/master/src/apps/H.NotifyIcon.Apps.WinUI But I agree with you that the README needs to be improved.