Xamarin 将IsVisible属性绑定到我的VIewModel

Xamarin 将IsVisible属性绑定到我的VIewModel,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我有一些条目,在该条目的焦点上,我想显示我的取消按钮 以下是xaml: <RelativeLayout> <controls:StandardEntry x:Name="mainEntry" BackgroundColor="White" BorderColor=

我有一些条目,在该条目的焦点上,我想显示我的取消按钮

以下是xaml:

 <RelativeLayout>
                <controls:StandardEntry
                        x:Name="mainEntry"
                        BackgroundColor="White"
                        BorderColor="Gray"
                        BorderThickness="0"
                        CornerRadius="15"
                        Placeholder="Search..."
                        TextColor="LightGray"
                        HeightRequest="10"
                        Padding="35,0"
                        FontSize="Default"
                        RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height,Factor=0,Constant=40}"
                        RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=0.7,Constant=0}">
                    <Entry.Behaviors>
                        <behavior:EventToCommandBehavior EventName="Focused" Command="{Binding SearchBarFocusedCommand}"/>
                        <behavior:EventToCommandBehavior EventName="Unfocused" Command="{Binding SearchBarUnfocusedCommand}"/>
                    </Entry.Behaviors>
                </controls:StandardEntry>
                <Image 
                        Source="Invest_Search_Icon.png" 
                        VerticalOptions="Center"
                        RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToView,ElementName=mainEntry, Property=X,Factor=1,Constant=10}"
                        RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToView, ElementName=mainEntry, Property=Y,Factor=1,Constant=10}"/>
                <Image 
                        Source="Invest_Search_Icon.png"
                        VerticalOptions="Center"
                        RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToView,ElementName=mainEntry, Property=Width,Factor=1,Constant=-25}"
                        RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToView, ElementName=mainEntry, Property=Y,Factor=1,Constant=10}"/>
                <Button 
                        Text="Cancel" 
                        TextColor="Gray"
                        IsVisible="{Binding CancelButtonIsVisible}"
                        BackgroundColor="White"
                        VerticalOptions="Start" 
                        CornerRadius="10" 
                        HeightRequest="40" 
                        Margin="0,0,50,0"
                        RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToView,ElementName=mainEntry, Property=Width,Factor=1,Constant=20}"
                        RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToView, ElementName=mainEntry, Property=Y,Factor=1,Constant=0}"/>
            </RelativeLayout>
因此,流程:

  • 在页面加载时,第一个SetDefaultContent()=>
    CancelButtonNisVisible=true
  • 在以条目为焦点时,隐藏SearchBarFocused()上的取消按钮=>
    CancelButtonIsVisible=false
  • 显然,SetDefaultContent正在工作

    它不起作用我的对焦方法,当我对焦时什么都没有发生,仍然有可见的取消按钮

    有什么建议吗?

    选项1 更简单的选项是使用命名引用将“取消”按钮上的
    IsVisible
    属性绑定到条目(maintry)控件上的
    IsFocused
    属性

    <Button Text="Cancel" 
         IsVisible="{Binding IsFocused, Source={x:Reference mainEntry}, 
                         Converter={StaticResource NegateBooleanConverter}}" />
    
    备选方案2 另一个选项是使用视图模型属性绑定
    IsFocused

    在viewmodel中使用setter创建属性

    public bool IsEntryFocused
    {
       set
       {
          CancelButtonIsVisible = !value;
       }
    }
    
    并在视图中设置绑定

    <controls:StandardEntry
         x:Name="mainEntry"
         ...
         IsFocused="{Binding IsEntryFocused}"
    

    您说命令实际得到执行(我假设您在OnSearchBarFocused中设置了一个断点,它被命中了?),因此显然vm被正确地设置为绑定上下文(您的代码没有显示发生这种情况的位置)。您的代码也没有显示RaisePropertyChanged的实现,因此很难知道它是否正常工作。另外,您是否考虑过只使用搜索栏控件?
    
    public bool IsEntryFocused
    {
       set
       {
          CancelButtonIsVisible = !value;
       }
    }
    
    <controls:StandardEntry
         x:Name="mainEntry"
         ...
         IsFocused="{Binding IsEntryFocused}"