Splat:Locator.Current 与 Locator.Current 可变

Splat: Locator.Current vs Locator.CurrentMutable

因此,Splat 提供了两种检索服务的方法:

var s1 = Locator.Current.GetService<IMyService>();
var s2 = Locator.CurrentMutable.GetService<IMyService>();

Documentation不清楚应该使用哪种方式

那么这两者有什么区别呢?我们应该使用哪一个?我们应该避免哪一个?

Locator.Current是ReadOnly版本,应该使用除非你正在注册服务。 Locator.CurrentMutable 是 read/write 版本,只能在注册服务时使用。

它们都指向相同的定位器,只是基于您使用哪个变量的用例。

基本上:

  • 使用 CurrentMutable 注册服务,
  • 在需要检索它们时使用 Current。