【问题标题】:IsVisible property issue xamarin.formsIsVisible 属性问题 xamarin.forms
【发布时间】:2020-05-09 20:28:24
【问题描述】:

我对标签中的 IsVisible 属性有疑问。如果文本是立即的(普通字符串) 并且 IsVisible 的值为 true 它将正常工作,但如果我使用绑定文本或属性 IsVisible 绑定它将无法工作。请帮忙。

<Label x:Name="st" Text="{Binding status}" HorizontalOptions="Start" FontSize="Medium" 
BindingContext="{x:Reference sw}"  IsVisible="{Binding IsToggled}" />

<Switch x:Name="sw"  IsToggled="True"  HorizontalOptions="EndAndExpand"  />

【问题讨论】:

  • 请不要将代码或错误作为图像发布。花时间复制代码并正确格式化。
  • 您介意编辑您的问题并更具体吗?你可以阅读这篇文章stackoverflow.com/help/how-to-ask。从我的角度来看,您可能会向我们提供您如何设置绑定。

标签: xaml xamarin.forms properties label issue-tracking


【解决方案1】:

首先,请接受 Jason 的建议,花点时间复制你的代码并用 ptoperly 格式化。

根据你的代码,不要将Label St BindingContext绑定到Switch Sw,因为你绑定了Label St Text到status,但是Switch Sw没有这个属性,所以请修改你的代码。

<Label
            FontSize="Medium"
            HorizontalOptions="StartAndExpand"
            IsVisible="{Binding Source={x:Reference sw}, Path=IsToggled}"
            Text="{Binding status}" />
        <Switch
            x:Name="sw"
            HorizontalOptions="EndAndExpand"
            IsToggled="True" />

 public partial class Page12 : ContentPage, INotifyPropertyChanged
{
    private string _status;
    public string status
    {
        get { return _status; }
        set
        {
            _status = value;
            RaisePropertyChanged("status");
        }
    }

    public Page12()
    {
        InitializeComponent();

        status = "this is test";
        this.BindingContext = this;
    }

    public event PropertyChangedEventHandler PropertyChanged;      
    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

注意:别忘了实现INotifyPropertyChanged接口,通知数据发生变化。

关于绑定,请看:

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/basic-bindings

【讨论】:

  • @a.1 有什么更新吗?现在你的问题解决了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多