使用AvalonDock和Caliburn Micro

时间:2016-01-01 19:00:51

标签: wpf vb.net mvvm caliburn.micro avalondock

我尝试将AvalonDock 2.8与Caliburn Micro 2.0.2一起使用。这是我现在拥有的:

MainView.xaml:

<Window x:Class="MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:cal="http://www.caliburnproject.org"
        xmlns:xcad="http://schemas.xceed.com/wpf/xaml/avalondock"
        xmlns:local="clr-namespace:CaliburnMicroAndAvalonDock"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="40"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Button x:Name="Open" Content="Open new document" Margin="5"/>

        <xcad:DockingManager Grid.Row="1" DocumentsSource="{Binding Items}">
            <xcad:DockingManager.LayoutItemContainerStyle>
                <Style TargetType="{x:Type xcad:LayoutItem}">
                    <Setter Property="Title" Value="{Binding Model.DisplayName}" />
                </Style>
            </xcad:DockingManager.LayoutItemContainerStyle>

            <xcad:DockingManager.LayoutItemTemplateSelector>
                <local:LayoutItemTemplateSelector>
                    <local:LayoutItemTemplateSelector.Template>
                        <DataTemplate>
                            <ContentControl cal:View.Model="{Binding .}" IsTabStop="False"/>
                        </DataTemplate>
                    </local:LayoutItemTemplateSelector.Template>
                </local:LayoutItemTemplateSelector>
            </xcad:DockingManager.LayoutItemTemplateSelector>

            <xcad:LayoutRoot>
                <xcad:LayoutPanel Orientation="Horizontal">
                    <xcad:LayoutDocumentPane/>
                </xcad:LayoutPanel>
            </xcad:LayoutRoot>
        </xcad:DockingManager>
    </Grid>
</Window>

MainViewModel.vb:

Imports Caliburn.Micro

Public Class MainViewModel
  Inherits Conductor(Of Screen).Collection.OneActive

  Private m_Index As Int32 = 0

  Sub Open()
    ActivateItem(New DocumentViewModel With {.DisplayName = $"Document {m_Index}"})
    m_Index += 1
  End Sub

End Class

Document.xaml:

<UserControl x:Class="DocumentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:CaliburnMicroAndAvalonDock"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBlock Text="{Binding DisplayName}"/>
    </Grid>
</UserControl>

DocumentViewModel.vb:

Imports Caliburn.Micro

Public Class DocumentViewModel
  Inherits Screen

End Class

LayoutItemTemplateSelector.vb:

Public Class LayoutItemTemplateSelector
  Inherits DataTemplateSelector

  Public Property Template As DataTemplate

  Public Overrides Function SelectTemplate(item As Object, container As DependencyObject) As DataTemplate
    Return Me.Template
  End Function

End Class

部分可行,问题是视图无法加载Cannot find view for System.Windows.Controls.ContentPresenter错误:

Error

我对Caliburn Micro和AvalonDock都很陌生,所以我的代码很大程度上基于我在网上找到的例子(例如this)。但这些样本相当陈旧,并且它不能与当前版本的AvalonDock(2.8.15465.16500)一起使用。如果我切换到较旧的(2.0.2000),它可以正常工作。

1 个答案:

答案 0 :(得分:2)

我设法在this answerthis explanation的帮助下解决了这个问题。 这是LayoutItemTemplateSelector.vb的更新代码:

Public Class LayoutItemTemplateSelector
  Inherits DataTemplateSelector

  Public Property Template As DataTemplate

  Public Overrides Function SelectTemplate(item As Object, container As DependencyObject) As DataTemplate
    If TypeOf item Is DocumentViewModel Then
      Return Me.Template
    Else
      Return MyBase.SelectTemplate(item, container)
    End If
  End Function

End Class