XamarinAndroid 自动绑定View变量

Android 编程时我们少不了使用FindIdByView函数,在Xamarin Android开发时也需要如此。这个工作很无聊且烦人。在常规Android开发中,人们已经发明了一些方法免除这项工作,例如Android数据绑定,还有butterknife,都可以解决这个问题。关于它们我不去细说了。但是这两个方案在Xamarin Android开发里还无法使用。本文介绍一个函数,就可以把程序员从这项工作中解脱出来,原文请参见http://redth.codes/auto-wire-up-views-xamarin-activity/。

下面直接上代码吧。

这段是一个Activity.

 1 using Android.App;
 2 using Android.Widget;
 3 using Android.OS;
 4 
 5 namespace AndApp2
 6 {
 7     [Activity(Label = "AndApp2", MainLauncher = true, Icon = "@drawable/icon")]
 8     public class MainActivity : Activity
 9     {
10         int count = 1;
11         Button myButton;
12         TextView txtName;
13 
14         protected override void OnCreate(Bundle bundle)
15         {
16             base.OnCreate(bundle);
17             this.BindView(Resource.Layout.Main);
18 
19             myButton.Click += (s,e)=> {
20                 myButton.Text = string.Format("{0} clicks!", count++);
21                 txtName.Text = "Bruce";
22             };
23         }
24     }
25 }


其中BindView是一个扩展方法,包括SetContent和将View绑定到本Activity的变量上。
Layout中有一个Button myButton和一个TextView txtName.

下面是BindView扩展方法:


 

 1 public static class ActivityExtensions
 2     {
 3         public static void BindView(this Activity activity, int layoutResId)
 4         {
 5             activity.SetContentView(layoutResId);
 6             //Get all the View fields from the activity
 7             var members = from m in activity.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
 8                           where m.FieldType.IsSubclassOf(typeof(View))
 9                           select m;
10 
11             if (!members.Any())
12                 return;
13 
14             members.ToList().ForEach(m =>
15             {
16                 try
17                 {
18                     //Find the android identifier with the same name
19                     var id = activity.Resources.GetIdentifier(m.Name, "id", activity.PackageName);
20                     //Set the activity field's value to the view with that identifier
21                     m.SetValue(activity, activity.FindViewById(id));
22                 }
23                 catch (Exception ex)
24                 {
25                     throw new MissingFieldException("Failed to wire up the field "
26                                                      + m.Name + " to a View in your layout with a corresponding identifier", ex);
27                 }
28             });
29         }
30     }

该方法使用了反射,会有人担忧其性能。但是对于一个Activity中具有不多的View来说可以忽略不计。
当然,也不是一点问题都没有。原文已经指出了,如果想使用一个在layout中没有对应的View时会抛出异常。这个问题其实很好解决。例如,修改一下BindView函数,让我们约定凡是自动绑定的view变量都以m开头,其他的变量不以m开头就好了。当让我们也可以采取在变量上加注解的方案,这样也能解决问题,至于具体怎么弄,就不在这里说了,到此为止,我觉得现在已经足够好了。

原文地址:https://www.cnblogs.com/brucepark/p/6659724.html