How to use autofac in an UWP app?

后端 未结 4 752
伪装坚强ぢ
伪装坚强ぢ 2021-02-04 18:58

I am using autofac in an UWP application. In my App instance, I am setting up the dependency, like this:

public sealed partial class App
{
   privat         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-04 19:42

    I'm very new to UWP (but a very old hand at .NET/WPF etc etc), so this might be hacky, but I by-passed the Frame.Navigate method altogether by putting the following at the top of my OnLaunched method:

            if (Window.Current.Content == null)
            {
                var windowFrame = new Frame()
                {
                    Content = new MainPage()
                };
    
                Window.Current.Content = windowFrame;
                Window.Current.Activate();
    
                return;
            }
    

    You then control the creation of your mainpage - if you want to support DI, then you can use something like:

                var windowFrame = new Frame()
                {
                    Content = container.GetInstance()
                };
    

    This then means that MainPage becomes your CompositionRoot and your DI flows from there.

    My guess is that you then don't get any navigate back/forward functionality, although I may be wrong...my app is only single-page so I've not seen if that is a problem or not...

提交回复
热议问题