5

Prismを使用してアプリを作成していて、少し障害が発生しています。

プリズム領域であるタブコントロールがあり、モデルをバインドするItemsSourceコレクションがあります。このモデルには、タブのキャプション、ビュー名、およびランダムID(この場合はGuid)を設定するために必要なデータが含まれています。これらのデータを使用して、動的プリズム領域を生成し、独自のプリズム領域を持つタブコンテンツのコンテナーにします。に移動します。

+Prism region(tab control)
|+ Prism region (dynamically created with Guid name)
 |+ Inserted view
  |+ Prism region
  |+ Prism region
  |+ Prism region
|+ Prism region (dynamically created with Guid name)    |
 |+ Inserted view
  |+ Prism region
  |+ Prism region
  |+ Prism region

私が遭遇している問題は、生成しようとしているリージョンが正しく登録されているように見えても、リージョンマネージャー内に登録されていないように見えることです。

チームの周りの一連の検索と質問は、残念ながら実用的な解決策をもたらしませんでした。

誰かがこれを以前に試したことがあるのか​​、それともコードでプリズム領域を動的に生成して登録するのに良いリソースがあるのだろうかと思います。

4

1 に答える 1

8

私は自分のアプリケーションでこれを行いました。これが私が思いついたコードです:

string regionName = "MyRegionName";
ContentPresenter RegionContentControl = new ContentPresenter { Focusable = false };

// This creates the region
Microsoft.Practices.Prism.Regions.RegionManager.SetRegionName(RegionContentControl, regionName);

// This adds the region to your region manager.
Microsoft.Practices.Prism.Regions.RegionManager.SetRegionManager(RegionContentControl, RegionManager);

// Get the region back
IRegion newRegion = RegionManager.Regions.FirstOrDefault(x => x.Name == regionName);

Unity から RegionManger を取得します。

アップデート:

Mark は、ContentPresenter はそのままでは許可されないことに注意しました。その機能を追加するアダプターは次のとおりです。

public class ContentPresenterRegionAdapter : RegionAdapterBase<ContentPresenter>
{
    public ContentPresenterRegionAdapter(IRegionBehaviorFactory behaviorFactory)
        : base(behaviorFactory)
    {
    }

    protected override void Adapt(IRegion region, ContentPresenter regionTarget)
    {
        region.Views.CollectionChanged += (s, e) =>
            {
                if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    foreach (FrameworkElement element in e.NewItems)
                    {
                        regionTarget.Content = element;
                    }
                }
                else if (e.Action == NotifyCollectionChangedAction.Remove)
                {
                     foreach (FrameworkElement currentElement in e.OldItems)
                         regionTarget.Content = null;
                }
            };
    }

    protected override IRegion CreateRegion()
    {
        return new AllActiveRegion();
    }
}

このマッピングを登録するには、これをブートストラッパーに追加する必要があります。

protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
{
   RegionAdapterMappings mappings = base.ConfigureRegionAdapterMappings();
   mappings.RegisterMapping(typeof(ContentPresenter), 
                            Container.Resolve<ContentPresenterRegionAdapter>());
   return mappings;
}
于 2012-10-29T20:48:51.160 に答える