如何知道字符串何时更改并执行某些操作? [英] How to know when a string changed and do something?

查看:22
本文介绍了如何知道字符串何时更改并执行某些操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提前为一个菜鸟问题道歉.我有一个在不受我控制的情况下发生变化的字符串(另一方的字符串)让我们称之为 firstString,每次此字符串更改时,我都需要在我的程序中执行一个操作.

I apologize in advance for a noob question. I have a string which is changing without my control (another party's string) lets call it firstString, every time this string changes I need to carry out an action in my program.

所以我创建了一个实现INotifyPropertyChanged"接口的类,并在其中创建了一个带有属性的字符串,我们称之为 secondString,在表单的主方法上,我创建了一个PropertyChangedEventHandler"和一个事件方法,它显示了一个值为 firstString 的消息框.

so I made a class that implements the "INotifyPropertyChanged" Interface, and created a string in it with a property, lets call it secondString, and on the main method of the form I've created a "PropertyChangedEventHandler" and an event method which shows a message box with the value of firstString.

如果我通过单击按钮更改 firstString 的值来手动测试和更改 firstString ,则一切正常,并且在通过 secondString 的属性后我收到一个带有 firstString 值的消息框,我将其设置如下:SecondString(这是一个属性)= firstString;

everything works well if I manually test and change firstString by clicking a button to change its value and I get a message box with firstString's value after it went through secondString's Property, I set it like this: SecondString(this is a property) = firstString;

但事情是 firstString 本身正在改变,我无法控制它,所以如果它由代码设置为等于 secondString 的属性,会发生的是它只在第一次运行时有效.

but the thing is firstString is changing by itself, and I don't have control over it, so if it is set by code to equal secondString's property what happens is that it only works for the first time that it runs.

所以现在每次 secondString 的属性发生变化时,都会触发事件并且该部分工作正常.但是每次 firstString 的值更改时,我都需要使用 firstString 的值自动设置 secondString 的值.我觉得 INotifyPropertyChanged 也应该以某种方式在这部分工作,但我不明白是怎么做的.所以我试图弄清楚如何将字符串的 A 值绑定"到 secondString 的属性,并进入 DataBinding,但我找不到任何将两个字符串绑定在一起的示例,仅是关于绑定到控件或从控件绑定.

so now every time secondString's property is changing, the event fires and that part is working OK. but I need to set secondString's value with firstString's value automatically every time that firstString's value changes. and I kind of figure that INotifyPropertyChanged should have somehow worked here for this part as well but I can't understand how. so I was trying to figure our how to "bind" string's A value to secondString's property, and got into DataBinding, but I couldn't find any example to bind two strings together, only about binding to or from a control.

这里是一个演示代码,我认为我没有注意到的关键是 firstString 是我从另一方的类库中获取的字符串.

here is a code to demo, I think the key that I've missed to note is that firstString is a string I get by another party's class library.

Using AnotherPartyLibrary;

FirstClass fc;          

 public Form1()
        {
            InitializeComponent();

            fc = new FirstClass();
            fc.PropertyChanged += new PropertyChangedEventHandler(fc_PropertyChanged);

            fc.SecondString = AnotherPartyLibrary.firstString;

            this.Disposed += new EventHandler(Form1_Disposed);
        }

     void fc_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
           MessageBox.Show("Something changed!" + e.PropertyName);
        }

    public class firstClass :INotifyPropertyChanged
    {
        private string secondString = string.Empty;

        public string SecondString
        {
            get { return this.secondString; }
            set
            {
                this.secondString = value;
                NotifyPropertyChanged("SecondString");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }

这个问题一般是怎么解决的?提前谢谢了!

How is this problem is usually solved? Many Thanks in advance!

除了 a.azemia 提供的解决方案之外,还有人可以提供其他解决方案吗?再次感谢!

雷.

推荐答案

假设您无法控制 firstString,那么您可以使用 BackgroundWorker 来监控 firstString 值并更新 SecondString

Based on the assumption that you do not have control over firstString then you can use BackgroundWorker to monitor firstString value and update SecondString

        BackgroundWorker bw = new BackgroundWorker();
        bw.DoWork += (s, e) =>
            {
                while (true)
                {
                    if (!fc.SecondString.Equals(AnotherPartyLibrary.firstString))
                    {
                        fc.SecondString = AnotherPartyLibrary.firstString;
                    }
                    Thread.Sleep(1000);
                }
            };
        bw.RunWorkerAsync();

您也可以使用 Microsoft TPL 来达到相同的效果

You can also use Microsoft TPL to achieve the same result

public async void YourMethodName() 
{
    await Task.Run(() => 
        {
                while (true)
                {
                    if (!fc.SecondString.Equals(AnotherPartyLibrary.firstString))
                    {
                        fc.SecondString = AnotherPartyLibrary.firstString;
                    }
                    Thread.Sleep(1000);
                }
        });
}

但是,如果您可以更新 firstString 的代码,那么您应该在 firstString 上实现 INotifyPropertyChanged.

However, if you can update the code for firstString then you should implement INotifyPropertyChanged on firstString.

这篇关于如何知道字符串何时更改并执行某些操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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