我应该在 WPF Prism 中绑定到 SharedService 属性
Should I bind to SharedService property in WPF Prism
我使用 SharedService (Prism) 在两个模块之间获取数据。在 SharedService 中,我放置了一个名为 AdapterName 的字符串 属性。
假设模块 A 中有 ViewAViewModel,模块 B 中有 ViewBViewModel
public class ViewAViewModel : BindableBase {
private string _adapterNameA;
public string AdapterNameA
{
get { return _adapterNameA; }
set { SetValue (ref _adapterNameA, value); }
}
private ISharedService _sharedService;
public ISharedService SharedService {
get { return _sharedService; }
set { SetValue (ref _sharedService, value); }
}
public ViewAViewModel (ISharedService sharedService) {
_sharedService = sharedService;
}
}
public class ViewBViewModel : BindableBase {
private string _adapterNameB;
public string AdapterNameB {
get { return _adapterNameB; }
set { SetValue (ref _adapterNameB, value); }
}
private ISharedService _sharedService;
public ISharedService SharedService {
get { return _sharedService; }
set { SetValue (ref _sharedService, value); }
}
public ViewBViewModel (ISharedService sharedService) {
_sharedService = sharedService;
}
}
public interface ISharedService {
string AdapterName { get; set; }
}
public class SharedService : BindableBase, ISharedService {
private string _adapterName;
public string AdapterName {
get { return _adapterName; }
set { SetValue (ref _adapterName, value); }
}
}
我有一个 ViewA 和 ViewB 文本框,我希望 ViewA 文本框中的值始终与 ViewB 中的相同。那么我是否应该更改 AdapterNameA get、set 中的 SharedService.AdapterName 值(类似于 AdapterNameB)?
public string AdapterNameA
{
get {
_adapterNameA = SharedService.AdapterName;
return _adapterNameA;
}
set {
SetValue (ref _adapterNameA, value);
SharedService.AdapterName = value;
}
}
或直接绑定到 SharedService 属性
Text = "{Binding Path=SharedService.AdapterName, UpdateSourceTrigger=PropertyChanged}"
还是其他方式? (我正在尝试使用 Prism 制作 WPF MVVM)
如果共享服务实现 INotifyPropertyChanged
事件并引发更改通知,您可以直接绑定到它的 属性,前提是您真的想通过 属性 公开服务查看模型。根据服务的功能,您可能不想这样做。
如果您不想公开该服务,则只能在视图模型中创建包装器属性。这提供了更好的封装,但以包装器属性的一些额外代码行为代价(如果您有多个)。
但是这里没有真正的对错。您必须决定在这种特定情况下哪种解决方案对您最有意义。
一般来说,服务应该抽象出来并负责从某处获取一些结果并将这些结果传递回视图模型。它通常不实现 INotifyPropertyChanged
事件并公开视图直接绑定到的属性。定义和公开此属性是视图模型的责任。视图模型可能仍然使用该服务来填充属性。
我使用 SharedService (Prism) 在两个模块之间获取数据。在 SharedService 中,我放置了一个名为 AdapterName 的字符串 属性。 假设模块 A 中有 ViewAViewModel,模块 B 中有 ViewBViewModel
public class ViewAViewModel : BindableBase {
private string _adapterNameA;
public string AdapterNameA
{
get { return _adapterNameA; }
set { SetValue (ref _adapterNameA, value); }
}
private ISharedService _sharedService;
public ISharedService SharedService {
get { return _sharedService; }
set { SetValue (ref _sharedService, value); }
}
public ViewAViewModel (ISharedService sharedService) {
_sharedService = sharedService;
}
}
public class ViewBViewModel : BindableBase {
private string _adapterNameB;
public string AdapterNameB {
get { return _adapterNameB; }
set { SetValue (ref _adapterNameB, value); }
}
private ISharedService _sharedService;
public ISharedService SharedService {
get { return _sharedService; }
set { SetValue (ref _sharedService, value); }
}
public ViewBViewModel (ISharedService sharedService) {
_sharedService = sharedService;
}
}
public interface ISharedService {
string AdapterName { get; set; }
}
public class SharedService : BindableBase, ISharedService {
private string _adapterName;
public string AdapterName {
get { return _adapterName; }
set { SetValue (ref _adapterName, value); }
}
}
我有一个 ViewA 和 ViewB 文本框,我希望 ViewA 文本框中的值始终与 ViewB 中的相同。那么我是否应该更改 AdapterNameA get、set 中的 SharedService.AdapterName 值(类似于 AdapterNameB)?
public string AdapterNameA
{
get {
_adapterNameA = SharedService.AdapterName;
return _adapterNameA;
}
set {
SetValue (ref _adapterNameA, value);
SharedService.AdapterName = value;
}
}
或直接绑定到 SharedService 属性
Text = "{Binding Path=SharedService.AdapterName, UpdateSourceTrigger=PropertyChanged}"
还是其他方式? (我正在尝试使用 Prism 制作 WPF MVVM)
如果共享服务实现 INotifyPropertyChanged
事件并引发更改通知,您可以直接绑定到它的 属性,前提是您真的想通过 属性 公开服务查看模型。根据服务的功能,您可能不想这样做。
如果您不想公开该服务,则只能在视图模型中创建包装器属性。这提供了更好的封装,但以包装器属性的一些额外代码行为代价(如果您有多个)。
但是这里没有真正的对错。您必须决定在这种特定情况下哪种解决方案对您最有意义。
一般来说,服务应该抽象出来并负责从某处获取一些结果并将这些结果传递回视图模型。它通常不实现 INotifyPropertyChanged
事件并公开视图直接绑定到的属性。定义和公开此属性是视图模型的责任。视图模型可能仍然使用该服务来填充属性。