Behaviors let you add functionality to user interface controls without having to subclass them. Instead, the functionality is implemented in a behavior class and attached to the control as if it was part of the control itself. This article provides an introduction to behaviors.

Behaviors enable you to implement code that you would normally have to write as code-behind, because it directly interacts with the API of the control in such a way that it can be concisely attached to the control and packaged for reuse across more than one application. They can be used to provide a full range of functionality to controls, such as:

  • Adding an email validator to an Entry.
  • Creating a rating control using a tap gesture recognizer.
  • Controlling an animation.
  • Adding an effect to a control.

Behaviors also enable more advanced scenarios. In the context of commanding, behaviors are a useful approach for connecting a control to a command. In addition, they can be used to associate commands with controls that were not designed to interact with commands. For example, they can be used to invoke a command in response to an event firing.

Xamarin.Forms supports two different styles of behaviors:

  • Xamarin.Forms behaviors – classes that derive from the Behavior or Behavior<T> class, where T is the type of the control to which the behavior should apply. For more information about Xamarin.Forms behaviors, see Xamarin.Forms Behaviors and Reusable Behaviors.
  • Attached behaviors  static classes with one or more attached properties. For more information about attached behaviors, see Attached Behaviors.

This guide focuses on Xamarin.Forms behaviors because they are the preferred approach to behavior construction.

 

동작을 사용하면 서브클래스 없이도 사용자 인터페이스 컨트롤에 기능을 추가할 수 있습니다. 대신 기능은 동작 클래스에서 구현되고 컨트롤 자체의 일부였던 것처럼 컨트롤에 연결됩니다. 이 문서에서는 동작을 소개합니다.

동작을 사용하면 코드가 컨트롤에 간결하게 첨부되고 둘 이상의 애플리케이션에서 재사용하도록 패키지될 수 있도록 컨트롤의 API와 직접 상호 작용하기 때문에, 대개 코드 숨김으로 작성해야 하는 코드를 구현할 수 있습니다. 다음과 같은 컨트롤에 광범위한 기능을 제공하는 데 사용할 수 있습니다.

  • 이메일 유효성 검사기를 Entry에 추가합니다.
  • 탭 제스처 인식기를 사용하여 등급 컨트롤을 만듭니다.
  • 애니메이션을 제어합니다.
  • 컨트롤에 효과를 추가합니다.

또한 동작은 더 고급 시나리오를 사용하도록 설정합니다. 명령 컨텍스트에서 동작은 명령에 컨트롤을 연결하는 데 유용한 접근 방식입니다. 또한 동작은 명령과 상호 작용하도록 설계되지 않은 컨트롤과 명령을 연결하는 데 사용할 수 있습니다. 예를 들어, 발생하는 이벤트에 대한 응답에서 명령을 호출하는 데 사용할 수 있습니다.

Xamarin.Forms는 동작의 두 가지 다른 스타일을 지원합니다.

  • Xamarin.Forms 동작  Behavior 또는 Behavior<T> 클래스에서 파생된 클래스입니다. 여기서 T는 동작이 적용되어야 하는 컨트롤의 형식입니다. Xamarin.Forms 동작에 대한 자세한 내용은 Xamarin.Forms 동작  재사용 가능한 동작을 참조하세요.
  • 연결된 동작 – 첨부된 속성이 하나 이상 있는 static 클래스입니다. 연결된 동작에 대한 자세한 내용은 연결된 동작을 참조하세요.

이 가이드는 Xamarin.Forms 동작에 집중합니다. 이는 동작 구성에 선호되는 접근 방식이기 때문입니다.

+ Recent posts