Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microsoft.Extensions.HostBuilder integration sample #35

Open
maxkatz6 opened this issue Nov 26, 2022 · 2 comments · May be fixed by #64
Open

Microsoft.Extensions.HostBuilder integration sample #35

maxkatz6 opened this issue Nov 26, 2022 · 2 comments · May be fixed by #64
Labels
enhancement New feature or request

Comments

@maxkatz6
Copy link
Member

Topics to cover

It shouldn't be a much related to avalonia, nor a big problem. But users expect some build in solution for this integration.
We should at least have ready to show sample in this repo, which would answer these questions.

Ideas for a sample

How to:

  • Setup MS host
  • Setup DI
  • Create VM from the IoC

Additional context

Related:
AvaloniaUI/Avalonia#8588
AvaloniaUI/Avalonia#5241 (comment)
AvaloniaUI/Avalonia#8936

@maxkatz6 maxkatz6 added the enhancement New feature or request label Nov 26, 2022
@kekekeks
Copy link
Member

kekekeks commented Nov 26, 2022

Some sample code to set up view locator using Microsoft DI:
    public class ViewLocator : IDataTemplate
    {
        private readonly Dictionary<Type, Func<Control>> _dic;
        public ViewLocator(IEnumerable<ViewLocationDescriptor> descriptors)
        {
            _dic = descriptors.ToDictionary(x => x.ViewModel, x => x.Factory);
        }
        public record ViewLocationDescriptor(Type ViewModel, Func<Control> Factory);
        
        public IControl Build(object param) => _dic[param.GetType()]();

        public bool Match(object data) => _dic.ContainsKey(data.GetType());
    }
    
    
        public static IServiceCollection AddView<TViewModel, TView>(this IServiceCollection services) where TView : Control, new()
        {
            services.AddSingleton(new ViewLocator.ViewLocationDescriptor(typeof(TViewModel), () => new TView()));
            return services;
        }
        

@timunie
Copy link
Collaborator

timunie commented Nov 26, 2022

That's a great idea and also something I struggle with. So a good opportunity to learn this stuff ^^

@maxkatz6 maxkatz6 linked a pull request Nov 4, 2023 that will close this issue
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants