简体   繁体   English

具有绑定属性的图像IsEnabled不能使用Xamarin表单

[英]Image IsEnabled with Binding Property Does not work Xamarin Forms

Using Xamarin forms -pcl v 2.3.4.267 -Debug on Android Device 在Android设备上使用Xamarin表单-pcl v 2.3.4.267 -Debug

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. 正如我在代码中提到的那样,Is Visible Works Great但是Enabled什么也没做。

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 . 这是Xamarin中的一个已知问题,已在此处报告,应该在未来版本的Xamarin.Forms中修复,更具体地说是版本2.4.0-pre

As a Workaround you can use the IsSubmitEnabled as the parameter for the CanExecute parameter in your Command . 作为解决方法,您可以使用IsSubmitEnabled作为Command CanExecute参数的参数。

Something like this: 像这样的东西:

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

But you will need to add a line Login.CanExecute(null); 但是你需要添加一行Login.CanExecute(null); in your Property setter too. 在您的Property setter中也是如此。

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. 注意 :仅仅是为了获取信息,这个问题似乎只发生在Android上,而在iOS上似乎正常工作。

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. 然后IsEnabled = false仅在按钮不可见时才有效,因为两者都绑定到相同的布尔属性=> IsSubmitEnabled。

Maybe you have to use a second boolean binding property? 也许你必须使用第二个布尔绑定属性?

If you want unclickable when you enable property false you do as following 如果您在启用属性false时想要不可点击,则执行以下操作

As First binding property of IsVisible and IsEnable must be different. 由于IsVisibleIsEnable的第一个绑定属性必须不同。

<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: 并且您在ViewModel中更改代码,如:

 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. 根据这篇文章 ,如果我们在绑定命令之前绑定IsEnabled属性,属性将不会触发。 I ran in to same problem and moved IsEnabled binding after Command binding, and IsEnabled property was set correctly. 我遇到了同样的问题,并在Command绑定后移动了IsEnabled绑定,并正确设置了IsEnabled属性。

Hope that helps 希望有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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