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.

2

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

>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.
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.
Agree, data binding is the way to go
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.
Sad day for Xamarin =(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.