WPF pasek notifcation area-przywrócenie aplikacji

0

Witam,
znalazłem przykłady w internecie do umieszczania aplikacji w pasku notification area(przy zegarku)(http://www.hardcodet.net/wpf-notifyicon).
Postanowiłem napisać sobie aplikację wyłączająca system po upływie wybranego czasu(taki stoper). Przed schowaniem aplikacji do paska w kontrolce label, co sekundę odliczam ile czasu pozostało do zamknięcia. Po zmnimalizowaniu appki do paska i ponownym jej przywróceniu, stopera brak-okno tworzy się na nowo. Czy ktoś może mi napisać jak zrobić żeby licznik po przywróceniu aplikacji wskazywał aktualną wartość? Poniżej kod( z internetu) do minimalizowania do paska:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Czasowy_Wylacznik_PC
{

    /// <summary>
    /// Provides bindable properties and commands for the NotifyIcon. In this sample, the
    /// view model is assigned to the NotifyIcon in XAML. Alternatively, the startup routing
    /// in App.xaml.cs could have created this view model, and assigned it to the NotifyIcon.
    /// </summary>
    public class NotifyIconViewModel
    {
        /// <summary>
        /// Shows a window, if none is already open.
        /// </summary>
        public ICommand ShowWindowCommand
        {
            get
            {
                return new DelegateCommand
                {
                    CanExecuteFunc = () =>Application.Current.MainWindow == null,
                    CommandAction = () =>
                    {
                        Application.Current.MainWindow = new MainWindow();//chyba tutaj jest problem..
                       // Application.Current.MainWindow.me
                        //Application.Current.MainWindow.Show();

                    }
                };
            }
        }

        /// <summary>
        /// Hides the main window. This command is only enabled if a window is open.
        /// </summary>
        public ICommand HideWindowCommand
        {
            get
            {
                return new DelegateCommand
                {
                  //  CommandAction = () => Application.Current.MainWindow.Close(),
                    CommandAction = () => Application.Current.MainWindow.Hide(), //zmieniłem na hide, bylo jak wyzej Close()
                    CanExecuteFunc = () => Application.Current.MainWindow != null,
                };

            }
        }


        /// <summary>
        /// Shuts down the application.
        /// </summary>
        public ICommand ExitApplicationCommand
        {
            get
            {
                return new DelegateCommand { CommandAction = () => Application.Current.Shutdown() };
            }
        }
    }


    /// <summary>
    /// Simplistic delegate command for the demo.
    /// </summary>
    public class DelegateCommand : ICommand
    {
        public Action CommandAction { get; set; }
        public Func<bool> CanExecuteFunc { get; set; }

        public void Execute(object parameter)
        {
            CommandAction();
        }

        public bool CanExecute(object parameter)
        {
            return CanExecuteFunc == null || CanExecuteFunc();
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    }
}

0
public ICommand ShowWindowCommand
        {
            get
            {
                return new DelegateCommand
                {
                    CanExecuteFunc = () =>Application.Current.MainWindow != null,   //tu zmień
                    CommandAction = () =>
                    {
                        Application.Current.MainWindow.Show(); //i tu
 
                    }
                };
            }
        }

I wydaje mi się że powinno działać

0

Niestety po tych zmianach, po zminimalizowaniu nie można przywrócić aplikacji. Znajduje się w pasku, ale nie da się jej otworzyć.

0

W HideWindowCommand masz MineWindow.Hide()?
Pokaż cały kod.

0
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:tb="http://www.hardcodet.net/taskbar"
                    xmlns:local="clr-namespace:Czasowy_Wylacznik_PC">

    <!-- The taskbar context menu - the first row is a dummy to show off simple data binding -->
    <!--
        The "shared" directive is needed if we reopen the sample window a few times - WPF will otherwise
        reuse the same context menu (which is a resource) again (which will have its DataContext set to the old TaskbarIcon)
  -->
    <ContextMenu x:Shared="false" x:Key="SysTrayMenu">
        <MenuItem Header="Pokaż wyłącznik" Command="{Binding ShowWindowCommand}" />
        <MenuItem Header="Schowaj wyłącznik" Command="{Binding HideWindowCommand}" />
        <Separator />
        <MenuItem Header="Zamknij" Command="{Binding ExitApplicationCommand}" />
    </ContextMenu>


    <!-- the application's NotifyIcon - started from App.xaml.cs. Declares its own view model. -->
    <tb:TaskbarIcon x:Key="NotifyIcon"
                    IconSource="/Red.ico"
                    ToolTipText="Podwójne kliknięcie otwiera okno, kliknięcie prawym pokazuje menu"
                    DoubleClickCommand="{Binding ShowWindowCommand}"
                    ContextMenu="{StaticResource SysTrayMenu}">

        <!-- self-assign a data context (could also be done programmatically) -->
        <tb:TaskbarIcon.DataContext>
            <local:NotifyIconViewModel />
        </tb:TaskbarIcon.DataContext>
    </tb:TaskbarIcon>

</ResourceDictionary>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Czasowy_Wylacznik_PC
{

    /// <summary>
    /// Provides bindable properties and commands for the NotifyIcon. In this sample, the
    /// view model is assigned to the NotifyIcon in XAML. Alternatively, the startup routing
    /// in App.xaml.cs could have created this view model, and assigned it to the NotifyIcon.
    /// </summary>
    public class NotifyIconViewModel
    {
        /// <summary>
        /// Shows a window, if none is already open.
        /// </summary>
        public ICommand ShowWindowCommand
        {
            get
            {
                return new DelegateCommand
                {
                    CanExecuteFunc = () =>Application.Current.MainWindow == null,
                    CommandAction = () =>
                    {
                        Application.Current.MainWindow = new MainWindow();
                       // Application.Current.MainWindow.me
                        Application.Current.MainWindow.Show();

                    }
                };
            }
        }

        /// <summary>
        /// Hides the main window. This command is only enabled if a window is open.
        /// </summary>
        public ICommand HideWindowCommand
        {
            get
            {
                return new DelegateCommand
                {
                  //  CommandAction = () => Application.Current.MainWindow.Close(),
                    CommandAction = () => Application.Current.MainWindow.Hide(),
                    CanExecuteFunc = () => Application.Current.MainWindow != null,
                };

            }
        }


        /// <summary>
        /// Shuts down the application.
        /// </summary>
        public ICommand ExitApplicationCommand
        {
            get
            {
                return new DelegateCommand { CommandAction = () => Application.Current.Shutdown() };
            }
        }
    }


    /// <summary>
    /// Simplistic delegate command for the demo.
    /// </summary>
    public class DelegateCommand : ICommand
    {
        public Action CommandAction { get; set; }
        public Func<bool> CanExecuteFunc { get; set; }

        public void Execute(object parameter)
        {
            CommandAction();
        }

        public bool CanExecute(object parameter)
        {
            return CanExecuteFunc == null || CanExecuteFunc();
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    }
}

0

Podbijam. Pomoże ktoś?

0

Wrzucam spakowany cały projekt. Pozdrawiam

0

Ponieważ nie potrafisz przekopiować kodu, miało być tak:

public ICommand ShowWindowCommand
        {
            get
            {
                return new DelegateCommand
                {
                    CanExecuteFunc = () =>Application.Current.MainWindow != null,   //tu zmień
                    CommandAction = () =>
                    {
                        Application.Current.MainWindow.Show(); //i tu
 
                    }
                };
            }
        }

a ty masz tak:

        public ICommand ShowWindowCommand
        {
            get
            {
                return new DelegateCommand
                {
                    CanExecuteFunc = () =>Application.Current.MainWindow == null,
                    CommandAction = () =>
                    {
                        Application.Current.MainWindow = new MainWindow();
                       // Application.Current.MainWindow.me
                        Application.Current.MainWindow.Show();

                    }
                };
            }
        }

Ale wtedy też pojawia się następny błąd, wyłączając krzyżykiem nie zwalnia poprawnie zasobów. Trzeba chyba przy wyłączaniu wykonać to co masz w OnExit w App.xaml.cs.
Nie wiem po co samo NotifyIcon robisz w MVVM a główne okno masz na eventach.

0
dam1an napisał(a):

Ponieważ nie potrafisz przekopiować kodu, miało być tak:

Tak jak wyżej- mam problem, że przy tym kodzie, po zamknięciu aplikacji krzyżykiem minimalizuje się ona do paska, ale nie da się potem otworzyć( został w projekcie stary kod po prostu;)).

dam1an napisał(a):

Ale wtedy też pojawia się następny błąd, wyłączając krzyżykiem nie zwalnia poprawnie zasobów. Trzeba chyba przy wyłączaniu wykonać to co masz w OnExit w App.xaml.cs.
Nie wiem po co samo NotifyIcon robisz w MVVM a główne okno masz na eventach.

Możesz podesłać jakieś gotowe rozwiązanie tego problemu?

1 użytkowników online, w tym zalogowanych: 0, gości: 1