Xamarin中状态更改时隐藏按钮

时间:2020-07-03 16:25:48

标签: c# .net visual-studio xamarin xamarin.forms

我实现了一个按钮,该按钮需要一个功能。该功能实际上将请求发送到服务器以发出证书。当用户收到凭据时,凭据状态为已提供,但是当他/她单击此按钮时,它将向服务器发送请求,并且按钮状态更改为已接收

我只想在“按钮状态”为“ 已提供”时显示按钮。

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 => CreateRequestAsync()是向服务器发送请求的服务器。成功发送请求后,我想隐藏按钮。

    public ICommand ProcessOffer => new Command(async () =>
    {
        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("Pool is not correctly configured. Please add proper genesis file.\n("+e.Message+")","Pool Error","Ok");
        }
    });

如果您可以指导我,那么您会很友善:)

1 个答案:

答案 0 :(得分:3)

IsVisible属性绑定到VM属性

<Button x:Name="Button_Round"  IsVisible="{Binding ButtonVisible}" ... />

然后在您的VM中(您的VM必须实现INotifyPropertyChanged)

private bool _ButtonVisible = true;

public bool ButtonVisible
{
  get {
    return _ButtonVisible;
  }
  set {
     _ButtonVisible = value;
     PropertyChanged("ButtonVisible");
  }
 }

然后,只要您收到服务器的响应,就可以将ButtonVisible设置为适当的值