3

Do anyone know why being specific with the MVVM Light RelayCommand generic type would cause its canExecute to always resolve to false for the binding? In order to get the correct behavior I had to use an object and then convert it to the desired type.

NOTE: canExecute was simplified to a boolean for testing the block that does not work and is normally a property CanRequestEdit.

Does NOT work:

public ICommand RequestEditCommand {
  get {
    return new RelayCommand<bool>(commandParameter => { RaiseEventEditRequested(this, commandParameter); },
                                  commandParameter => { return true; });
  }
}

Works:

public ICommand RequestEditCommand {
  get {
    return new RelayCommand<object>(commandParameter => { RaiseEventEditRequested(this, Convert.ToBoolean(commandParameter)); },
                                    commandParameter => { return CanRequestEdit; });
  }
}

XAML:

<MenuItem Header="_Edit..." Command="{Binding RequestEditCommand}" CommandParameter="true"/>

How can i get the Process used to open a file in C#

I am trying to open different files with their associated applications like this:

                ProcessPath = @"C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg";
                ProcessStartInfo processStartInfo = new ProcessStartInfo();
                processStartInfo.FileName = ProcessPath;
                processStartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(ProcessPath);

                process = new Process();
                process.StartInfo = processStartInfo;
                process.EnableRaisingEvents = true;
                process.Exited += Process_Exited;
                process.Start();     

Please can you help me get hold of the Process object that represents the application opened because after the call to process.Start(); most of the fields inside process have threw exceptions.

Thanks.

4

1 回答 1

2

查看的代码RelayCommand<T>,特别是我用“!!!”标记的行:

public bool CanExecute(object parameter)
{
    if (_canExecute == null)
    {
        return true;
    }

    if (_canExecute.IsStatic || _canExecute.IsAlive)
    {
        if (parameter == null
#if NETFX_CORE
            && typeof(T).GetTypeInfo().IsValueType)
#else
            && typeof(T).IsValueType)
#endif
        {
            return _canExecute.Execute(default(T));
        }

        // !!!
        if (parameter == null || parameter is T)
        {
            return (_canExecute.Execute((T)parameter));
        }
    }

    return false;
}

您传递给命令的参数是字符串“true”,而不是boolean true,因此条件将失败,因为parameteris notnull并且is子句为 false。换句话说,如果参数的值与T命令的类型不匹配,则返回false.

如果您真的想将布尔值硬编码到您的 XAML 中(即您的示例不是虚拟代码),那么请查看此问题以了解如何执行此操作。

于 2016-03-21T17:29:20.203 回答