0

我正在轉向使用SimpleIoc進行依賴注入以使用Static類。 SimpleIoc中有一個Register<TClass>(Func<TClass> factory) where TClass : class方法,但我找不到使用它的任何示例。這裏有一個鏈接to the source code在MVVMLight的SimpleIoc中註冊已經實例化的類

我是否正確接近或者DI總是需要在註冊時創建它?這是我應該用來註冊課程的方法嗎? 你能舉個例子說明如何做到這一點嗎?

這是我試圖更新的Silverlight代碼。 編輯:將我的方法移至答案

回答

0

我將我的示例從我的問題轉到了答案,因爲沒有其他人在22天內回答過它。編輯:這是更好。

public partial class App : Application 
    {    
      /// <summary> 
      /// Initializes a new instance of the <see cref="App"/> class. 
      /// </summary> 
      public App() 
      { 
       this.Startup += (s, e) => 
       { 
       // create and register it now 
       SimpleIoc.Default.Register<IUserToken>(() => { return new UserToken(); }); 
       SimpleIoc.Default.GetInstance<IUserToken>().PopulateUserTokenFromService(() => 
       { 
        // don't do anything until the user token is populated from the server 
        InitializeComponent();     

        this.RootVisual = new View(); 
       }); 
       } 
      } 
相關問題