Xamarin WPF具有自定义启动功能,在手动调用mainWindow关闭时退出

时间:2019-05-30 07:32:45

标签: c# wpf xamarin.forms

Xamarin WPF已添加到Visual Studio Xamarin.Forms.WPF上的Xamarin Project。

仅编辑WPF项目App.xaml和App.xaml.cs,MainWindow.xaml不变。

我的意图很简单,一个Tray应用程序,它允许用户配置为在应用程序启动时打开mainWindow或根本不显示(仅Tray)。也许实施过于幼稚,可能还不够专业,如果您有实现它的其他方式。请推荐。

App.xaml

<Application x:Class="WPFXamarin3.WPF.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WPFXamarin3.WPF"
             Startup="Application_Startup"

App.xaml.css

   public partial class App : System.Windows.Application
    {

        private NotifyIcon _notifyIcon;
        FormsApplicationPage mainWindow;


        private void Application_Startup(object sender, StartupEventArgs e)
        {
            _notifyIcon = new NotifyIcon();
            _notifyIcon.Icon = Icon.ExtractAssociatedIcon(Assembly.GetEntryAssembly().Location);
            _notifyIcon.ShowBalloonTip(1000, null, "AB", ToolTipIcon.None);
            _notifyIcon.Text = "BB";
            _notifyIcon.Visible = true;

            ContextMenu contextMenu1 = new System.Windows.Forms.ContextMenu();
            MenuItem menuItem1 = new System.Windows.Forms.MenuItem();
            menuItem1.Index = 0;
            menuItem1.Text = "Exit";
            menuItem1.Click += new System.EventHandler(this.menuItem1_Click);

            MenuItem menuItem2 = new System.Windows.Forms.MenuItem();
            menuItem2.Index = 1;
            menuItem2.Text = "Close";
            menuItem2.Click += new System.EventHandler(this.menuItem2_Click);


            contextMenu1.MenuItems.AddRange(
                    new System.Windows.Forms.MenuItem[] { menuItem1, menuItem2 });
            _notifyIcon.ContextMenu = contextMenu1;

            mainWindow = new MainWindow();
            mainWindow.Show();

        }

        private void menuItem1_Click(object Sender, EventArgs e)
        {
            System.Windows.Application.Current.Shutdown();
        }

        private void menuItem2_Click(object Sender, EventArgs e)
        {
            mainWindow.Close();
            //mainWindow.Hide();
        }

0 个答案:

没有答案