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

MenuFlyoutItem Click Events not beeing called #92

Open
ste-phil opened this issue Apr 25, 2023 · 1 comment
Open

MenuFlyoutItem Click Events not beeing called #92

ste-phil opened this issue Apr 25, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@ste-phil
Copy link

Describe the bug

MenuFlyoutItem Click events not called in the app

image

Steps to reproduce the bug

  • Create a blank WinUI3 app.
  • Reference H.NotifyIcon.WinUI
  • Insert tray-icon code into app.xaml resources
<tb:TaskbarIcon
    x:Key="TrayIcon3"
    ToolTipText="ToolTip"
    NoLeftClickDelay="True"
>
  <tb:TaskbarIcon.ContextFlyout>
          <MenuFlyout>
              <ToggleMenuFlyoutItem x:Name="EnabledToggle" Text="Enabled" IsChecked="True"/>
              <ToggleMenuFlyoutItem x:Name="EnabledNotificationsToggle" Text="Notification on unpack" IsChecked="True"/>
              <MenuFlyoutSeparator></MenuFlyoutSeparator>
              <MenuFlyoutItem x:Name="ExitBtn" Text="Exit"/>
          </MenuFlyout>
      </tb:TaskbarIcon.ContextFlyout>
  </tb:TaskbarIcon>
  • Get the TaskBarIcon reference from the Resources and assign some event handlers to the individual flyout items
taskbarIcon = Resources["TrayIcon3"] as TaskbarIcon;
taskbarIcon.ForceCreate(false);

var enabledToggle = taskbarIcon.FindName("EnabledToggle") as ToggleMenuFlyoutItem;
var enabledNotificationToggle = taskbarIcon.FindName("EnabledNotificationsToggle") as ToggleMenuFlyoutItem;
var exitBtn = taskbarIcon.FindName("ExitBtn") as MenuFlyoutItem;

enabledToggle.Click += EnabledToggle_Clicked;
enabledNotificationToggle.Click += EnabledNotificationToggle_Clicked;
exitBtn.Click += ExitBtn_Clicked;

Expected behavior

Events should be called

Screenshots

No response

NuGet package version

image

Platform

WinUI

IDE

Visual Studio 2022

Windows Version

Windows 11

WindowsAppSDK Version

Other

WindowsAppSDK Type

Other

Manifest


<Package
  xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
  xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
  xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
  IgnorableNamespaces="uap rescap">

  <Identity
    Name="-"
    Publisher="-"
    Version="1.0.0.0" />

  <Properties>
    <DisplayName>AutoUnzipper</DisplayName>
    <PublisherDisplayName>phste</PublisherDisplayName>
    <Logo>Assets\StoreLogo.png</Logo>
  </Properties>

  <Dependencies>
    <TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.17763.0" MaxVersionTested="10.0.19041.0" />
    <TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.17763.0" MaxVersionTested="10.0.19041.0" />
  </Dependencies>

  <Resources>
    <Resource Language="x-generate"/>
  </Resources>

  <Applications>
    <Application Id="App"
      Executable="$targetnametoken$.exe"
      EntryPoint="$targetentrypoint$">
      <uap:VisualElements
        DisplayName="AutoUnzipper"
        Description="AutoUnzipper"
        BackgroundColor="transparent"
        Square150x150Logo="Assets\Square150x150Logo.png"
        Square44x44Logo="Assets\Square44x44Logo.png">
        <uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png"  Square71x71Logo="Assets\SmallTile.png" Square310x310Logo="Assets\LargeTile.png"/>
        <uap:SplashScreen Image="Assets\SplashScreen.png" />
      </uap:VisualElements>
    </Application>
  </Applications>

  <Capabilities>
    <rescap:Capability Name="runFullTrust" />
  </Capabilities>
</Package>
`

### Additional context

_No response_
@ste-phil ste-phil added the bug Something isn't working label Apr 25, 2023
@exequielarroyo
Copy link

exequielarroyo commented May 19, 2023

You can use ICommand or XamlUICommand interface instead.

    public NavigationPage()
    {
        this.InitializeComponent();
        
        // Add Event Listener for the Command
        Command.ExecuteRequested += Command_ExecuteRequested;
    }
    
    // Property with Instanciation
    public XamlUICommand Command { get; set; } = new XamlUICommand();
    
    // Example Event (exits that application)
    private void Command_ExecuteRequested(XamlUICommand sender, ExecuteRequestedEventArgs args)
    {
        App.Current.Exit();
    }

Sample WinUI XAML

        <tb:TaskbarIcon
            x:Name="TrayIcon"
            ToolTipText="{StaticResource appName}"
            TrayToolTip="{StaticResource appName}"
            ContextMenuMode="SecondWindow"
            IconSource="./../Assets/Icon1.ico"
            >
            <tb:TaskbarIcon.ContextFlyout>
                <MenuFlyout AreOpenCloseAnimationsEnabled="False">
                    <MenuFlyoutItem Command="{x:Bind ShowHide}" Text="Show/Hide Window" />
                    <MenuFlyoutSeparator />
                    <MenuFlyoutItem Command="{x:Bind Command}" Text="Exit" />
                </MenuFlyout>
            </tb:TaskbarIcon.ContextFlyout>
        </tb:TaskbarIcon>

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

2 participants