现在,MAUI 为我们提供了所有 Essentials API 的接口,我正尝试在我的代码中使用它们,以使事情更具可测试性。
在这段视频中,詹姆斯说我们应该将它们注册为:
builder.Services.AddSingleton<IGeolocation, GeolocationImplementation>();
Run Code Online (Sandbox Code Playgroud)
但现在实现类都是内部的,因此不能以这种方式注册。
现在在类上使用 Current 或 Default 属性的正确方法如下所示?
builder.Services.AddSingleton<IGeolocation>(ctx => Geolocation.Default);
builder.Services.AddSingleton<IConnectivity>(ctx => Connectivity.Current);
Run Code Online (Sandbox Code Playgroud)
谢谢
许多变化接连发生。James 的视频当时可能是正确的,但 .NET MAUI 中的实现随后发生了更改。
正如您所指出的,正确的方法是这样做:
builder.Services.AddSingleton<IGeolocation>(ctx => Geolocation.Default);
builder.Services.AddSingleton<IConnectivity>(ctx => Connectivity.Current);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
589 次 |
| 最近记录: |