[英]How to know when a string changed and do something?
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. 我有一个在没有我的控制的情况下正在更改的字符串(另一方的字符串),请将该字符串称为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. 因此,我制作了一个实现“ INotifyPropertyChanged”接口的类,并在其中创建了一个带有属性的字符串,将其命名为secondString,并在表单的主要方法上创建了“ PropertyChangedEventHandler”和一个显示一个带有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的属性后得到一个带有firstString值的消息框,则一切正常,我将其设置如下: SecondString
(这是一个属性) = firstString;
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. 但是问题是firstString本身在变化,并且我无法控制它,因此,如果代码将其设置为等于secondString的属性,则会发生的情况是它仅在第一次运行时起作用。
so now every time secondString's property is changing, the event fires and that part is working OK. 因此,现在每当secondString的属性更改时,事件都会触发,并且该部分正常运行。 but I need to set secondString's value with firstString's value automatically every time that firstString's value changes. 但是我需要在firstString的值每次更改时自动将firstString的值设置为firstString的值。 and I kind of figure that INotifyPropertyChanged should have somehow worked here for this part as well but I can't understand how. 我有点认为INotifyPropertyChanged应该也可以在这部分工作,但我不知道如何。 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. 因此,我试图弄清楚如何将字符串的A值“绑定”到secondString的属性,并进入DataBinding,但是我找不到任何将两个字符串绑定在一起的示例,仅是关于控件的绑定。
EDIT: 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. 编辑:这是一个演示代码,我想我想注意的关键是firstString是我通过另一方的类库获得的字符串。
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! 提前谢谢了!
Edit: can anybody offer another solution other than what a.azemia has offered?Thanks again! 编辑:除了a.azemia提供的解决方案之外,谁能提供其他解决方案?再次感谢!
Ray. 射线。
Based on the assumption that you do not have control over firstString then you can use BackgroundWorker to monitor firstString value and update SecondString 基于您无法控制firstString的假设,则可以使用BackgroundWorker监视firstString值并更新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();
You can also use Microsoft TPL to achieve the same result 您还可以使用Microsoft TPL获得相同的结果
public async void YourMethodName()
{
await Task.Run(() =>
{
while (true)
{
if (!fc.SecondString.Equals(AnotherPartyLibrary.firstString))
{
fc.SecondString = AnotherPartyLibrary.firstString;
}
Thread.Sleep(1000);
}
});
}
However, if you can update the code for firstString then you should implement INotifyPropertyChanged
on firstString. 但是,如果可以更新firstString的代码,则应在firstString上实现INotifyPropertyChanged
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.