2

Using the amazing MonoTouch.Dialog tool set, how can I ensure that Backing fields are not show. When I assign an [Entry] attribute to a get;set; property, I get the following rendered:

[Caption("Weight")]
[Entry(Placeholder = "Kilograms", KeyboardType = UIKeyboardType.PhonePad)]
public string Weight { get; set; }

enter image description here

Ian Vink
  • 66,960
  • 104
  • 341
  • 555

3 Answers3

3

This is a bug in MonoTouch.Dialog, your best option is to not use properties, and instead use just fields directly or alter your local copy of MonoTouch.Dialog to prevent creating elements if they are not public (currently it uses public and private methods).

Change this line:

var members = o.GetType ().GetMembers (BindingFlags.DeclaredOnly | BindingFlags.Public |
                                       BindingFlags.NonPublic | BindingFlags.Instance);

With:

var members = o.GetType ().GetMembers (BindingFlags.DeclaredOnly | BindingFlags.Public |
                                       BindingFlags.Instance);
miguel.de.icaza
  • 32,654
  • 6
  • 58
  • 76
2

I consider this a bug in MonoTouch.Dialog. I've made major changes to BindingContext in my project so I can't point out the exact place to make the fix but this might help:

    private MemberInfo[] GetMembers(object dataContext)
    {
        return dataContext.GetType().GetMembers(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance).Where(m =>
        {
            var methodInfo = m as MethodBase;
            //Bug 662867: var skip = m.GetCustomAttribute<SkipAttribute>(true) != null;
            var skip = m.Name == "ToString";
            return (methodInfo == null || !methodInfo.IsConstructor && !methodInfo.IsSpecialName) && m.MemberType != MemberTypes.Field && !skip;
        }).ToArray();
    }

As you can see in the code above I am doing a specific check for !methodInfo.IsSpecialName which are the Get and Set methods of an Automatic Property. You can use this method to get the members of your object.

My project MonoTouch.MVVM which uses my version of MonoTouch.Dialog does not use fields and only properties. If you want to use attributes on fields remove the && m.MemberType != MemberTypes.Field in the code above.

Robert Kozak
  • 2,043
  • 17
  • 33
  • I went back and check the original MonoTouch.Dialog sources. Add my GetMembers() method and replace line 202 in Reflect.cs with this: var members = GetMembers(o); – Robert Kozak Jan 26 '11 at 19:53
0

I had to create actual backing fields (not use automatic properties), and then add the [Skip] attribute to them. Miguel would know if there's a better way though.

Rob Gibbens
  • 1,122
  • 1
  • 8
  • 24