Make Dependency Injection with Avalonia possible? #16775
Replies: 4 comments 7 replies
-
Most of the time visual elements should not be reused because they cannot be attached to visual tree again and again. Why do you want to do this? |
Beta Was this translation helpful? Give feedback.
-
Hello, there is nothing to do with reuse visual elements. The point is that Avalonia should not create (or resolve) the object If Avalonia let the class MyUserControl(MyUserControlViewModel viewModel);
class MyUserControlViewModel(IMyRepository repositoy);
interface IMyRepository;
class PostgresRepository: IMyRepository; Avalonia doesn't know how to create this kind of
|
Beta Was this translation helpful? Give feedback.
-
Hi. |
Beta Was this translation helpful? Give feedback.
-
This part is actually possible. |
Beta Was this translation helpful? Give feedback.
-
Hello,
Avalonia doesn't support Dependency Injection. You can use whatever DI Container in your Avalonia application but you are on your own. There is simply no integration point telling Avalonia to use your DI Container when resolve a View.
I looked at the code generated by Avalonia from the following Xaml and something surprise me..
Look at the Line 43, why does Avalonia do
new MyUserControl()
while it could use theServiceProvider P_0
to resolve this object? Avalonia can fallback tonew MyUserControl()
in case theServiceProvider P_0
are unable to resolve object.Is there a way to tell Avalonia to use our
IServiceProvider
to resolve object? which means Avalonia should change the line 63 and use ourServiceProvider
instead of creating a new one..Dependency Injection is possible with Avalonia, only if it can answer these 2 problems.
Beta Was this translation helpful? Give feedback.
All reactions