簡體   English   中英

將參數傳遞給PageRenderer Prism Xamarin Forms

[英]Passing parameter to PageRenderer Prism Xamarin Forms

我正在嘗試使用Xamarin Form和Prism創建身份驗證過程。 我為每個平台創建了PageRenderer類,我需要將自定義參數傳遞給它。 嘗試在ViewModel中使用INavigationAware接口,然后從自定義頁面訪問它,但是在PageRenderer啟動后調用了OnNavigatedTo方法,因此導航參數仍然為空。

誰能為這個問題提出解決方案?

有幾種方法可以解決此問題:

1)根據建議,設置頁面INavigationAware並設置要從PageRenderer訪問的屬性。 根據發送的內容,這當然可能會破壞MVVM模式。 但是您可以執行以下操作:

創建一個界面

public interface IMyPage
{
    string Message { get; set; }
}

設置您的ViewModel以處理您需要發送到Renderer的參數

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();
    }
}

設置頁面以實現您的界面並在頁面上創建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標記中,將ViewModel的屬性綁定到Bindable屬性

<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>

最后在渲染器中處理對屬性的更改

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
    }
}

編輯:如果您需要在初始化時進行設置,請記住您可以從渲染器訪問Container並注入Interface或Concrete類型以訪問所需的屬性。 我將以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
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM