添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
乖乖的灯泡  ·  jupyter notebook ...·  1 年前    · 
含蓄的火腿肠  ·  Java-有空值(NULL) ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm using WPF NotifyIcon , and actually I'm trying to learn how to display the window after minimize it in the tray. So when the user double click on the icon, the window should appear again. Actually I've created this code:

private void MetroWindow_StateChanged(object sender, EventArgs e)
   TaskbarIcon tbi = new TaskbarIcon();
   tbi.DoubleClickCommand = Recover(tbi);
     switch (WindowState)
         case WindowState.Minimized:
            Visibility = Visibility.Hidden;
            tbi.Visibility = Visibility.Visible;
            break;
private void Recover(TaskbarIcon tbi)
    tbi.Visibility = Visibility.Hidden;
    Visibility = Visibility.Visible;

How you can see when I minimize the window the icon in the tray appear. This working pretty well. I've declared the icon like this:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:Test.Utils.Resources.UIDictionary"
                xmlns:tb="http://www.hardcodet.net/taskbar">
<tb:TaskbarIcon x:Key="NotifyIcon"
              IconSource="/Utils/Images/Test.ico"
              ToolTipText="hello world" />
</ResourceDictionary>

Now the problem is that on this line: tbi.DoubleClickCommand = Recover(tbi); I get this error:

Cannot convert the type void in System.Windows.Input.ICommand

It's not possible call a method in this way? Why?

I suggest you read up on WPF in general, especially MVVM and the Command pattern. If you treat it as if it were WinForms, you could as well use WinForms. – nvoigt May 11, 2016 at 15:12 To address the actual problem: DoubleClickCommand is an ICommand. You're calling Recover which returns a void and trying to assign that void to something (which never makes sense). Furthermore, to do this, you've created a new TaskbarIcon that is always local to the MetroWindow_StateChanged function. It's never added to the UI, so nothing you do to it matters. I'm not familiar with metro apps, so I'm not sure how to get the TaskbarIcon from your UI that you actually need. – Millie Smith May 11, 2016 at 16:19

Here is the code for a simple RelayCommand that is what you need

public class RelayCommand : ICommand
    private Action<object> action;
    public RelayCommand(Action<object> action)
        this.action = action;
    public bool CanExecute(object parameter)
        return true;
    public void Execute(object parameter)
        action(parameter);
    public event EventHandler CanExecuteChanged;

and then, like the other answer its just

tbi.DoubleClickCommand =new RelayCommand(_ => Recover(tbi));
                @DeanChalk Okay, now is clear, I thought that RelayCommand was a class of c#, thanks for the nice example :)
– Dillinger
                May 11, 2016 at 15:22

Actually, DoubleClickCommand is of type ICommand.

You need to set it as RelayCommand which is subtype of ICommand for it to compile like:

 tbi.DoubleClickCommand =new RelayCommand(param => Recover(tbi));

You can read more about Relay Commands on this MSDN link

Uhm, I don't understand a thing, when I paste your code the compiler underline RelayCommand and display: RelayCommand was not found. I've no using available in the intellisense, what's happean? – Dillinger May 11, 2016 at 15:13

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.