Reputation: 4366
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:
static
Labelstatic Label matching
{
get { return matchLabel; } //errors here
set { matchLabel = value; } //and here
}
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
Upvotes: 1
Views: 6027
Reputation: 21365
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 { };
if (SkeletonID1 == SkeletonID2)
{
this.MyEvent("Your IDs are Matching!");
}
else if (SkeletonID2 != SkeletonID1)
{
this.MyEvent("Your IDs don't Match.");
}
if (PersonDetected == true)
{
this.MyEvent("Scan Aborted");
}
In your WPF view
this.MydomainComponent.MyEvent += (x) => { this.matchLabel.Content = x; };
Upvotes: 2
Reputation: 2771
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 x:Class="WpfApplication2.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MyWindow" Height="300" Width="300">
<Grid>
<Label Content="{Binding Path=MyLabelValue}" />
</Grid>
</Window>
Window Code Behind (Code)
using System.Windows;
using System.ComponentModel;
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MyWindow.xaml
/// </summary>
public partial class MyWindow : Window, INotifyPropertyChanged
{
public MyWindow()
{
InitializeComponent();
DataContext = this; // Sets context of binding to the class
}
// Property for binding
private string _mylabelvalue;
public string MyLabelValue
{
get { return _mylabelvalue; }
set
{
_mylabelvalue = value;
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("MyLabelValue"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
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/
Upvotes: 1
Reputation: 190941
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.
Upvotes: 1