简体   繁体   中英

How to set up a single object instance for an application?

I'm following an MVVM pattern for my application and to prevent memory leaks, I need to cut down the number of object instances to one. This object instance should then be handed of to the other classes where needed.

In my current implementation the instance is being set up in the AppVM, and passed to the AdductionAbductionFlexionViewModel . But from doing a search for the MyoDeviceModle() instance, a second instance is shown here in the AdductionAbductionFlexionView .

My question is how can I set up the constructor in the AdductionAbductionFlexionView to take the _connectedDevice instance that's being used throughout the app?

This is the AppVM showing the main and only instance that should be used:

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];
        }

And this is how its passed to the AdductionAbductionFlexionViewModel :

private MyoDeviceModel _myoDevice; private DatabaseModel _dataObj;

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;    
    };


}

But I'm wondering how I can pass the _connectedDevice instance instead of 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();

            };

        }

You are passing the initial instance to the other class's constructor. Thus you do use only one instance and not two. If you don't want to pass the instance around, you need to look at implementing a Singleton Pattern .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM