0

ビューに設定PartCreationPolicy.NonSharedしましたが、一部の特定のユーザーCreationPolicy.Sharedに対して (パフォーマンスの向上のために) を使用できますが、それを実行できるかどうかはわかりません。ServiceLocatorビューのインスタンスを取得するために使用しています。お気に入り

view = ServiceLocator.GetInstance<MyUserControl>("view_contract");

これに対する最善の解決策は何でしょうか。Googleで検索しましたが、 でいくつかの解決策が見つかりましCompositionContainer.GetExportsたが、

1- CompositionContainerViewModel でインスタンスを取得できません。

2-この投稿では、 GetExportの下に書かれています

エクスポートの値を連続して呼び出すと、パーツのライフタイムが共有か非共有かに関係なく、同じインスタンスが返されます。

誰かが最善の解決策とそのためのコードスニペットの例を提案できますか?

4

1 に答える 1

2

私が理解しているように、共有ビューと非共有ビューを区別するための「ビジネスロジック」があります(たとえば、ユーザーのタイプに応じて)。これは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
}
于 2013-02-20T12:50:40.600 に答える