Search code examples
xamlxamarinxamarin.forms

Constructor with parameters for XAML elements in Xamarin Forms


Is it possible to create XAML element with parametrized constructor using dependency injection service? Or is there some other way to pass dependencies? I would like to have Behavior with some dependency services. I thought the DependencyResolver.ResolveUsing is going to solve my problem, but this method only allows to resolve special types of objects like renderers or effects. I can bind to viewmodel properties but this will produce large xaml instead of just simple <SomeBehavior/>


Solution

  • No answer satisfied my needs so I share my solution. I use StaticResources (Application.Current.Resources) as ServiceLocator. This way I can access required services from my behavior without boilerplate code. Yes, I know it is not a perfect solution, but seems the best in my situation.