简体   繁体   English

如何为应用程序设置单个对象实例?

[英]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. 我正在为我的应用程序遵循MVVM模式,并且为了防止内存泄漏,我需要将对象实例的数量减少到一个。 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 . 在我目前的执行情况被设立在AppVM,并传递给AdductionAbductionFlexionViewModel But from doing a search for the MyoDeviceModle() instance, a second instance is shown here in the AdductionAbductionFlexionView . 但是,从做了一个搜索MyoDeviceModle()情况下,第二个实例在这里显示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? 我的问题是如何建立的构造在AdductionAbductionFlexionView采取_connectedDevice那就是BEING整个应用程序使用的实例?

This is the AppVM showing the main and only instance that should be used: 这是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];
        }

And this is how its passed to the AdductionAbductionFlexionViewModel : 这就是它传递给AdductionAbductionFlexionViewModel

private MyoDeviceModel _myoDevice; 私人MyoDeviceModel _myoDevice; private DatabaseModel _dataObj; 私有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() 但是我想知道如何传递_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();

            };

        }

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 . 如果您不想传递实例,则需要考虑实现Singleton Pattern

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用类的单个对象实例来设置多个成员值? - How to use single object instance for class to set the multiple member value? 如何使用IPC将数据从ActiveX对象传递到VB6应用程序-(单个实例应用程序)? - How to use IPC to pass data from ActiveX Object into a VB6 application - (Single instance application)? 如何实现每台机器应用程序的单实例? - How to implement single instance per machine application? 如何设置便携式设备检测的单个实例 - How to set single instance of the portable device detection 如何模拟自定义对象列表并将其设置为返回 object 的真实实例 - How do I mock a List of custom objects and set it up to return a real instance of an object 如何设置类实例控件事件? - how to set up class instance control events? WCF服务客户端应用程序获取“对象未设置为对象的实例” - WCF service client application getting “Object not set to an instance of an object” 如何诊断 Object 参考未设置为 Object 的实例 - How to Diagnose Object Reference not set to an Instance of an Object 对象引用未设置为对象的实例。(C#Web应用程序) - Object reference not set to an instance of an object.(C# web application) Winform应用程序中的MDIparent返回对象引用未设置为对象的实例 - MDIparent in Winform Application return Object reference not set to an instance of an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM