Improve your Xamarin Development

Vipin Johney
4 min readJun 12, 2021

--

Some of the important points which should be kept in might while you develop a Xamarin application.

1. All UI changes has to be called from the Main Thread

- Application user interfaces are always single-threaded. Even in multi-threaded devices — there’s only one representation of the screen and any changes to what is displayed need to be coordinated through a single ‘access point’.

- This prevents multiple threads from trying to update the same pixel at the same time. So, your code should only make changes to user interface controls from the main (or UI) thread.

- Any UI updates that occur on a different thread (such as a callback or background thread) may not get rendered to the screen, or could even cause a crash.

- If code is executing on a background thread, in a task or a callback then it is likely NOT executing on the main UI thread. In this case you should wrap the code in a call to InvokeOnMainThread or BeginInvokeOnMainThread like this:

InvokeOnMainThread ( () => {
// manipulate UI controls
});

- Example of BG thread

new System.Threading.Thread(new System.Threading.ThreadStart(() => {
label1.Text = “updated in thread”; // should NOT reference UILabel on background thread!
})).Start();

- Example on how to handle the above case

new System.Threading.Thread(new System.Threading.ThreadStart(() => {
InvokeOnMainThread (() => {
label1.Text = “updated in thread”; // this works!
});
})).Start();

- Reference

https://docs.microsoft.com/en-us/xamarin/ios/user-interface/ios-ui/ui-thread

2. Avoid blocking the Main Thread

- If we block the main thread, the UI functions will have to wait till the blocking function completes execution and our app won’t work smoothly and its performance will suffer.

- So, to avoid using the main thread to perform heavy operations, we can either use async/await or background workers to do them.

- We should use the main thread only to update the UI.

3. Always follow MVVM patters

- Model-view-viewmodel pattern is used to separate domain logic and the presentation layer.

- By ignoring the MVVM pattern, we cannot reuse code. This makes it difficult to maintain and test the code, as some developers will implement it in the code-behind without using commands or data binding.

- The main advantages of using MVVM are that it makes code easy to reuse, work out, maintain, and test. Once we implement code in the ViewModel, we can use the same ViewModel in multiple projects.

4. Avoid repeating the same template code or view in multiple places

- When writing a code snippet which will be used at different places, always move it to separate function. And invoke the function from all the places that require it.

- Same goes with UI, if there is a UI template that is used at multiple places, create a custom class for it and reuse.

- Doing so would make it easy to manage and make changes.

5. Be knowledgeable of options that make coding easy and speed up the implementation

- For example, when designing UI, if the same style needs to be applied at different views, use <style> and reuse the style object.

- Likewise, C# also provides many functions and data types which can help us at certain times.

- So always research for the options when you have implement a complex task.

6. Do not use Libraries or NuGets that are outdated

- When developing an application, make sure you are using the latest versions of libraries and IDE in all platforms.

- When we use older versions in our application, there is a chance that it might not function in the newer versions of the platforms.

- Always verify that the Library or NuGet packages support the version of the OS the application is targeting. This can be checked in the information page provides by the Library or NuGet.

- If you don’t verify before implementing, Application may not work as intended or might crash or may get rejected while we upload to App Stores.

- Sometimes we might have to add a Libraries which only supports certain versions of the OS. In this case before using the library function, verify if the application is running on the version of the OS supported by the library.

For example, when library only supports Android Lollipop and above :

If (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
// Call Function supported by Lollipop and above
}

7. Follow proper code formatting

- Coding standards should always be followed.

- Proper formatting and indentation is must.

- Follow as mentioned in the instruction documents provided by client or the standards mentioned in documentations provided by Microsoft for C# or respective documentations for other languages.

- Always add comments where ever necessary

8. Always be aware of the features and changes is the latest version releases

- Read through documentations , blogs and news on the latest releases and be aware of the features available for the new releases

- This goes for both OS and IDE releases and updates.

- It is important to be also aware for library or function deprecations. If a library or NuGet is deprecated for a version of the OS, application might crash when the library functions are invoked in the code.

9. Monitor the errors displayed when you build and deploy

- Whenever you build the app or deploy the app on a device, the IDE (Visual Studio) will display logs of the process. These logs will also display errors and warnings. Keep an eye on it and always check if there is any major issues mentioned in the logs.

- Some of the warnings may not cause issue on the test device or simulator currently used by the developer, but can cause major problems on a different set of devices.

- Be aware that the application will be running on different set of devices that have difference configurations.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Vipin Johney
Vipin Johney

Written by Vipin Johney

Coding | Mobile App | Native | Xamarin |

No responses yet

Write a response