简体   繁体   中英

Image IsEnabled with Binding Property Does not work Xamarin Forms

Using Xamarin forms -pcl v 2.3.4.267 -Debug on Android Device

I have an Image that is being used as button

          <Image Source="loginbutton.png"           
           Aspect="AspectFit"
           HorizontalOptions="Fill"
           Margin="50,20,50,0"
           fe:TappedGestureAttached.Command="{Binding Login}"
           IsVisible ="{Binding user.IsSubmitEnabled}"<---works fine
           IsEnabled="{Binding user.IsSubmitEnabled}"<---Does nothing
           /> 

as i mentioned in the code the Is Visible Works Great But Is Enabled does nothing.

note:-if there is any workaround please share it .

This is a known issue in Xamarin, already reported here and should be fixed in a future version of Xamarin.Forms, more specifically version 2.4.0-pre .

As a Workaround you can use the IsSubmitEnabled as the parameter for the CanExecute parameter in your Command .

Something like this:

public MyViewModel()
{
    Login = new Command(() => OnLogin(), () => IsSubmitEnabled);
}

But you will need to add a line Login.CanExecute(null); in your Property setter too.

private bool _isSubmitEnabled;
public bool IsSubmitEnabled
{
    get { return _isSubmitEnabled; }
    set
    {
        _isSubmitEnabled= value;
        RaisePropertyChanged(nameof(IsSubmitEnabled));
        Login.CanExecute(null);
    }
}

This should work in the mean time. Till the fix is in production.

Note : just for information, this issue seems only to be happening on Android while on iOS seems to be working correctly.

Hope this helps.-

Are you using both statements in parallel?

IsVisible ="{Binding user.IsSubmitEnabled}"<---works fine
IsEnabled="{Binding user.IsSubmitEnabled}"<---Does nothing

Then IsEnabled=false is only active if the button is invisible, because both are binding to the same boolean property => IsSubmitEnabled.

Maybe you have to use a second boolean binding property?

If you want unclickable when you enable property false you do as following

As First binding property of IsVisible and IsEnable must be different.

<Image Source="loginbutton.png"           
           Aspect="AspectFit"
           HorizontalOptions="Fill"
           Margin="50,20,50,0"
           fe:TappedGestureAttached.Command="{Binding Login}"
           IsVisible ="{Binding user.IsSubmitVisible}"
           IsEnabled="{Binding user.IsSubmitEnabled}"
           />

And you change the code in ViewModel like:

 public void Login()
    {
        If(IsSubmitEnabled){
           // Put your code here
        }
    }

According to this post , if we bind IsEnabled property before binding commands, the properties wont' trigger. I ran in to same problem and moved IsEnabled binding after Command binding, and IsEnabled property was set correctly.

Hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM