私が知る限り、現在PRISMは文字列を渡すことができますが、オブジェクトを渡すことはできません。私はこの問題を克服する方法が何であるかを知りたい。PRISMの新しいビューにナビゲートするときにオブジェクトを渡す方法は?
私はリストコレクションを渡したいと思います。私の場合、UriQueryは役に立ちません。私はこの場合何をすべきですか?
私が知る限り、現在PRISMは文字列を渡すことができますが、オブジェクトを渡すことはできません。私はこの問題を克服する方法が何であるかを知りたい。PRISMの新しいビューにナビゲートするときにオブジェクトを渡す方法は?
私はリストコレクションを渡したいと思います。私の場合、UriQueryは役に立ちません。私はこの場合何をすべきですか?
私は自分の技術を持っています。
オブジェクトのハッシュコードを抽出し、ハッシュコードをキーとして、オブジェクトをペアの値としてDictionary
に保存します。
次に、UriQuery
にハッシュコードを添付します。
その後、対象のビューにあるUriのハッシュコードを取得し、それを使用して元のオブジェクトをDictionary
から要求する必要があります。
いくつかのサンプルコード:
パラメータリポジトリクラス:
public class Parameters
{
private static Dictionary<int, object> paramList =
new Dictionary<int, object>();
public static void save(int hash, object value)
{
if (!paramList.ContainsKey(hash))
paramList.Add(hash, value);
}
public static object request(int hash)
{
return ((KeyValuePair<int, object>)paramList.
Where(x => x.Key == hash).FirstOrDefault()).Value;
}
}
発信者コード:
UriQuery q = null;
Customer customer = new Customer();
q = new UriQuery();
Parameters.save(customer.GetHashCode(), customer);
q.Add("hash", customer.GetHashCode().ToString());
Uri viewUri = new Uri("MyView" + q.ToString(), UriKind.Relative);
regionManager.RequestNavigate(region, viewUri);
ターゲットビューコード:
public partial class MyView : UserControl, INavigationAware
{
// some hidden code
public void OnNavigatedTo(NavigationContext navigationContext)
{
int hash = int.Parse(navigationContext.Parameters["hash"]);
Customer cust = (Customer)Parameters.request(hash);
}
}
それです。
'object' getter/setterを使用してPRISMイベントを作成できます。イベント内でオブジェクトがキャストされているかキャストされていないかのイベントを発生させます(有名な「インフラストラクチャ」プロジェクトのようなイベントの実装が「共有」されているかどうかによって異なります)。上記のイベントにRegion - Subscribe()を実装しているViewModelでは、それを受け取ってローカルに格納してから、 'OnNavigatedTo'関数呼び出しを待つだけです。 OnNavigatedTo関数が呼び出されると、オブジェクト/ class/structが既に存在し、ViewModelを実行できます。例えば
- イベントクラス:
namespace CardManagment.Infrastructure.Events
{
using Microsoft.Practices.Prism.Events;
/// <summary>
/// Event to pass 'Selected Project' in between pages
/// </summary>
public class SelectedProjectViewEvent : CompositePresentationEvent<SelectedProjectViewEvent>
{
public object SelectedPorject { get; set; }
}
}
'呼び出し' クラス
/// <summary>
/// Called when [back to project view].
/// </summary>
/// <param name="e">The e.</param>
public void OnBackToProjectView(CancelEditProjectEvent e)
{
eventAggregator.GetEvent<SelectedProjectViewEvent>().Publish(new SelectedProjectViewEvent()
{
SelectedPorject = selectedProject
});
regionManager.RequestNavigate(WellKnownRegionNames.ProjectViewRegion, new System.Uri("ProjectDetailsView", System.UriKind.Relative));
}
そして、この 'レシーバー' クラスの
/// <summary>
/// Called when the implementer has been navigated to.
/// </summary>
/// <param name="navigationContext">The navigation context.</param>
public void OnNavigatedTo(NavigationContext navigationContext)
{
if (this.SelectedProject == null) // <-- If event received untill now
this.ShouldBeVisible = false;
else
this.ShouldBeVisible = true;
}
あなたは渡す方法もをチェックアウトすることができますIOCを使用していてコンストラクタインジェクションを使用したい場合は、オブジェクトを使用します。
プリズム5及び6:NavigationParametersクラスは、現在の領域またはRegionManagerインスタンスのRequestNavigateメソッドのオーバーロードを使用して、ナビゲーション中にオブジェクトパラメータを渡すために使用することができます。
ありがとうございましたDiegoさんのソリューションです。これが私が探しているものです。 –
ちょうど1つのこと、あなたはどう思いますか?パラメータは静的なクラスかサービスではありませんでしたか? –
。これは、私が機能の作業をしようとしていたときの回避策でした。どんな種類の改善も大歓迎です。この例を改善する場合は、ここでそれを共有してください。;)bye –