2011-11-22 69 views
5

我有以下的樣本數據,其工作地非常好......在Expression Blend中重用設計數據?

<SampleData:DashboardViewModel xmlns:SampleData="clr-namespace:MyApp.ViewModels"> 
    <SampleData:DashboardViewModel.Employees> 
     <SampleData:EmployeeViewModel FirstName="Aaron" "Adams" /> 
     <SampleData:EmployeeViewModel FirstName="Billy" "Bob" /> 
     <SampleData:EmployeeViewModel FirstName="Charlie" "Chaplin" /> 
    </SampleData:DashboardViewModel.Employees> 
</SampleData:DashboardViewModel> 

不過,我覺得這將是能夠重用樣品的員工是列表,而不是每次都重新鍵入它是有用的。我無法弄清楚如何重用這個列表。基本上,我想有一個包含員工該列表,然後能夠包括我的其他樣本中另一個的sampleData文件(SampleEmployees.xaml)...

<SampleData:DashboardViewModel xmlns:SampleData="clr-namespace:MyApp.ViewModels"> 
    <SampleData:DashboardViewModel.Employees ... /> <!-- What goes in there? --> 
</SampleData:DashboardViewModel> 

<SampleData:OtherViewModel xmlns:SampleData="clr-namespace:MyApp.ViewModels"> 
    <SampleData:OtherViewModel.Employees ... /> <!-- What goes in there? --> 
</SampleData:OtherViewModel> 

此外,如何在單獨創建列表另一個XAML文件?

視圖模型:

public class DashboardViewModel : NotificationObject 
{ 
    public class DashboardViewModel(IDataService dataService) 
    { 
     InternalEmployees = new ObservableCollection<EmployeeViewModel>(dataService.GetEmployees()); 
     Employees = new ReadOnlyObservableCollection<EmployeeViewModel>(InternalEmployees); 
    } 

    private ObservableCollection<EmployeeViewModel> InternalEmployees { get; set; } 
    public ReadOnlyObservableCollection<EmployeeViewModel> Employees { get; private set; } 
} 
+0

我不認爲這是可能的默認系統。我認爲必須創建一個[CustomTool](http://www.google.com/search?q=visual+studio+custom+tool)來解析源文件,然後生成另一個設計數據文件。這將防止重新輸入,但生成的生成文件仍然包含完整的數據(不是對其他數據的「引用」)。 –

+0

所以基本上我需要把它變成微軟連接的建議? –

+0

去吧。注意VS2011在dev預覽中,而Blend 5處於類似的階段,所以如果他們不支持它,我不會預見他們實現這個功能... –

回答

0

在某些情況下,這是很容易,那些要求:

  1. 集合屬性是IEnumerable類型或IList的(不是類實現它)
  2. 酒店有一個setter。

例如public IEnumerable Employees { get; set; }

首先將項目提取到資源字典中,例如

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:obj="clr-namespace:Test.Objects"> 
    <x:Array x:Key="SampleEmps" Type="obj:Employee"> 
     <obj:Employee Name="Skeet" Occupation="Programmer" /> 
     <obj:Employee Name="Skeet" Occupation="Programmer" /> 
     <obj:Employee Name="Dimitrov" Occupation="Programmer" /> 
    </x:Array> 
</ResourceDictionary> 

然後,您將其添加到將包含ViewModels的控件的MergedDictionary。例如

<Window.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Objects/SampleData.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
     <!-- ... --> 

然後你就可以參考使用StaticResource集合:

<obj:SomeOtherViewModel Employees="{StaticResource SampleEmps}"/> 

現在有了專門收藏的問題是,你不能簡單地用XAML創建它們。而缺少二傳手的問題是你不能使用StaticResource來分配財產,所以我認爲一個二傳手總是必要的。

如果您有專門的集合,則可以使用MarkupExtension來創建實例。

[ContentProperty("Items")] 
public class GenericCollectionFactoryExtension : MarkupExtension 
{ 
    public Type Type { get; set; } 
    public Type T { get; set; } 
    public IEnumerable Items { get; set; } 

    public override object ProvideValue(IServiceProvider serviceProvider) 
    { 
     var genericType = Type.MakeGenericType(T); 
     var list = Activator.CreateInstance(genericType) as IList; 
     if (list == null) throw new Exception("Instance type does not implement IList"); 
     foreach (var item in Items) 
     { 
      list.Add(item); 
     } 
     return list; 
    } 
} 

您可以直接在資源創建例如一個ObservableCollection或你管通過這個擴展的地方數組,你需要的物品:

xmlns:om="clr-namespace:System.Collections.ObjectModel;assembly=System" 
<obj:SomeViewModel x:Key="SomeVM"> 
    <obj:SomeViewModel.Employees> 
     <me:GenericCollectionFactory Type="{x:Type om:ObservableCollection`1}" 
            T="{x:Type obj:Employee}"> 
      <StaticResource ResourceKey="SampleEmps" /> 
     </me:GenericCollectionFactory> 
    </obj:SomeViewModel.Employees> 
</obj:SomeViewModel> 

`1om:ObservableCollection的末尾是必要的,因爲該類型是通用的。

+0

當我有機會的時候,我將不得不嘗試這個...雖然它確實有點味道。我不喜歡將我的示例數據構建到窗口資源中的想法。 –

+0

@ m-y:您可以擴展上面的標記擴展,或者創建另一個在其他代碼中沒有對其他代碼中的資源字典的硬引用的情況下即時獲取資源。 –