我遵循我的应用程序的MVVM模式并防止内存泄漏,我需要将对象实例的数量减少到一个。然后,应该将该对象实例交给其他需要的类。
在我当前的实现中,实例正在AppVM中设置,并传递给AdductionAbductionFlexionViewModel
。但是,通过搜索MyoDeviceModle()
实例,AdductionAbductionFlexionView
中会显示第二个实例。
我的问题是如何在AdductionAbductionFlexionView
中设置构造函数以获取在整个应用中使用的_connectedDevice
实例?
这是AppVM,显示了应该使用的唯一实例:
private MyoDeviceModel _connectedDevice;
#endregion
/// <summary>
/// Initializes a new instance of the <see cref="ApplicationViewModel"/> class.
/// </summary>
public ApplicationViewModel()
{
_connectedDevice = new MyoDeviceModel();
// Add available pages
PageViewModels.Add(new HomeViewModel(new UserLoginModel()));
PageViewModels.Add(new AdductionAbductionFlexionViewModel(_connectedDevice, new DatabaseModel()));
// Set starting page
CurrentPageViewModel = PageViewModels[0];
}
这就是它传递给AdductionAbductionFlexionViewModel
:
public event Action<string> DataChanged;
private string _itemString;
/// <summary>
/// Initializes a new instance of the <see cref="AdductionAbductionFlexionViewModel"/> class.
/// </summary>
/// <param name="Myodevice">The device.</param>
/// <param name="progressData">The progress data.</param>
public AdductionAbductionFlexionViewModel(MyoDeviceModel Myodevice, DatabaseModel progressData)
{
DataSubmitCommand = new RelayCommand (this.SaveChangesToPersistence);
CalibrationSetCommand = new RelayCommand(this.CallibratePitchMinimumCall);
DatabaseSearchCommand = new RelayCommand(this.QueryDataFromPersistence);
_myoDevice = Myodevice;
_myoDevice.MyoDeviceStart();
_dataObj = progressData;
_myoDevice.StatusUpdated += (update) =>
{
CurrentStatus = update;
};
_myoDevice.PoseUpdated += (update) =>
{
PoseStatus = update;
};
_myoDevice.PainfulArcDegreeUpdated += (update) =>
{
PainfulArcStatus = update;
};
_myoDevice.DegreesUpdated += (update) =>
{
DegreeStatus = update;
};
_myoDevice.StartDegreeUpdated += (update) =>
{
StartDegreeStatus = update;
};
_myoDevice.EndDegreeUpdated += (update) =>
{
EndDegreeStatus = update;
};
}
但我想知道如何通过_connectedDevice
实例代替new MyoDeviceModel()
public AdductionAbductionFlexionView()
{
InitializeComponent();
this.DataContext = new AdductionAbductionFlexionViewModel(new MyoDeviceModel(), new DatabaseModel());
this.Loaded += AdductionAbductionFlexionView_Loaded;
this.Loaded -= AdductionAbductionFlexionView_Loaded;
((AdductionAbductionFlexionViewModel)DataContext).DataChanged += x =>
{
new ToastPopUp("Submit Succeeded!", "Progress data submitted succesfully", NotificationType.Information)
{
Background = new LinearGradientBrush(System.Windows.Media.Color.FromRgb(0, 189, 222), System.Windows.Media.Color.FromArgb(255, 10, 13, 248), 90),
BorderBrush = new SolidColorBrush(System.Windows.Media.Color.FromRgb(0, 189, 222)),
FontColor = new SolidColorBrush(System.Windows.Media.Color.FromRgb(255, 255, 255))
}.Show();
};
}