Giter Site home page Giter Site logo

2urbo / xamarin.bindableproperty.fody Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nicolo-ottaviani/xamarin.bindableproperty.fody

0.0 1.0 0.0 435 KB

An assembly weaver, based on Fody, that automatically generates bindable properties on a Xamarin Forms project.

License: MIT License

C# 100.00%

xamarin.bindableproperty.fody's Introduction

Xamarin.BindableProperty.Fody

An assembly weaver, based on Fody, that automatically transforms plain auto-implemented properties into BindableProperties that can be used in Xamarin Forms.

Installation

Compile the BindableProperty project in order to build the NuGet package (it is in the "nuget" directory in the solution folder). Add this nuget package to your prject. Then, add a file called FodyWeavers.xml in your project root (build action must be set to Content), and write the following inside:

<?xml version="1.0" encoding="utf-8"?>
<Weavers>
    <BindableProperty />
</Weavers>

See the Fody documentation for more info on the FodyWeavers.xml file.

Usage

Just decorate an auto-implemented get/set property with the Bindable attribute.

For example, this:

public class MyButton : Xamarin.Forms.Button
{
    [Bindable]
    public string FontFamily { get; set; }
}

will become like this:

public class MyButton : Xamarin.Forms.Button
{
    public static readonly BindableProperty FontFamilyProperty = BindableProperty.Create(nameof(FontFamily), typeof(string), typeof(Button), null, null);
    public string FontFamily { get { return (string)GetValue(FontFamilyProperty); } set { SetValue(FontFamilyProperty, value); } }
}

The public static readonly XxxProperty is automatically generated if not present. The getter and setter methods are automatically implemented with GetValue(...) and SetValue(...) methods, respectively. The Bindable attribute is removed from the resulting dll.

If you want some piece of code to be called whenever the property value changes, simply add a void OnXxxChanged method, where Xxx is the name of the property. It will be registered on the ValueChanged delegate in the BindableProperty.Create method:

public class MyButton : Xamarin.Forms.Button
{
    [Bindable]
    public string FontFamily { get; set; }
    
    private void OnFontFamilyChanged(string newValue) { 
        //do stuff here
        Console.WriteLine("Property value is changed");
    }
}

The OnXxxChanged method must have only one parameter (whose type must match the type of the property) that will contain the new value. The OnXxxChanged method can be public or private as well.

Other info

The actual weaver code is in the BindablePropery.Fody project, while the BindableProperty project serves to build the NuGet package.

xamarin.bindableproperty.fody's People

Contributors

nicolo-ottaviani avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.