2016-12-10 26 views
-1
public ICommand OpenDialogFile 
     { 
      get 
      { 
       return new DelegateCommand<RichEditBox>(OpenDialogToAttach); 
      } 
     } 

Command="{Binding OpenDialogFile}" CommandParameter="{Binding ElementName=TweetEditBox}" 

したがって、DeledateCommandはRichEditBoxについて理解しています。私は "非ジェネリック型 'DelegateCommand'は型引数で使用できません

OpenDialogToAttach(RichEditBox editBox)"を使用しています。

どのように解決しますか。私はUWPの開発者です。ここで

はDelegateCommandは、使用しているDelegateCommandクラスの実装は、あなたがそれに渡すしようとしている一般的な引数をサポートしていません

internal class DelegateCommand : ICommand 
{ 
    private readonly Action _execute; 

    private readonly Func<bool> _canExecute; 

    /// <summary> 
    /// Initializes a new instance of the RelayCommand class that 
    /// can always execute. 
    /// </summary> 
    /// <param name="execute">The execution logic.</param> 
    /// <exception cref="ArgumentNullException">If the execute argument is null.</exception> 
    public DelegateCommand(Action execute) 
     : this(execute, null) 
    { 
    } 

    /// <summary> 
    /// Initializes a new instance of the RelayCommand class. 
    /// </summary> 
    /// <param name="execute">The execution logic.</param> 
    /// <param name="canExecute">The execution status logic.</param> 
    /// <exception cref="ArgumentNullException">If the execute argument is null.</exception> 
    public DelegateCommand(Action execute, Func<bool> canExecute) 
    { 
     if (execute == null) 
     { 
      throw new ArgumentNullException("execute"); 
     } 

     _execute = execute; 
     _canExecute = canExecute; 
    } 

    /// <summary> 
    /// Occurs when changes occur that affect whether the command should execute. 
    /// </summary> 
    public event EventHandler CanExecuteChanged; 

    /// <summary> 
    /// Raises the <see cref="CanExecuteChanged" /> event. 
    /// </summary> 
    [SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate", 
     Justification = "This cannot be an event")] 
    public void RaiseCanExecuteChanged() 
    { 
     var handler = CanExecuteChanged; 
     if (handler != null) 
     { 
      handler(this, EventArgs.Empty); 
     } 
    } 

    /// <summary> 
    /// Defines the method that determines whether the command can execute in its current state. 
    /// </summary> 
    /// <param name="parameter">This parameter will always be ignored.</param> 
    /// <returns>true if this command can be executed; otherwise, false.</returns> 
    [DebuggerStepThrough] 
    public bool CanExecute(object parameter) 
    { 
     return _canExecute == null ? true : _canExecute(); 
    } 

    /// <summary> 
    /// Defines the method to be called when the command is invoked. 
    /// </summary> 
    /// <param name="parameter">This parameter will always be ignored.</param> 
    public void Execute(object parameter) 
    { 
     if (CanExecute(parameter)) 
     { 
      _execute(); 
     } 
    } 
} 
+0

DelegateCommandはあなたが使っていますか?翔インポート文 –

+0

'公共DelegateCommandは { (アクションは、のFunc canExecuteを実行する)場合(実行== nullの) { スロー新しい例外ArgumentNullException( "実行します")。 } _execute = execute; _canExecute = canExecute; } ' –

+0

私はコンストラクタを表示していますが、ステートメントは使用していません。だから、これはあなたのクラスです...あなたはその身体を質問に加える必要があります。私はあなたのクラスはgeneric –

答えて

0

クラスDelegateCommandは一般的ではないため、タイプパラメータRichEditBoxでインスタンス化することはできません。

あなたは便利たとえば、あなたが必要とするコンテキストを取得することにラムダを渡すことができるように、アクションを受け取るコンストラクタを持っている:

return new DelegateCommand(x => { 
    RichEditBox richBox = (RichEditBox) x; 
    OpenDialogToAttach()... 
}); 
+0

ありがとうございます。あなたは.... –

+0

残念ながら私は悪い、私はそれがアクションを受け入れることに気付いたので、パラメータを持つことはできません。 –

+0

コードにRichEditBoxのインスタンスがありますか?もしそうなら、何も渡す必要はありません –

0

ことDelegateCommandクラスの新しいインスタンスをCode.Initializesです。

return new DelegateCommand<RichEditBox>(OpenDialogToAttach) 

あなたがMicrosoft.Practices.Composite.Presentation.CommandsからDelegateCommandクラスの実装を使用することができます。それは一般的な引数を持つ実装です。

+0

ナゲットから来たか、どこにありますか?Microsoft.Practices.Composite.Presentation.Commands。 –

+0

[こちら](https://www.nuget.org/packages/Prism.Core/6.3.0-pre1)はコアのナゲットパッケージです。これがMicrosoftのパッケージと同じ実装であるかどうかはわかりません。おそらく[this](https://www.nuget.org/packages/Prism.Wpf/6.3.0-pre1)パッケージも必要です。 – NtFreX

0

あなたはGitHubの上のUWPのための一般的なDelegateCommandクラスのプリズムの実装を見つけます: https://github.com/PrismLibrary/Prism/blob/master/Source/Prism/Commands/DelegateCommand%7BT%7D.cs

独自の実装をこれに置き換えることができます。つまり、クラス定義をプロジェクトにコピーすることができます。あなたも、基本クラスが必要になります。https://github.com/PrismLibrary/Prism/blob/master/Source/Prism/Commands/DelegateCommandBase.cs

代わりにあなたがNuGet使用Prism.Windowsパッケージをインストールすることができます([ツール] - > [Nugetパッケージマネージャ - >をVisual Studioでのパッケージマネージャコンソール):https://www.nuget.org/packages/Prism.Windows/

...とあなたのコードで直接DelegateCommandを使用してください:

public ICommand OpenDialogFile 
    { 
     get 
     { 
      return new Prism.Commands.DelegateCommand<RichEditBox>(OpenDialogToAttach); 
     } 
    } 
関連する問題