私は、よく知られている Calculator の例の独自のバージョンを実装することにより、MEF フレームワークの操作をマスターしようとしています。ユーザー インターフェイスは WPF です。
構成後、Viewmodel はビュー内ObservableCollection(Of IOperation)
の「ListBox」によって表される を保持Buttons
します。それぞれのテキストButton
は、という名前のプロパティとしてChar
定義されています。ListBox の SelectedItem を ViewModel のプロパティにバインドすること で、どちらが押されたかを知らなくても、現在選択されているメソッドを起動できます。(以下にこれを示すコード。)IOperation
Symbol
Calculate
IOperation
Button
ただし、InputBindings
ここで View に追加する必要があります。ここで、それぞれがで定義されているKeyBinding
に関連付けられます。( ) ステートメントを実装して Viewmodel のs のコレクションを反復処理し、「Calculate」メソッドを呼び出す必要があるものを選択することは避けられないようです。Symbol
IOperation
Select Case
switch
IOperation
何か案は?
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