如何将对象传递给Xamarin Forms自定义渲染器? [英] How to pass in an object to a Xamarin Forms custom renderer?
问题描述
如何将某个对象的实例传递给Xamarin表单custom render
?
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)
{
....
}
}
这就是我想要做的....(注意已添加了ctor ...)
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());
推荐答案
如果在LoginPage
对象中定义了可公开访问的属性,则可以使用Element.NameOfYourProperty
语法在Renderer
中引用该属性.
If you define a publicly accessible property in your LoginPage
object you can reference it in the Renderer
using Element.NameOfYourProperty
syntax.
示例代码...
LoginPage.cs (在您的PCL公共项目中).
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 (在您的iOS项目中)
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))
{ ... }
...
}
}
}
这篇关于如何将对象传递给Xamarin Forms自定义渲染器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!