private void DoLayoutReset(bool forceUpdate = false) { if (_resetQueued || forceUpdate) { _resetQueued = false; if (_nativeWindow is null) { var appMenu = NativeMenu.GetMenu(Application.Current); if (appMenu == null) { appMenu = CreateDefaultAppMenu(); NativeMenu.SetMenu(Application.Current, appMenu); } SetMenu(appMenu); } else { if (_menu != null) { SetMenu(_nativeWindow, _menu); } } _exported = true; } }
protected override void OnAttached() { base.OnAttached(); Observable.FromEventPattern(AssociatedObject, nameof(AssociatedObject.AttachedToVisualTree)) .Take(1) .Subscribe(x => { if (Menu != null) { if (AssociatedObject.GetVisualRoot() is TopLevel tl) { NativeMenu.SetMenu(tl, Menu); } } }); }
public static NotifyIcon?Init(App app, EventHandler notifyIconClick) { if (IsInitialized) { return(null); } NotifyIcon? notifyIcon = null; IDisposable?menuItemDisposable = null; var icon = GetIconByCurrentAvaloniaLocator(); var text = TaskBarWindowViewModel.TitleString; #if WINDOWS if (OperatingSystem2.IsWindows) { notifyIcon = DI.Get <NotifyIcon>(); notifyIcon.Text = text; notifyIcon.Icon = icon; #if WINDOWS notifyIcon.RightClick += (_, e) => { IViewModelManager.Instance.ShowTaskBarWindow(e.X, e.Y); }; #else menuItemDisposable = InitMenuItems(notifyIcon); #endif notifyIcon.Click += notifyIconClick; notifyIcon.DoubleClick += notifyIconClick; notifyIcon.AddTo(app); } else #endif { // if (OperatingSystem2.IsLinux) // { ////#pragma warning disable CA1416 // 验证平台兼容性 //// if (IPlatformService.Instance.IsDeepin) ////#pragma warning restore CA1416 // 验证平台兼容性 //// { //// // Deepin catch. //// // Unhandled exception: System.Reflection.TargetInvocationException: Arg_TargetInvocationException //// //---> Tmds.DBus.DBusException: com.deepin.DBus.Error.Unnamed: notifier item has been registered //// // at Tmds.DBus.DBusConnection.CallMethodAsync(Message msg, Boolean checkConnected, Boolean checkReplyType) //// // at Tmds.DBus.Connection.CallMethodAsync(Message message) //// // at Tmds.DBus.CodeGen.DBusObjectProxy.SendMethodReturnReaderAsync(String iface, String member, Nullable`1 inSignature, MessageWriter writer) //// // at Avalonia.X11.X11TrayIconImpl.CreateTrayIcon() in /_/src/Avalonia.X11/X11TrayIconImpl.cs:line 85 //// // at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__127_0(Object state) //// // at Avalonia.Threading.AvaloniaSynchronizationContext.<>c__DisplayClass5_0.<Post>b__0() in /_/src/Avalonia.Base/Threading/AvaloniaSynchronizationContext.cs:line 33 //// // at Avalonia.Threading.JobRunner.Job.Avalonia.Threading.JobRunner.IJob.Run() in /_/src/Avalonia.Base/Threading/JobRunner.cs:line 166 //// // at Avalonia.Threading.JobRunner.RunJobs(Nullable`1 priority) in /_/src/Avalonia.Base/Threading/JobRunner.cs:line 37 //// // at Avalonia.X11.X11PlatformThreading.CheckSignaled() in /_/src/Avalonia.X11/X11PlatformThreading.cs:line 164 //// // at Avalonia.X11.X11PlatformThreading.RunLoop(CancellationToken cancellationToken) in /_/src/Avalonia.X11/X11PlatformThreading.cs:line 244 //// // at Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancellationToken) in /_/src/Avalonia.Base/Threading/Dispatcher.cs:line 61 //// return null; //// } // } NativeMenu menu = new(); menuItemDisposable = InitMenuItems(menu); TrayIcon trayIcon = new() { Icon = new(icon), ToolTipText = text, Menu = menu, }; trayIcon.Clicked += notifyIconClick; TrayIcon.SetIcons(app, new() { trayIcon, }); if (OperatingSystem2.IsMacOS) { NativeMenu.SetMenu(app, menu); } } if (menuItemDisposable != null) { menuItemDisposable.AddTo(app); } IsInitialized = true; return(notifyIcon); }