簡體   English   中英

MVVM Light SimpleIoC找不到實例

[英]MVVM Light SimpleIoC can't find instance

我正在使用WPF MVVM Light SimpleIoC來實現對我的服務的訪問。 注冊和使用虛擬機沒有任何問題,但是注冊服務時無法使用它。

有我的代碼:

public interface IDeviceDataAccessService
{
    List<Models.Device> GetDevices();
    bool InsertDevice(ref Models.Device device, int userId);
    bool RemoveDevice(Models.Device device);
}

執行:

public class DeviceDataAccessService : DataAccesBase, IDeviceDataAccessService
{

    public DeviceDataAccessService(string connectionString) : base(connectionString)
    {
    }
    ...
}

ViewModelLocator

SimpleIoc.Default.Register<IDeviceDataAccessService, DeviceDataAccessService>();

並在代碼中使用

SimpleIoc.Default.GetInstance<IDeviceDataAccessService>();

當我將IDeviceDataAccessService更改為“ TestViewModel”時,我沒有任何問題。 但是我不能使用我的服務。

Message = "Exception has been thrown by the target of an invocation."

 at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] 
 arguments, Signature sig, Boolean constructor)\r\n  
 at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, 
 Object[] parameters, Object[] arguments)\r\n  
 at System.Delegate.DynamicInvokeImpl(Object[] args)\r\n  
 at GalaSoft.MvvmLight.Ioc.SimpleIoc.DoGetService(Type serviceType, String 
 key, Boolean cache) in C:\\Users\\lbugn\\Documents\\MVVMLight\\GalaSoft.MvvmLight\\GalaSoft.MvvmLight.Extras (PCL)\\Ioc\\SimpleIoc.cs:line 622\r\n   
 at GalaSoft.MvvmLight.Ioc.SimpleIoc.GetInstance[TService]() in C:\\Users\\lbugn\\Documents\\MVVMLight\\GalaSoft.MvvmLight\\GalaSoft.MvvmLight.Extras (PCL)\\Ioc\\SimpleIoc.cs:line 1059\r\n   
 at LabDesk.ViewModel.MainViewModel..ctor() in C:\\Users\\kzrostek\\Documents\\Git repo\\labdesk\\LabDESK\\LabDesk\\ViewModel\\MainViewModel.cs:line 141"

的InnerException

{"Type not found in cache: System.String."}

我忘記了如何注冊連接字符串,但我認為您的錯誤是在於deviceaccesservice的構造函數:

public DeviceDataAccessService(string connectionString) 

需要一個連接字符串作為參數。 它沒有給它這樣的...崩潰。 我認為您需要更多類似的東西:

SimpleIoc.Default.Register<IDeviceDataAccessService>(() => {
    return new DeviceDataAccessService("Whatever connectionstring should be");
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM