wpfui icon indicating copy to clipboard operation
wpfui copied to clipboard

NotifyIcon.Menu Binding Failed

Open LHTXQ opened this issue 3 years ago • 6 comments

Describe the bug

Tray icon menu item binding failed, only a white bar will be displayed. image

To Reproduce

NotifyIconMenuItems = new ObservableCollection<object>{......}

<ui:NotifyIcon.Menu>
   <ContextMenu ItemsSource="{Binding NotifyIconMenuItems,Mode=OneWay}"/>
</ui:NotifyIcon.Menu>

Expected behavior

Screenshots

No response

OS version

Newest.

.NET version

6.0

WPF-UI NuGet version

Newest.

Additional context

No response

LHTXQ avatar Feb 18 '23 15:02 LHTXQ

You need to write items directly in XAML instead of using itemsSource

What if I want to change the content while running? In general, both should work well.

LHTXQ avatar Mar 02 '23 10:03 LHTXQ

I played a lot of English before I found that you should be able to understand Chinese

I know what you mean, except " I played a lot of English before I found that you should be able to understand Chinese". 因为按这句话的意思你接下来应该会说中文,但却仍是英文。

LHTXQ avatar Mar 02 '23 12:03 LHTXQ

I played a lot of English before I found that you should be able to understand Chinese

I know what you mean, except " I played a lot of English before I found that you should be able to understand Chinese". 因为按这句话的意思你接下来应该会说中文,但却仍是英文。

用的翻译机 后面就懒得改中文了... 我觉得这个项目有缺陷 你看他给的demo demo里没有功能 一般都是有缺陷的(甚至不能使用默认的) 建议还是根据他的demo 学习他的每个部分的细节技术 和设计理念 然后自己搞 listview之类的 没有表头是真的离谱 我甚至都打算用一个动态grid之类的写个表了

:)懒得想英文了,以下也是翻译的: One of the aims of open-source software is to reduce duplication of effort, so that everyone can contribute to the same project with less effort. (“事半功倍”的意思完全没翻译出来。) My programming skills are too poor to contribute at the moment, but it would be great if you could incorporate your own modifications into the project, while also reaping the benefits of other improved parts of the project.

LHTXQ avatar Mar 02 '23 12:03 LHTXQ

I played a lot of English before I found that you should be able to understand Chinese

I know what you mean, except " I played a lot of English before I found that you should be able to understand Chinese". 因为按这句话的意思你接下来应该会说中文,但却仍是英文。

用的翻译机 后面就懒得改中文了... 我觉得这个项目有缺陷 你看他给的demo demo里没有功能 一般都是有缺陷的(甚至不能使用默认的) 建议还是根据他的demo 学习他的每个部分的细节技术 和设计理念 然后自己搞 listview之类的 没有表头是真的离谱 我甚至都打算用一个动态grid之类的写个表了

:)懒得想英文了,以下也是翻译的: One of the aims of open-source software is to reduce duplication of effort, so that everyone can contribute to the same project with less effort. My programming skills are too poor to contribute at the moment, but it would be great if you could incorporate your own modifications into the project, while also reaping the benefits of other improved parts of the project.

I'm not saying that the author's open source project is not good. I mean that there are still some defects in some aspects. Don't use it yourself I don't have a solution (to solve this problem correctly so that later comers can use it quickly and accurately). Now I can only modify some places to mask some places to achieve some very simple functions

好吧。

😂,感觉开头的 “ I'm not saying that ...” 有点“中里中气”的,或许“ I'm not meaning that...” 更合适。

LHTXQ avatar Mar 02 '23 12:03 LHTXQ

我发现不仅仅ContextMenu无法通过ItemsSource赋值,甚至手动绑定的Command也无法触发,请问你是否有碰到这种情况,如何解决呢

Xaml: <ui:MenuItem Header="退出" Command="{Binding TrayMenuActionCommand}" CommandParameter="exit"/>

ViewModel: [RelayCommand] public void OnTrayMenuAction(string action) { }

cnzhangw avatar Dec 05 '24 07:12 cnzhangw

用之前的写法处理

public class CustomNotifyIconService : NotifyIconService
{
    public CustomNotifyIconService()
    {
        TooltipText = "DEMO";
        // If this icon is not defined, the application icon will be used.
        Icon = BitmapFrame.Create(
            new Uri("pack://application:,,,/Assets/wpfui-icon-256.png", UriKind.Absolute)
        );
        ContextMenu = new ContextMenu
        {
            FontSize = 14d,
            Items =
        {
            new Wpf.Ui.Controls.MenuItem
            {
                Header = "Home",
                Icon = new SymbolIcon { Symbol = SymbolRegular.Home12 },
                Tag = "home",
            },              new Wpf.Ui.Controls.MenuItem
            {
                Header = "Close",
                Icon = new SymbolIcon { Symbol = SymbolRegular.ClosedCaption16 },
                Tag = "Close",
                Command=new RelayCommand(() => App.Current.Shutdown())
            },
        }
        };
        foreach (var singleContextMenuItem in ContextMenu.Items)
            if (singleContextMenuItem is System.Windows.Controls.MenuItem)
                ((System.Windows.Controls.MenuItem)singleContextMenuItem).Click += OnMenuItemClick;

    }

    protected override void OnLeftClick()
    {
        System.Diagnostics.Debug.WriteLine(
            $"DEBUG | WPF UI Tray event: {nameof(OnLeftClick)}",
            "Wpf.Ui.Demo"
        );
    }

    private void OnMenuItemClick(object sender, RoutedEventArgs e)
    {
        if (sender is not System.Windows.Controls.MenuItem menuItem)
            return;

        System.Diagnostics.Debug.WriteLine(
            $"DEBUG | WPF UI Tray clicked: {menuItem.Tag}",
            "Wpf.Ui.Demo"
        );
    }

}

ApplicationHostService.cs

 private INavigationWindow _navigationWindow;
  private async Task HandleActivationAsync()
        {
            if (!Application.Current.Windows.OfType<MainWindow>().Any())
            {
                _navigationWindow = (
                    _serviceProvider.GetService(typeof(INavigationWindow)) as INavigationWindow
                )!;
                _navigationWindow!.ShowWindow();
                _navigationWindow.Navigate(typeof(Views.Pages.DashboardPage));
            }
            var notifyIconManager =
    _serviceProvider.GetService(typeof(INotifyIconService)) as INotifyIconService;

            if (!notifyIconManager!.IsRegistered)
            {
                notifyIconManager!.SetParentWindow(_navigationWindow as Window);
                notifyIconManager.Register();
            }
            await Task.CompletedTask;
        }

App.xaml.cs

       _ = services.AddSingleton<INotifyIconService, CustomNotifyIconService>();

coderinhelloworld avatar Dec 31 '24 06:12 coderinhelloworld