Xamarin IsVisible绑定到getter属性后不会动态更新

时间:2019-12-03 22:46:15

标签: c# xaml xamarin mvvm xamarin.forms

我正在尝试通过将IsVisible绑定到getter属性来动态更新元素的可见性。但是,除非刷新页面,否则IsVisible不会自动更改UI。

以下是代码段:

xaml:

<StackLayout>
    <Picker Title="Choose..." ItemsSource="{Binding MyItemOptions}" SelectedItem="{Binding MyItem}"/>
    <Label Text="MyItem is Milk" IsVisible="{Binding Path=IsMilkItem}"/>
</StackLayout>

C#:

private string _myItem;
public string MyItem
{
    get => _myItem;
    set
    {
        SetValue(ref _myItem, value);
    }
}

public bool IsMilkItem
{
    get { return MyItem == "Milk"; }
}




public event PropertyChangedEventHandler PropertyChanged;

protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

protected bool SetValue<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
    bool response = false;
    if (field == null ||
        !field.Equals(value))
    {
        field = value;
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        response = true;
    }
    return response;
}

在更新MyItem之后,在代码中更新了boolean属性。但是,在我手动重新渲染页面之前,UI元素不会自动显示。

如果有人对此有解决方案,我将不胜感激。

2 个答案:

答案 0 :(得分:0)

更改MyItem的值时,它不会引发PropertyChanged的{​​{1}}事件,因此永远不会告诉用户界面更新

IsMilkItem

答案 1 :(得分:0)

根据您的描述,您想从选择器中选择项目,当选择项目为“牛奶”时,您希望“标签”控件可见,对吗?

如果是,我修改了您的代码,您可以看一下:

public partial class Page17 : ContentPage, INotifyPropertyChanged
{

    public ObservableCollection<string> MyItemOptions { get; set; }
    private string _MyItem;
    public string MyItem
    {
        get { return _MyItem; }
        set
        {
            _MyItem = value;
            if(_MyItem== "Milk")
            {
                IsMilkItem = true;
            }
            else
            {
                IsMilkItem = false;
            }
        }
    }
    private bool _IsMilkItem;
    public bool IsMilkItem
    {
        get { return _IsMilkItem; }
        set
        {
            _IsMilkItem = value;
            RaisePropertyChanged("IsMilkItem");
        }
    }

    public Page17 ()
    {
        InitializeComponent ();
        MyItemOptions = new ObservableCollection<string>()
        {
            "item 1",
            "item 2",
            "item 3",
            "Milk"
        };
        this.BindingContext = this;
    }

    public event PropertyChangedEventHandler PropertyChanged;


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

更新

您可以使用标签触发来设置标签可见,并且不需要IsMilkItem属性来设置标签可见。

<Picker x:Name="picker1" Title="Choose..." ItemsSource="{Binding MyItemOptions}" SelectedItem="{Binding MyItem}" />
        <Label Text="MyItem is Milk" IsVisible="False" >
            <Label.Triggers>
                <DataTrigger TargetType="Label" Binding="{Binding Source={x:Reference picker1},Path=SelectedItem}" Value="Milk">
                    <Setter Property="IsVisible"
                Value="True"/>
                </DataTrigger>
            </Label.Triggers>
        </Label>

这是.cs:

public ObservableCollection<string> MyItemOptions { get; set; }
    private string _MyItem;
    public string MyItem
    {
        get { return _MyItem; }
        set
        {
            _MyItem = value;

        }
    }


    public Page17 ()
    {
        InitializeComponent ();
        MyItemOptions = new ObservableCollection<string>()
        {
            "item 1",
            "item 2",
            "item 3",
            "Milk"
        };
        this.BindingContext = this;
    }

enter image description here