0

I have a wpf setup where I have a timer running and I want to check to see what services are running and their state. I had this working in .net 4.5 but when I rolled it back to 4 the dispatcher signature changed and I am no longer able to access the elements. Basically I have The dispatcher object makes a call to the methods GetElementName

//use dispatcher to execute on the UI thread vs timer thread
Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { GetElementName(0, s.LabelName); }));
Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { GetElementName(1, s.LabelName); }));
Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { GetElementName(2, s.LabelName); }));

GetElementName(int, string) basically evaluates the type and name and is supposed to locate the element within the UI.

private void GetElementName(int etype, string labelName)
{       
   switch (etype)
     {
       case 0:
            CurrentLabel = (Label)base.FindName(labelName);
            break;
       case 1: 
            CurrentGrid = (Grid)base.FindName(labelName);
            break;
       case 2:
            CurrentEllipse = (Ellipse)base.FindName(labelName);
            break;
      }
}

//dummy properties
public Label CurrentLabel { get; set; }
public Grid CurrentGrid { get; set; }
public Ellipse CurrentEllipse { get; set; }

What I am seeing is the properties never get set they always return null.

Any suggestions on what I am missing here as to why the items are not being located and set?

This is a really simple/basic app so I am not overlaying any pattern this is straight code-behind from the Main-Window.xaml file. Each element that I want to modify has a x:Name identifier.

Edit

In bypassing the select statement I have also explicitly written out the attempt to retrieve the UI element using the following:

CurrentLabel = (Label)Dispatcher.Invoke((Action)(() => { FindName(s.LabelName); }));
CurrentGrid = (Grid)Dispatcher.Invoke((Action)(() => { FindName(s.GridName); }));
CurrentEllipse = (Ellipse)Dispatcher.Invoke((Action)(() => { FindName(s.ElipseName); }));

This has also returned null values

-cheers,

rlcrews
  • 3,482
  • 19
  • 66
  • 116
  • Does this compile? First parameter should be `Action` and second `DispatcherPriority`. – Rohit Vats Mar 04 '14 at 18:11
  • Yes it will compile and run. It will also compile and run if I just have the action with no priority set (I believe normal is implied). the failure in the code occurs later when attributes are supposed to be applied to the selected controls( which remain null) – rlcrews Mar 04 '14 at 18:18
  • 1
    Create a proper ViewModel and use proper DataBinding and all your problems will magically disappear. – Federico Berasategui Mar 04 '14 at 22:48
  • @rlcrews, try using `Application.Current.Dispatcher.Invoke` everywhere you use `Dispatcher.Invoke`. – noseratio Mar 05 '14 at 01:00

0 Answers0