0

私は、よく知られている Calculator の例の独自のバージョンを実装することにより、MEF フレームワークの操作をマスターしようとしています。ユーザー インターフェイスは WPF です。

構成後、Viewmodel はビュー内ObservableCollection(Of IOperation)の「ListBox」によって表される を保持Buttonsします。それぞれのテキストButtonは、という名前のプロパティとしてChar定義されています。ListBox の SelectedItem を ViewModel のプロパティにバインドすること で、どちらが押されたかを知らなくても、現在選択されているメソッドを起動できます。(以下にこれを示すコード。)IOperationSymbolCalculateIOperationButton

ただし、InputBindingsここで View に追加する必要があります。ここで、それぞれがで定義されているKeyBindingに関連付けられます。( ) ステートメントを実装して Viewmodel のs のコレクションを反復処理し、「Calculate」メソッドを呼び出す必要があるものを選択することは避けられないようです。SymbolIOperationSelect CaseswitchIOperation

何か案は?

XAML:

<ListBox Grid.Column="1" Grid.Row="3" Name="OperationsList" 
         SelectedItem="{Binding ActiveOperation,Mode=TwoWay}"
         ItemsSource="{Binding Operations}">
         <ListBox.ItemsPanel>
             <ItemsPanelTemplate>
                 <UniformGrid IsItemsHost="True"/>
             </ItemsPanelTemplate>
         </ListBox.ItemsPanel>
         <ListBox.ItemTemplate>
             <DataTemplate>
                 <Button Content="{Binding Symbol}"
                         ToolTip="{Binding Description}"
                         Command="{Binding ElementName=OperationsList, Path=DataContext.ActivateOperation}"
                            Click="Button_Click_1"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

操作:

Public Interface IOperation
    Function Calculate() As Double
    Property Left As Double
    Property Right As Double
    Property Symbol As String
    Property Description As String
End Interface

ビューモデル:

Private _activateOperation As Command
    Public Property ActivateOperation As Command
        Get
            Return _activateOperation
        End Get
        Set(value As Command)
            _activateOperation = value
            OnPropertyChanged()
        End Set
    End Property

Public Property ActiveOperation As IOperation
    Get
        Return _compositor.ActiveOperation
    End Get
    Set(value As ICalculator)
        _compositor.ActiveOperation = value
        OnPropertyChanged()
    End Set
End Property

   Public ReadOnly Property Operations As ObservableCollection(Of IOperation)
        Get
            Return New ObservableCollection(Of ICalculator)(_compositor.Operations)
        End Get
    End Property

   Private Sub DoActivateOperation(Optional parameter As Object = Nothing)
        If Left.HasValue Then
            MakeCalculation()
        Else
            Left = CDbl(Display)
            ClearDisplay()
        End If
    End Sub
4

1 に答える 1

1

コードは端が少し荒いので、それに応じて調整してください...

// in ViewModel, after composition

// map string symbols to input Keys (for brevity, using a Dictionary)
Dictionary<string, System.Windows.Input.Key> SymbolsToKeys = new Dictionary<string, Key>
{
  { "+", Key.Add },
  // etc.
}

// compose the list (expose it via a public property)
// (ensure not to get confused: the KeyValuePair.Key is for the string symbol
// ... and the KeyValuePair.Value is for the Sys.Win.Input.Key value)
KeyBindings = (from symbolAndKey in SymbolsToKeys
               join op in Operations on Equals(symbolAndKey.Key, op.Symbol)
               select new KeyBinding(
                 new Command(op.Calculate)),
                 new KeyGesture(symbolAndKey.Value)
              ).ToList();

// in View, after Binding to ViewModel
var vm = DataContext as YourViewModel;
if(vm != null)
{
   foreach(var keybinding in vm.KeyBindings){
       this.InputBindings.Add(keybinding);
   }
}
于 2013-01-03T20:10:29.987 に答える