如何在Caliburn.Micro中使用DesignInstance

3

我正在使用Caliburn.Micro

我有一个WPF视图,在设计时间成功地使用样本数据来填充基本属性(如名字等),但无法找到复杂类型的属性和集合的视图。

<UserControl x:Class="NonRepositoryItems.Reviewer.ReviewerView"
         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:Controls="clr-namespace:MyFramework.Controls.BusyIndicator.Implementation;assembly=App.Framework"
         xmlns:cal="http://www.caliburnproject.org"
         xmlns:FormPanel="clr-namespace:MyFramework.Controls.FormPanel;assembly=App.Framework"
         xmlns:SampleData="clr-namespace:NonRepositoryItems.SampleData"
         mc:Ignorable="d" 
         d:DataContext="{d:DesignInstance SampleData:SampleReviewerViewModel, IsDesignTimeCreatable=True}"
         >
<Grid>
    <Border>
        <DockPanel>
            <DockPanel.Resources>
                <Style TargetType="Label"  >
                    <Setter Property="FontWeight" Value="Bold" />
                    <Setter Property="FontSize" Value="12" />
                </Style>
                <Style TargetType="TextBlock"  >
                    <Setter Property="VerticalAlignment" Value="Center" />
                </Style>
            </DockPanel.Resources>
            <FormPanel:FormPanel Columns="2" Margin="5" DockPanel.Dock="Top">
                <Label Content="Job Title"/>
                <TextBlock Text="{Binding JobTitle}"  />

                <Label Content="Honorific"/>
                <TextBlock Text="{Binding Honorific}" />

                <Label Content="First Name"/>
                <TextBlock Text="{Binding FirstName}" />

                <Label Content="Last Name"/>
                <TextBlock Text="{Binding LastName}" />

                <Label Content="Gender"/>
                <TextBlock Text="{Binding Gender}"    />

            </FormPanel:FormPanel>

            <FormPanel:FormPanel Columns="1" Margin="5" DockPanel.Dock="Top">
                <Label Content="Postal Address"/>                    
                <ContentControl cal:View.Model="{Binding PostalAddress}" />        
                <Label Content="Courier Address"/>
                <ContentControl cal:View.Model="{Binding CourierAddress}"  />
            </FormPanel:FormPanel>
            <ListView ItemsSource="{Binding RepositoryItems}"></ListView>
        </DockPanel>

    </Border>

    <Controls:BusyIndicator IsBusy="{Binding IsBusy}" BusyContent="Busy..." Grid.ColumnSpan="2"></Controls:BusyIndicator>
    <ContentControl x:Name="Dialogs" 
                    VerticalContentAlignment="Stretch"
                    HorizontalContentAlignment="Stretch"/>
</Grid>
</UserControl>

这是示例数据(SampleData)

public class SampleReviewerViewModel : ReviewerViewModel
{
    public SampleReviewerViewModel()
    {
        Honorific = "Mr";
        FirstName = "John";
        LastName = "Smith";
        ReviewerId = "125634";
        Gender = "Male";
        JobTitle = "REC Chair";
        ReviewerTaskCount = "10";
        ReviewerRequestedTaskCount = "20";
        ReviewerDispatchedTaskCount = "33";
        ReviewerRequestedReturnedCount = "50";
        PostalAddress = new AddressViewModel();
        CourierAddress = new AddressViewModel();
        IsBusy = false;
        RepositoryItems = new ObservableCollection<RepositoryItemViewModel>
                        {
                            new RepositoryItemViewModel() {Title = "Red"      },
                            new RepositoryItemViewModel() {Title = "Orange"   },
                            new RepositoryItemViewModel() {Title = "Yellow"   },
                            new RepositoryItemViewModel() {Title = "Green"    },
                            new RepositoryItemViewModel() {Title = "Blue"     },
                            new RepositoryItemViewModel() {Title = "Indigo"   },
                            new RepositoryItemViewModel() {Title = "Violet"   }
                        };
    }

}

这是我的地址视图。
<UserControl x:Class="NonRepositoryItems.Reviewer.AddressView"
         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:SampleData="clr-namespace:NonRepositoryItems.SampleData" mc:Ignorable="d" 
          d:DataContext="{d:DesignInstance SampleData:SampleAddressViewModel, IsDesignTimeCreatable=True}"
         >

<Border>
<Grid Margin="4">
    <StackPanel>
            <TextBlock Text="{Binding AddressLine1}"  Visibility="{Binding IsAddressLine1Visible, Converter={StaticResource booleanToVisibility}}"/>
            <TextBlock Text="{Binding AddressLine2}"  Visibility="{Binding IsAddressLine2Visible, Converter={StaticResource booleanToVisibility}}" />
            <TextBlock Text="{Binding AddressLine3}"  Visibility="{Binding IsAddressLine3Visible, Converter={StaticResource booleanToVisibility}}" />
            <TextBlock Text="{Binding City}"          Visibility="{Binding IsCityVisible, Converter={StaticResource booleanToVisibility}}"/>
            <TextBlock Text="{Binding PostCode}"      Visibility="{Binding IsPostCodeVisible, Converter={StaticResource booleanToVisibility}}"/>
            <TextBlock Text="{Binding Country}"       Visibility="{Binding IsCountryVisible, Converter={StaticResource booleanToVisibility}}"/>
    </StackPanel>
</Grid>
</Border>
</UserControl>

还有它的样本数据

public class SampleAddressViewModel : AddressViewModel
{

    public SampleAddressViewModel()
    {
        Type = "Mail Address";
        AddressLine1 = "350 Fifth Avenue";
        AddressLine2 = "";
        AddressLine3 = "";
        City = "New York, NY ";
        PostCode = "10118";
        Country = "United States of America";
    }
}
1个回答

6
你的用户控件不需要cal:Bind.AtDesignTime="True"
<UserControl x:Class="NonRepositoryItems.Reviewer.AddressView"
         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:SampleData="clr-namespace:NonRepositoryItems.SampleData" mc:Ignorable="d" 
          d:DataContext="{d:DesignInstance SampleData:SampleAddressViewModel, IsDesignTimeCreatable=True}"
         cal:Bind.AtDesignTime="True">

我没有那个属性。cal:Bind只有一个Model属性。我应该使用另一个程序集吗?这篇文章似乎是我需要的,但如果有更集成的方法,我会很感兴趣了解 http://mnajder.blogspot.com.au/2011/09/design-time-support-for-caliburnmicro.html - Peter
1
你使用的CM版本是哪个?那篇博客文章中的内容现在已经集成进去了。这里有一个SL示例:https://bitbucket.org/dbeattie/designdata/src - Derek Beattie
我正在使用通过 NuGet v1.2.0 获取的那个。 - Peter
这很奇怪,因为我使用了“管理Nuget包”来获取我的...但是当我安装“Install-Package Caliburn.Micro”时,它会下载最新的版本。 - Peter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接