【问题标题】:Hide the Button when the Status Changed in Xamarin在 Xamarin 中状态更改时隐藏按钮
【发布时间】:2020-10-24 09:49:05
【问题描述】:

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

我只想在按钮状态为Offered时显示按钮。

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");
        }
    });

如果你能指导我,你会很友善:)

【问题讨论】:

  • 创建一个你的按钮可以观察到启用/禁用的布尔属性,然后在你的函数调用之前/之后设置该布尔属性
  • 你能用一点代码指导我吗,我对xamarin不太了解。
  • @MattGreen 您好,如果回复有帮助,请不要忘记接受它作为答案(点击此答案左上角的✔),它将帮助有类似问题的其他人。 :-)

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


【解决方案1】:

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 设置为适当的值

【讨论】:

  • 我收到此错误:“将 'PropertyChanged' 更改为 'IReactiveNotifyPropertyChanged'”
  • 你在使用响应式框架吗?如果是这样,您应该阅读他们关于使用 PropertyChanged 的​​文档
猜你喜欢
  • 1970-01-01
  • 2017-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多