3

The need to call FindViewById is a really messed up way to get the View inside an Activity. It may cause an exception at runtime, if the view does not exist in the layout. Is there a layout generator to generate the Activity with all known Views as member variables (like in Windows.Forms or better WPF) in order to get Activities typesafe?

Cons

  • It's not typesafe
  • It's timeconsuming to implement
  • Not error prone, causing exceptions at runtime
  • Writing a lot of boilerplate code

Advantages

  • May have lower memory impact, when there are a lot unused Views, that don't need a member variable

  • A little better load performance.

So that instead of this:

EditText _editText;

// ...

_editText = FindViewById(Resource.Id.editText1);

_editText.Text = "Hallo World!";

I end up with just this:

_editText.Text = "Hallo World!";

The prefered way would be to utilize Androids Data Binding. But this is not available for Xamarin.

Community
  • 1
  • 1
Redwolf
  • 540
  • 4
  • 17
  • Consider looking at [Cheeseknife](https://github.com/MarcelBraghetto/Cheeseknife), a nice binding library that solves _some_ of these issues. – matthewrdev Aug 12 '16 at 22:14
  • MFractor also supports validation for missing ids using the **MFractor.Annotations** library. See http://www.mfractor.com/code-analysis-using-mfractor-annotations/ – matthewrdev Aug 12 '16 at 22:16

1 Answers1

1

This is a really open ended question. There are many ways that can achieve type safety and obtain views.The best and most acceptable way is to use inflation at runtime, by putting the type you want into the views:

A self defined ViewGroup can be inflated into a Container. This is done programmatically and you have "code-defined types" rather than expected these types to exist in your XML document, or however you are getting them. Look at :How to Programmatically Add Views to Views for implementation specifics (the same in Xamarin with some uppercases and what not).

You shouldn't really be making anything that will be problematic based off of the way you are calling functions and etc. I'd say really take a look at the way you are programming and make it more standard to the way Google(Android) and Microsoft(Xamarin) wants you to, and you won't run into as many problems like this. (I know this isn't helpful, but the question is super generic so just understanding the frameworks will make you come to good solution.)

Another great way is using the data binding Api's offered in Android and Xamarin : Android's Data Binding

Community
  • 1
  • 1
jStaff
  • 650
  • 1
  • 9
  • 25
  • >make it more standard to the way Google(Android) and Microsoft(Xamarin) wants you to, and you won't run into as many problems like this. <-- This is what I'm doing, but the default way is really time consuming. – Redwolf Aug 12 '16 at 18:52
  • They released a data binding api for Xamarin and also Android. If you really want to eliminate the time. Look it up. It is similar to data binding in HTML but for XML. Simple. – jStaff Aug 12 '16 at 19:12
  • Agree, data binding is the way to go – Redwolf May 30 '17 at 20:04
  • The problem is androids data binding through gradle is not supported by Xamarin. One has to rely on third party apps or implement it the old way. – Redwolf Jun 30 '17 at 12:06
  • Sad day for Xamarin =( – jStaff Jul 12 '17 at 14:32