Help with Silverlight EditableValidatingObject

Apr 20, 2011 at 6:16 PM

I have been using Cinch great in WPF so now I am moving to Silverlight and running into an issue.

 I currently have a Cinch EditableValidatingObject for each EF entity. It populates fine but when I try to enter Edit mode ("BeginEdit") the GetFieldValues() method on EditableValidatingObject Fails.

I get a Attempt by method 'Cinch.EditableValidatingObject.<GetFieldValues>b__0(System.Reflection.FieldInfo)' to access field XXXX Failed for any field that is private.

This works flawlessly in WPF so I dont know how to correct this.

 

Any assistance would be greatly appreciated.

Adam

Apr 20, 2011 at 9:23 PM

After digging and digging I found the solution the build I was using for Silverlight had the following in the GetFieldValues method

Code Snippet
  1. return GetType().GetFields(BindingFlags.Public |
  2.     BindingFlags.NonPublic | BindingFlags.Instance).Select(
  3.     fi => new { Key = fi.Name, Value = fi.GetValue(this) })
  4.         .ToDictionary(k => k.Key, k => k.Value);

after to changing to

Code Snippet
  1. return GetType().GetProperties(BindingFlags.Public |
  2.                 BindingFlags.NonPublic | BindingFlags.Instance)
  3.                 .Where(pi => pi.CanRead && pi.GetIndexParameters().Length == 0)
  4.                 .Select(pi => new { Key = pi.Name, Value = pi.GetValue(this, null) })
  5.                 .ToDictionary(k => k.Key, k => k.Value);

 everything worked as in my WPF Build.

 



 

Coordinator
Apr 21, 2011 at 2:05 PM

Ah I see you had an older version or something then?

Coordinator
Apr 21, 2011 at 2:07 PM

One thing you should know is that I used to be of the frame of mind to expose a currentXXX object from my ViewModel which is what the EditableValidatingObject/ValidatingObject were all about, these days I tend to use the ViewModels directly so do not really do any real work in the EditableValidatingObject/ValidatingObject objects. Good you have an answer though.

Apr 21, 2011 at 3:53 PM

Well I was a little soon to respond. The changes allowed me to capture the properties but they do not restore correctly on a cancel Edit... Guess I am still stuck.

Have you done any silverlight work using Cinch, EF4, and Ria Services. I would be interested in seeing how you would go about working with your entities.

Thanks as always.

Apr 21, 2011 at 7:00 PM
Edited Apr 21, 2011 at 7:05 PM

This is what I ended up using in the end. This restores all the values I need for my application. The changes I made in the EditableValidatingObject.cs and are as follows

Code Snippet
  1. protected virtual Dictionary<string, object> GetFieldValues()
  2.         {
  3.             
  4.               return GetType().GetProperties(BindingFlags.Public |
  5.                 BindingFlags.NonPublic | BindingFlags.Instance)
  6.                 .Where(pi => pi.CanWrite && pi.GetIndexParameters().Length == 0)
  7.                 .Select(pi => new { Key = pi.Name, Value = pi.GetValue(this, null) })
  8.                 .ToDictionary(k => k.Key, k => k.Value);
  9.          }

and

Code Snippet
  1. protected virtual void RestoreFieldValues(Dictionary<string, object> fieldValues)
  2.       {
  3.        
  4.           if (fieldValues == null) return;
  5.  
  6.           foreach (PropertyInfo pi in GetType().GetProperties(
  7.               BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
  8.               .Where(pi => pi.CanWrite && pi.GetIndexParameters().Length == 0))
  9.           {
  10.               object value;
  11.               if (fieldValues.TryGetValue(pi.Name.ToLower(), out value))
  12.                   pi.SetValue(this, value, null);
  13.               else
  14.               {
  15.                   Debug.WriteLine("Failed to restore property " +
  16.                   pi.Name + " from cloned values, property not found in Dictionary.");
  17.               }
  18.           }
  19.  
  20.       }