开发者

No parameterless constructor for the PhoneApplicationPage

开发者 https://www.devze.com 2023-03-21 00:55 出处:网络
I have a Windows Phone app with Ninj开发者_高级运维ect IOC. At some point I realized that my MainPage.xaml.cs (initial page of the application that gets initialized first) need to have a constructor

I have a Windows Phone app with Ninj开发者_高级运维ect IOC.

At some point I realized that my MainPage.xaml.cs (initial page of the application that gets initialized first) need to have a constructor with parameters.

I have added params to a constructor like this:

public partial class MainPage : PhoneApplicationPage
    {
        private readonly Settings _settings;

        // Constructor
        public MainPage(Settings _settings)

In my Ninject modules I have the binder for Settings type:

this.Bind<Settings>().ToSelf().InSingletonScope();

However, whenever I am to run an app, I get a MissingMethodException at startup.

I have worked around this problem by retaining a parameterless constructor in my MainPage(), and I use a service locator pattern with Ninject to get the Settings instance.

I want to know if there is a way for me to still have my app service locator free?


I'm afraid this isn't possible with the way the Silverlight navigation works. You have to have the parameterless constructor. Typically you would use the service locator to resolve your ViewModel which is where you need your dependencies injected, rather than your view.

As a side note don't fall into the trap of thinking that you should be developing your mobile apps the same way as you do desktop apps. The same rules don't automatically apply. IOC is an Enterprise Design Pattern, that aims to reduce the complexity of large apps with many developers developed over long periods. Phone apps are typically small apps with few developers developed over short periods - so its not necessarily true to that you have to rigidly follow the design pattern to the letter or even at all.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号