あなたはこれを扱うことができ、いくつかの方法があります。これを送信していることに応じて、もちろんMVVMパターンを破ることができます。しかし、あなたのような何かを行うことができ、次のとおりです。
は
public interface IMyPage
{
string Message { get; set; }
}
セットアップあなたのインターフェイスを実装するレンダラ
public class MainPageViewModel : BindableBase, INavigationAware
{
private string _message;
public string Message
{
get { return _message; }
set { SetProperty(ref _message, value); }
}
public void OnNavigatedTo(NavigationParameters parameters)
{
Message = parameters[ "message" ].ToString();
}
}
セットアップページに送信する必要があるパラメータを処理するために、あなたのViewModelをインターフェースを作成します。ページにBindablePropertyを作成する
public class MainPage : ContentPage, IMyPage
{
public static readonly BindableProperty MessageProperty = BindableProperty.Create(nameof(Message), typeof(string), typeof(MainPage), string.Empty);
public string Message
{
get { return (string)GetProperty(MessageProperty); }
set { SetProperty(MessageProperty, value); }
}
}
電子XAMLマークアップは、バインド可能なプロパティのプロパティにあなたのレンダラハンドルの変更で最後に
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApplication.Views.MainPage"
Message="{Binding Message}">
</ContentPage>
にあなたのViewModelからプロパティをバインド
public class MyPageRenderer : PageRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
// Do foo
}
}
マインドあなたのより良い方法はただ一つ、ベースページでこれを実装することですContentPageやTabbedPageなどの代わりにページを継承するように入力してから、ページをインポートしてインターフェイスが実装されているかどうかを確認してください。
2)IEventAggregatorを使用して、イベントをリッスンして処理します。
public class MainPageViewModel : INavigationAware
{
IEventAggregator _ea { get; }
public MainPageViewModel(IEventAggregator ea)
{
_ea = ea;
}
public void OnNavigatedTo(NavigationParameters parameters)
{
_ea.GetEvent<MyEvent>().Publish(parameters[ "message" ].ToString());
}
}
public class MyPageRenderer : PageRenderer
{
public MyPageRenderer()
{
var ea = (App as PrismApplication).Container.Resolve<IEventAggregator>();
ea.GetEvent<MyEvent>().Subscribe(OnMyEvent);
}
public void OnMyEvent(string message)
{
// Do what you need to
}
}
EDIT:あなたが何か初期化時に設定することが必要な場合 、あなたのレンダラからコンテナにアクセスし、必要なプロパティまたはプロパティにアクセスするためのインタフェースやコンクリートの種類を注入することができます覚えています。例としてUnityを使用します。これにより、静的なプロパティを使用するよりもはるかにテスト可能なコードになります。
public class App : PrismApplication
{
protected override void RegisterTypes()
{
Container.Register<IMySettings,MySettings>(new ContainerControlledLifetimeManager());
}
}
public class MyPageViewModel
{
IMySettings _mySettings { get; }
public MyPageViewModel(INavigationService navigationService, IMySettings mySettings)
{
_mySettings = mySettings;
// Update this somewhere in the code before navigating to the new page
}
}
public class MyPageRenderer : PageRenderer
{
public MyPageRenderer()
{
var mySettings = (App as PrismApplication).Container.Resolve<IMySettings>();
// Use MySettings to set the property you need to set
}
ちょうどあなたが知っているように、INavigationAwareインターフェイスをページ自体に追加することもできます。私はあなたがやっていることをやってみたことはありませんが、それがまったく役立つかどうかは分かりません。 –
これまでと同じ動作です。私はそれのためにMVVMを壊したくありません。 – matrostik
サンプルやその他の情報がないと、問題の内容を理解することが難しいです。 1)プロセス6.3.0のプロセスが「OnNavigatingTo」になっていることを理解しています - >新しいページをプッシュする - > OnNavigatedFrom' - > 'OnNavigatedTo'(6.2には' OnNavigatingTo'がありません) 2パラメータはPageRendererの開始後に生成されますか? –