如何实时提升propertyChanged事件 [英] How do I raise a propertyChanged event in real time

查看:82
本文介绍了如何实时提升propertyChanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个应用程序来保存用户活动我最初使用的代码来自于此我相信不过我改变了它以满足我的需求。我的问题是我想实时保存应用程序运行的持续时间,也就是说我的代码运行一个调用我的方法的计时器。然而,我一直被困在一个用户在应用程序上的持续时间,所以我认为我可以使用propertychanged事件来做到这一点但是我对此非常不熟悉并且我已经阅读了很多这方面的文章。

http://stackoverflow.com/questions/12341666/elegant-way- to-implement-inotifypropertychanged-across-many-controls [ ^ ]

http://jesseliberty.com/2012/06/28/c-5making-inotifypropertychanged-easier/ [ ^ ]

所有这些都是我读过的很多代码之一不是真的为我工作,我也不知道如何。所以这是我最初使用的代码或代码的一部分我已删除它虽然因为它没有在这段代码中工作但我还没有实现属性更改因为我说我不知道​​如何使用它。

I am currently developing an application that will save user activity I had initially been working with some code from Here I believe however I changed it around to suit my needs. My problem is that I would like to save the duration an application is running for in real time that is to say my code runs off a timer which calls my methods. However I have been stuck getting the duration a user is on an application so I thought I could use propertychanged event to do this however I am very unfamilier with this and I have read so many articles on this.
http://stackoverflow.com/questions/12341666/elegant-way-to-implement-inotifypropertychanged-across-many-controls[^]
http://jesseliberty.com/2012/06/28/c-5making-inotifypropertychanged-easier/[^]
All being one of many I had read however the code does not really work for me and I do not know how to either. So this is the code or part of the code I was originally using I have since deleted it though as it was not working in this code I hadn't implemented property changed as of yet because Like i said I am not sure how to use it.

DateTime appStart = new DateTime();
        DateTime appStart = new DateTime();
        TimeSpan Duration;
        DateTime now = DateTime.Now;
        string time = now.ToString();
        const int nChars = 256;
        int handle = 0;
        StringBuilder Buff = new StringBuilder(nChars);
        handle = GetForegroundWindow();

        if (GetWindowText(handle, Buff, nChars) > 0)
        {
            string strbuff = Buff.ToString();
            appstart = DateTime.Now();

            #region insert statement

            try
            {
                var with = cnSave;
                if (with.State == ConnectionState.Open)
                    with.Close();
                with.ConnectionString = cnString;
                with.Open();
                string strQRY = "Insert Into [Log] values ('" + strbuff + "', '" + time + "', '" + Processing + "')";


                OleDbCommand cmd = new OleDbCommand(strQRY, cnSave);
                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (Exception)
                {

                }

            }
            finally { }

            #endregion
            ActivityTimer.Start();
            Processing = "Working";
        }



上面的代码只是写入数据库。我想要做的是首先制作一个时间戳,当窗口改变时很简单我只需添加代码appStart但超出此范围我不知道应该做什么。


The code above though simply writes into the database. What I had wanted to do was first off make a timestamp for when the window changed that is easy I simply add the code "appStart" but beyond that I am not sure what I should do.

推荐答案

除了声明事件实例的类型之外,您永远不能从任何地方调用任何事件。甚至不是派生类。与常规委托实例相比,这是CLR事件的一个重要的万无一失的功能。显然,声明某些事件的所有非废话类型都有一些调用事件的代码,因此,如果您有权访问此类代码,则可以间接调用。因此,如果有问题的事件是 PropertyChanged 事件,您可以通过更改相关的属性值来调用它。



-SA
You can never invoke any event from anywhere except the type where the event instance is declared. Not even from a derived class. This is an important fool-proof feature of CLR events, in contrast to "regular" delegate instances. Apparently, all non-nonsense types declaring some events have some code where the event is invoked, so, if you have access to such code, you can invoke the even indirectly. So, if the event in question is the PropertyChanged event, you can invoke it by… well, by changing relevant property value.

—SA


这篇关于如何实时提升propertyChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆