Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Binding for Event Handlers

$
0
0

I'm having trouble creating a data binding for an event (like Clicked on button).

The control is implemented with XAML and code behind. I didn't think the XAML was important so I didnt include it here. I've also excluded other bindings since they work and I didn't want to make the question more complicated.

public delegate void DiceRollCancelled();
public partial class DiceRoll : ContentView
{

    public static readonly BindableProperty OnDiceRollCancelledProperty = 
        BindableProperty.Create<DiceRoll, DiceRollCancelled> (w => w.OnDiceRollCancelled, null);

    public DiceRollCancelled OnDiceRollCancelled
    {           
        get { return (DiceRollCancelled)GetValue (OnDiceRollCancelledProperty); }
        set { SetValue (OnDiceRollCancelledProperty, value); }                          
    }   

    private void FireCancelled()
    {
        DiceRollCancelled eventHandler = OnDiceRollCancelled;
        if (null == eventHandler)
            return;

        //  OnDiceRollCancelled is always null!!!
    }   
}

To use, I include the control and bind the event to public method on my ViewModel

public class Stage3SellViewModel 
{
    public void DiceRollCancelled()
    {
        FireDisplayAlert("Dice Roll Cancelled", "got it", "OK");                
    }
}

The XAML for the view is

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:cv="clr-namespace:DecisivePower.Views;assembly=DecisivePower"
             x:Class="DecisivePower.Views.Stage3SellView"
             >

    <cv:DiceRoll OnDiceRollCancelled="{Binding DiceRollCancelled}" />
</ContentView>

I'm pretty sure the ViewModel is getting correctly bound to the View. Other bindings to values like int work. I'm thinking the problem is either how my ViewModel implements the event handler or how the control itself creates it.

Any ideas?

Matt


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>