2011-11-22 9 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: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> 

...別のSampleDataをその後、私の他のサンプルではこれを含めることができるよう、従業員のリストが含まれています(SampleEmployees.xaml)を提出したいです別のXAMLファイル??

のViewModel:これは簡単で、いくつかのケースでは

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

基本的に私はこれをMicrosoft Connectの提案に変える必要がありますか? –

+0

それに行く。VS2011はデベロッパーのプレビューにあり、Blend 5は似たような段階にあるので、まだサポートしていないのであれば、この機能を実装することはできません。 –

答えて

0

は、それらが必要になります。

  1. コレクションプロパティのタイプがIEnumerableをであるかのIList(ないクラスがそれを実装する)
  2. プロパティセッターがあります。

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を使用してインスタンスを作成できます。

あなたが直接あなたがアイテムを必要とする場所にこの拡張を介して、たとえば資源のObservableCollectionかパイプ配列を作成することができ
[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; 
    } 
} 

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> 

`1タイプが一般的なので、om:ObservableCollectionの最後に必要です。

+0

私はチャンスを得るときにこれを試してみる必要があります...しかし、それには少しのにおいがあります。私は、サンプルデータをウィンドウリソースに組み込む必要はないと思っています。 –

+0

@ m-y:上記のマークアップ拡張を拡張したり、他のコード内のリソース辞書へのハードリファレンスなしでその場でリソースを取得する別のものを作成することができます。 –

関連する問題