私が理解しているように、共有ビューと非共有ビューを区別するための「ビジネスロジック」があります(たとえば、ユーザーのタイプに応じて)。これはDIコンテナで処理すべきではないと思います...
「プリズム スタイル」でこれを達成したい場合はINavigationAware
、ViewModel でプリズム インターフェイスを使用できます。ビューとビューモデルは共有されておらず、ナビゲーションを介してアクティブ化/構築します (MEF と完全に連携します)。「共有」/「非共有」のビジネス ロジックを「IsNavigationTarget」メソッドに入れます。Prism はこのメソッドを自動的に呼び出し、必要な場合にのみ新しいビュー インスタンスを作成します。
ここにいくつかのコードがあります:
ビュー (ナビゲーション ターゲットとしてビュー名を忘れないでください!):
[Export(Constants.ViewNames.MyFirstViewName)] // The Export Name is only needed for Prism's view navigation.
[PartCreationPolicy(CreationPolicy.NonShared)] // there may be multiple instances of the view => NO singleton!!
public partial class MyFirstView
{ ... }
ビューモデル:
[PartCreationPolicy(CreationPolicy.NonShared)]
[Export]
public class MyFirstViewModel: Microsoft.Practices.Prism.Regions.INavigationAware
{
#region IINavigationAware
// this interface ships with Prism4 and is used here because there may be multiple instances of the view
// and all this instances can be targets of navigation.
/// <summary>
/// Called when [navigated to].
/// </summary>
/// <param name="navigationContext">The navigation context.</param>
public override void OnNavigatedTo(NavigationContext navigationContext)
{
...
}
/// <summary>
/// </summary>
public override void OnActivate()
{
...
}
/// <summary>
/// Determines whether [is navigation target] [the specified navigation context].
/// </summary>
/// <param name="navigationContext">The navigation context.</param>
/// <returns>
/// <c>true</c> if [is navigation target] [the specified navigation context]; otherwise, <c>false</c>.
/// </returns>
public override bool IsNavigationTarget(NavigationContext navigationContext)
{
// use any kind of busines logic to find out if you need a new view instance or the existing one. You can also find any specific target view using the navigationContext...
bool thereCanBeOnlyOneInstance = ...
return thereCanBeOnlyOneInstance;
}
#endregion
}