Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Custom Renderer with Constructor Injection

$
0
0

Hi,

We are using TinyIoC for DI, and I need to inject a dependency into my Custom Renderers in order to get resources:


[assembly: ExportRenderer(typeof(Entry), typeof(CustomEntryRenderer))]
namespace MyNamespace.CustomRenderers
{
public class CustomEntryRenderer : EntryRenderer
{
private readonly IResourceManager _resourceManager;

    //public CustomEntryRenderer()
    //{

    //}

    public CustomEntryRenderer(IResourceManager resourceManager)
    {
        _resourceManager = resourceManager;
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        SetFont();
    }

    private void SetFont()
    {
        Control.Font = UIFont.FromName(_resourceManager.Get<string>("GlobalFont"), _resourceManager.Get<int>("TextBoxFontSize"));
        Control.TextColor = UIColor.Red;
    }
}

}

However, this code throws an exception:

System.MissingMethodException: Default constructor not found for type MyNamespace.CustomRenderers.CustomEntryRenderer

If I add a default constructor, my _resourceManager is of course null. If I remove the IoC constructor and hardcode the font and fontsize, everything works great.

Any ideas?

Thanks,

Peter.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>