Posts

Showing posts from February, 2023

Behaviors -.Net MAUI

.Net MAUI Behaviors allow you to enhance the functionality of a control without sub classing. Instead, the functionality is implemented in a behavior class and attached to the control as if it was part of the control itself. .Net MAUI Behaviors are similar to Xamarin.Forms Behaviors . Behaviors enables you code reusability across application and helps you to avoid writing code in code behind instead we can maintain code in a behavior class. They can be used to provide full functionality to controls like Event to command behaviours Entry Validators Handling events raised in a control etc There are 2 types of behaviors Attached behaviors .NET MAUI behaviors Attached behaviors Attached behaviors are static classes with one or more attached properties. An attached property is a special type of bindable property or a custom property. They are recognizable in XAML as attributes . Let’s create an attached behavior “MaxLengthValidatorBehavior” for entry . It changes text colour to red if chara

App LifeCycle in .Net MAUI

.Net MAUI application lifecycle includes four states Not Running Running Deactivated Stopped The execution state of an app depends on the app’s history. For example, when an app is installed for the first time, or a device is started, the app can be considered to be  not running  and it isn’t loaded in the memory yet . When the app is opened it goes to  running  state.If the other apps window gains focus it goes to  deactivated  state.if user returns back to apps window it goes to running state again.if user goes to home screen or terminates the application that our app window is no longer visible then app goes to  stopped  state. Managing the app lifecycle Cross Platform lifecycle events .NET MAUI raises cross-platform lifecycle events on the  Window  class when an app transitions from one state to other states.Below are the events raised Created:  This event is raised after the native window has been created. At this point the cross-platform window will have a native window handler,

Invoke Platform Specific Code in .NET MAUI

Image
Invoke Platform Specific Code in .NET MAUI .Net MAUI is the evolution of Xamarin forms. In Xamarin forms we can invoke platform specific code using Dependency Services but we all know that MAUI follows single project approach to provide better performance and productivity. We can invoke Platform Specific Code by following below 2 approaches Using Dependency Injection Using Partial Classes and Partial Methods Using Dependency Injection Dependency Injection is a form of Inversion of Control(IOC) in which an object receives other objects that it depends on at run time. Lets Invoke Platform specific code in .Net MAUI using Dependency Injection by taking an example that provides us device model in Andriod and IOS Step1: Create an Interface  IDeviceInfoService outside of platforms folder namespace MAUISample { public interface IDeviceInfoService { string GetDeviceModel () ; } } Step2: Create a service at each platform level that inherits IDeviceInfo