So I have a program in which I am telling the user whether two skeletons match, but the thing is that I need to access the label
via a class
. The error I keep getting is
Error1 An object reference is required for the
non-static field, method, or property
'WpfApplication1.MainWindow.matchLabel'
Here's what I have in my code:
The static
Label
static Label matching
{
get { return matchLabel; } //errors here
set { matchLabel = value; } //and here
}
The Class
private class Scan
{
private void Start()
{
Skeleton skeleton = new Skeleton();
if (PersonDetected == true)
{
int SkeletonID2 = skeleton.TrackingId;
if (SkeletonID1 == SkeletonID2)
{
matching.Content = "Your IDs are Matching!";
}
else if (SkeletonID2 != SkeletonID1)
{
matching.Content = "Your IDs don't Match.";
}
}
}
private void Stop()
{
if (PersonDetected == true)
{
matching.Content = "Scan Aborted";
}
}
}
Basically I want to know how to make the label in wpf
static
, or if there is another way to do this.
Thanks in advance
I think that you could use another approach, like @Daniel said, using UI elements on multiple threads is a bad idea.
If my understanding is correct, you just want to notify to the user the result from your domain logic, the way I would do it is simple, create an event:
public event Action<string> MyEvent = delegate { };
In your WPF view
this.MydomainComponent.MyEvent += (x) => { this.matchLabel.Content = x; };
This is a bad idea. You shouldn't create UI elements on multiple threads.
You really should consider implementing the MVVM pattern. It will make your code more decoupled and increase testablility.
Your best bet would be to use the built in WPF Databinding. You can use the MVVM pattern but it's not required for this to work.
Window Class (XAML)
Window Code Behind (Code)
By using this method when you set / call the property on the window you get the value for the label. When you change the property - you update the value in the UI via data binding and the INotifyPropertyChanged interface. I have a section on doing this via reflection and using the MVVM pattern on my blog here.
http://tsells.wordpress.com/category/mvvm/