How can I pass an instance of some object, to a Xamarin Forms custom render
?
this is the custom renderer...
public class LoginPageRenderer : PageRenderer
{
public override void ViewDidAppear (bool animated)
{
....
}
}
and this is what I want to do.... (notice the ctor has been added ...)
public class LoginPageRenderer : PageRenderer
{
private SomeFoo _someFoo;
public LoginPageRenderer(MyFoo someFoo)
{
_someFoo = someFoo;
}
public override void ViewDidAppear (bool animated)
{
....
}
}
Finally, this is where this view is called (in some other part of the code).
await _navigationPage.Navigation.PushModalAsync(new LoginPage());
If you define a publicly accessible property in your LoginPage
object you can reference it in the Renderer
using Element.NameOfYourProperty
syntax.
Sample code ...
LoginPage.cs (in your PCL common project).
namespace Foo
{
public class LoginPage : ContentPage
{
private readonly Foo _foo;
public LoginPage(Foo foo)
{
_foo = foo;
}
public Foo Foo { get; private set; }
}
}
LoginPageRenderer.cs (in your iOS project)
[assembly: ExportRenderer (typeof (LoginPage), typeof (LoginPageRenderer))]
namespace Foo.iOS
{
public class LoginPageRenderer : PageRenderer
{
private Foo Foo
{
get
{
var loginPage = Element as LoginPage;
return loginPage == null
? null
: loginPage.Foo;
}
}
public override void ViewDidAppear (bool animated)
{
if (string.IsNullOrWhitespace(Foo.SecretName))
{ ... }
...
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With