Issues accessing CSLA object from WPF code class.

Issues accessing CSLA object from WPF code class.

Old forum URL: forums.lhotka.net/forums/t/5534.aspx


GusE posted on Saturday, October 04, 2008

Hello All...

I'm trying to use an example right out of the Project Tracker App..  I create a WPF user control derived from the EditForm base.  I can then in the code base attach events to delagates within the code base with the following code.

 

   Dim dp As Csla.Wpf.CslaDataProvider = TryCast(Me.FindResource("Controller"), Csla.Wpf.CslaDataProvider)
   AddHandler dp.DataChanged, AddressOf DataChanged

However if I attempt to access the object like this; I get an object Nothing returned.

 

 Dim objCtrlr as MyObjects.ControllerObject = TryCast(Me.FindResource("Controller"), MyObjects.ControllerObject)

 

Any ideas people??  I have seen this done in many code bases... So it should work.. Let us know... :)

 

 

RockfordLhotka replied on Monday, October 06, 2008

There are two possibilities.

  1. There is no resource named "Controller" (due to a typo or something)
  2. The resource can't be cast into your type, so TryCast() returns Nothing/null

You may consider (temporarily) breaking that line into two lines so you can debug more easily:

Dim tmp As Object = Me.FindResource("Controller")

Dim objCtrlr = TryCast(tmp, MyObjects.ControllerObject)

GusE replied on Monday, October 06, 2008

Thanks for the reply Rocky.   As I said before; while hooking the events to delegates it works fine with the same name..  So 'Controller' does exist.  I thought of doing this, and I'm not really sure why I haven't tried it yet.. Indifferent [:|]  I will try this.. 

Just going off onto another thought, when you define a resource on the page; and find that resource.  You should be able to Cast that object as any named object that the resource either defines, or inherits.. Correct?? 

 

  -G

GusE replied on Monday, October 06, 2008

Ok, here is what I'm getting after trying the above attempt...

 

dim obj as Object = FindResource("Controller")

The object returns as Csla.Wpf.CslaDataProvider

 

Then when attempting the TryCast it fails...  Mainly because I believe I'm hitting a proxy to the class that I am trying to capture... Indifferent [:|]

 

Any ideas???

sergeyb replied on Monday, October 06, 2008

What is the class defintion for ControllerObject?

Sergey.

RockfordLhotka replied on Monday, October 06, 2008

So you don’t expect to get a CslaDataProvider?

 

RockfordLhotka replied on Monday, October 06, 2008

You should be able to cast the object using normal .NET rules. Which means you can cast it to its own type, to anything it inherits from or to any interface it implements.

 

You can’t cast it to a subclass type, unless you specifically know that it IS that subclass.

 

Rocky

 

GusE replied on Thursday, October 09, 2008

All I can say is that I was correct in my assumption that I should be able to cast the object.. The only issue is that I have to cast the Property on the Container object... DUH...  Can you tell that I've been working WAY too much this week??  40 hours and going and it's just Thursday at 9am!

The answer is as follows...

Dim dp as Csla.Wpf.ClsaDataProvider = TryCast(Me.FindResource("Controller"), Csla.Wpf.CslaDataProvider)

dim _ctrlr as MainDisplayController = TryCast(dp.Data, MainDisplayController)

And wow, magically data...  I think I could drive a spike through my head at this point and it wouldn't effect my cognitive thinking process... Embarrassed [:$]

  -G

Copyright (c) Marimer LLC