将 AvalonDock 与 Caliburn Micro 结合使用

Using AvalonDock with Caliburn Micro

我正在尝试将 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 错误:

我对 Caliburn Micro 和 AvalonDock 都很陌生,所以我的代码很大程度上基于我在网上找到的示例(例如 this)。但是这些样本很旧,不适用于当前版本的 AvalonDock (2.8.15465.16500)。如果我切换到旧版本 (2.0.2000),它工作正常。

and this 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