C# 根据C中的条件隐藏/取消隐藏按钮#

C# 根据C中的条件隐藏/取消隐藏按钮#,c#,asp.net,.net,xamarin,xamarin.forms,C#,Asp.net,.net,Xamarin,Xamarin.forms,我在Xamarin工作。在CredentialPage.xml页面中,有一个按钮,我想根据CredentialViewMode.cs页面中凭据的状态隐藏和取消隐藏该按钮 CredentialPage.xml <Button x:Name="Button_Round" WidthRequest="40" HeightRequest="40" CornerRadius="20" BorderWidth="

我在Xamarin工作。在CredentialPage.xml页面中,有一个按钮,我想根据CredentialViewMode.cs页面中凭据的状态隐藏和取消隐藏该按钮

CredentialPage.xml

<Button x:Name="Button_Round"
WidthRequest="40"
HeightRequest="40"
CornerRadius="20"
BorderWidth="2"
TextColor="White"
BorderColor="Teal"
BackgroundColor="Teal"
Text="Accept Offer"
Command="{Binding ProcessOffer}" />
要隐藏/取消隐藏按钮的条件

if(_credentialStatus == "Offered")
{
    // Button should be Visible
}
else
{
    // Hide the Button
}

使用IsVisible属性:

<Button x:Name="Button_Round"
WidthRequest="40"
HeightRequest="40"
CornerRadius="20"
BorderWidth="2"
TextColor="White"
BorderColor="Teal"
BackgroundColor="Teal"
Text="Accept Offer"
Command="{Binding ProcessOffer}" 
IsVisible="{Binding IsOfferButtonVisible}"
/>
<Button x:Name="Button_Round"
IsVisible={Binding CredentialVisible}
WidthRequest="40"
HeightRequest="40"
CornerRadius="20"
BorderWidth="2"
TextColor="White"
BorderColor="Teal"
BackgroundColor="Teal"
Text="Accept Offer"
Command="{Binding ProcessOffer}" />

使用IsVisible属性:

<Button x:Name="Button_Round"
WidthRequest="40"
HeightRequest="40"
CornerRadius="20"
BorderWidth="2"
TextColor="White"
BorderColor="Teal"
BackgroundColor="Teal"
Text="Accept Offer"
Command="{Binding ProcessOffer}" 
IsVisible="{Binding IsOfferButtonVisible}"
/>
<Button x:Name="Button_Round"
IsVisible={Binding CredentialVisible}
WidthRequest="40"
HeightRequest="40"
CornerRadius="20"
BorderWidth="2"
TextColor="White"
BorderColor="Teal"
BackgroundColor="Teal"
Text="Accept Offer"
Command="{Binding ProcessOffer}" />

将布尔属性添加到ViewModel中,例如:

private bool _credentialVisible;
public bool CredentialVisible
{
    get 
    {
        return _credentialVisible;
    }
    set
    {
       if (_credentialVisible != value)
       {
           _credentialVisible = value;
           NotifyPropertyChanged();
       }
    }
}
您的视图模型应该实现接口
INotifyPropertyChanged

public event PropertyChangedEventHandler PropertyChanged;

protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
然后在视图中绑定到
IsVisible
属性:

<Button x:Name="Button_Round"
WidthRequest="40"
HeightRequest="40"
CornerRadius="20"
BorderWidth="2"
TextColor="White"
BorderColor="Teal"
BackgroundColor="Teal"
Text="Accept Offer"
Command="{Binding ProcessOffer}" 
IsVisible="{Binding IsOfferButtonVisible}"
/>
<Button x:Name="Button_Round"
IsVisible={Binding CredentialVisible}
WidthRequest="40"
HeightRequest="40"
CornerRadius="20"
BorderWidth="2"
TextColor="White"
BorderColor="Teal"
BackgroundColor="Teal"
Text="Accept Offer"
Command="{Binding ProcessOffer}" />
然后您可以按如下方式使用它:

<ContentPage.Behaviors>
    <behaviors:EventHandlerBehavior EventName="Appearing">
        <behaviors:InvokeCommandAction Command="{Binding AppearingCommand}" />
    </behaviors:EventHandlerBehavior>
</ContentPage.Behaviors>

将布尔属性添加到ViewModel中,例如:

private bool _credentialVisible;
public bool CredentialVisible
{
    get 
    {
        return _credentialVisible;
    }
    set
    {
       if (_credentialVisible != value)
       {
           _credentialVisible = value;
           NotifyPropertyChanged();
       }
    }
}
您的视图模型应该实现接口
INotifyPropertyChanged

public event PropertyChangedEventHandler PropertyChanged;

protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
然后在视图中绑定到
IsVisible
属性:

<Button x:Name="Button_Round"
WidthRequest="40"
HeightRequest="40"
CornerRadius="20"
BorderWidth="2"
TextColor="White"
BorderColor="Teal"
BackgroundColor="Teal"
Text="Accept Offer"
Command="{Binding ProcessOffer}" 
IsVisible="{Binding IsOfferButtonVisible}"
/>
<Button x:Name="Button_Round"
IsVisible={Binding CredentialVisible}
WidthRequest="40"
HeightRequest="40"
CornerRadius="20"
BorderWidth="2"
TextColor="White"
BorderColor="Teal"
BackgroundColor="Teal"
Text="Accept Offer"
Command="{Binding ProcessOffer}" />
然后您可以按如下方式使用它:

<ContentPage.Behaviors>
    <behaviors:EventHandlerBehavior EventName="Appearing">
        <behaviors:InvokeCommandAction Command="{Binding AppearingCommand}" />
    </behaviors:EventHandlerBehavior>
</ContentPage.Behaviors>

粘贴代码后,按钮隐藏,但如果状态更改,它仍然隐藏。。为什么?因为代码需要再次运行。是一个事件改变了它吗?如果是,那么在这种情况下,您应该重新检查值并重新分配可绑定属性,我添加了
公共bool IsOfferButtonVisible{get;private set;}
…我会看一看,看看是否可以:)粘贴代码后,按钮会隐藏,但如果状态更改,仍然会隐藏。。为什么?因为代码需要再次运行。是一个事件改变了它吗?如果是,那么在这种情况下,您应该重新检查值并重新分配可绑定属性,我添加了
公共bool IsOfferButtonVisible{get;private set;}
…我会看看是否可以:)谢谢您的回复,@Athanasios Kataras的回复更容易理解。谢谢您的回复,@Athanasios Kataras的答案更容易理解。