Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
symonxdd opened this issue Mar 8, 2023 · 4 comments
Open
Labels
bug Something isn't working

Comments

@symonxdd
Copy link

symonxdd commented Mar 8, 2023

Describe the bug

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

  • Unknown member ContextMenu on element TaskbarIcon
  • The property ContextMenu was not found in type TaskbarIcon

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

  • Microsoft.Windows.SDK.BuildTools: v10.0.22621.755
  • Microsoft.WindowsAppSDK: v1.2.230217.4
  • H.NotifyIcon.WinUI: v2.0.93

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:

  • MainWindow.xaml:
<!-- 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>
  • MainWindow.xaml.cs:
using Microsoft.UI.Xaml;

namespace HNotifyIconAttempt2;

public sealed partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
}
@symonxdd symonxdd added the bug Something isn't working label Mar 8, 2023
@symonxdd symonxdd changed the title The property ContextMenu was not found in type TaskbarIcon [WinUI] The property ContextMenu was not found in type TaskbarIcon Mar 8, 2023
@HavenDV
Copy link
Owner

HavenDV commented Mar 8, 2023

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

@symonxdd
Copy link
Author

symonxdd commented Mar 8, 2023

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
Copy link

steam3d commented Mar 9, 2023

@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
Copy link
Owner

HavenDV commented Mar 9, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants