即使我根据 xamarin 表单中的标签内容刷新页面,如何使按钮不可见

How to make a button invisible even when I refresh a page depending on a label content in xamarin forms

当我点击按钮时它会隐藏,但是当我刷新页面时它仍然存在。如何根据标签中的内容使其隐藏?

 private void Button3_Clicked(object sender, EventArgs e)
        {
            App.products[Index].Quantity++;
            Button btn = (Button)sender;
            if (btn.IsVisible)
            { btn.IsVisible = false; }
            else
            {
                btn.IsVisible = false;

            }
        }

我希望根据此值 App.products[Index].Quantity 刷新页面时此 Button 保持隐藏状态。当我单击 Button 时,它从 0 变为 1,如果它不是 0,我想隐藏 Button。

在YourPage.xaml中:

    <Button IsVisible={Binding ButtonIsVisible} ... />

在YourPage.xaml.cs:

public YourPage()
{
    InitializeComponent();
    ...
    BindingContext = this;
}

// NOTE: `=>` not `=`. To be a property; expression evaluated every time it is needed.
public bool ButtonIsVisible => App.products[Index].Quantity == 0;

private void Button3_Clicked(object sender, EventArgs e)
{
    App.products[Index].Quantity++;
    // So xaml sees the change.
    OnPropertyChanged(nameof(ButtonIsVisible));
}

更多信息,google xamarin binding