微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决根据C#中的条件隐藏/取消隐藏按钮

我正在使用Xamarin.Forms。在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}" />

Credentialviewmodel.cs

#region Bindable Command
[Obsolete]
public ICommand ProcessOffer => new Command(async () =>
{
    var RegisteredPIN = await SecureStorage.GetAsync("RegisteredPIN");
    string PIN = await App.Current.MainPage.displayPromptAsync("Enter PIN",null,"Ok","Cancel",6,Keyboard.Numeric);
    if (PIN == RegisteredPIN)
    {
        try
        {
            //await _poolConfigurator.ConfigurePoolsAsync();
            var agentContext = await _agentContextProvider.GetContextAsync();
            var credentialRecord = await _credentialService.GetAsync(agentContext,_credential.Id);
            var connectionId = credentialRecord.ConnectionId;
            var connectionRecord = await _connectionService.GetAsync(agentContext,connectionId);
            (var request,_) = await _credentialService.CreateRequestAsync(agentContext,_credential.Id);
            await _messageService.SendAsync(agentContext.Wallet,request,connectionRecord);
            await DialogService.AlertAsync("Request has been sent to the issuer.","Success","Ok");
        }
        catch (Exception e)
        {
            await DialogService.AlertAsync(e.Message,"Error","Ok");
        }
    }
    else if (PIN != RegisteredPIN && PIN != null)
    {
        DialogService.Alert("Provided PIN is not correct");
    }
});

#endregion

我要在其上隐藏/取消隐藏按钮的条件

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}"
/>

然后在您的代码后面

if(_credentialStatus == "Offered")
{
    IsOfferButtonVisible  = true;
}
else
{
    // Hide the Button
    IsOfferButtonVisible = false;
}
,

向您的ViewModel添加bool属性,例如:

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"
IsVisible={Binding CredentialVisible}
WidthRequest="40"
HeightRequest="40"
CornerRadius="20"
BorderWidth="2"
TextColor="White"
BorderColor="Teal"
BackgroundColor="Teal"
Text="Accept Offer"
Command="{Binding ProcessOffer}" />

您没有指定何时需要检查按钮是否应该可见,但是我想在加载页面时需要它,在这种情况下,您需要一种行为来在页面出现时触发命令,而不是自己编写,可以使用以下NuGet:https://www.nuget.org/packages/Behaviors.Forms/

安装后,将名称空间添加到您的ContentPage

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:behaviors="clr-namespace:Behaviors;assembly=Behaviors"

然后您可以按以下方式使用它:

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

页面出现时,将调用方法AppearingCommand,您可以在此处设置按钮的可见性:

public ICommand AppearingCommand => new Command(() =>
{
    if(_credentialStatus == "Offered")
    {
        CredentialsVisible = true;
    }
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。