Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Xamarin Opacity and IsVisible properties aren't working at run time after Forms package update.

TimothyTimothy USUniversity ✭✭
edited March 28 in Xamarin.Forms

I recently updated my Xamarin Forms packages on a new project I've been working on. After doing this, I noticed that my IsVisible and Opacity Data Bound UI elements no longer change based on button clicks etc. If I set the values in the constructor of my view controller, it sets it and displays accordingly. If I try to set it after that on a button or label click, within a command, the OnPropertyChanged event fires but the UI elements don't adjust.

This was working flawlessly prior to the update and I've not changed any Binding code. Is anyone else experiencing this?

Edit: After doing some more testing, I found this is also the case with setting any element in data binding. It works fine in the constructor but trying to set the values in a command triggers the OnPropertyChanged event but never actually adjusts the UI.

Best Answer

Answers

  • TimothyTimothy USUniversity ✭✭
    edited March 28

    I've uploaded an example on Bitbucket that shows the binding only working in the constructor but not when the commands are called. It can be found here:
    https://bitbucket.org/TimothyCarter/testbinding/src/master/

    I'll also include the code:

    <?xml version="1.0" encoding="utf-8"?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:d="http://xamarin.com/schemas/2014/forms/design"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 mc:Ignorable="d"
                 x:Class="TestBinding.MainPage"
                 xmlns:local="clr-namespace:TestBinding;assembly=TestBinding"
                 >
        <ContentPage.BindingContext>
            <local:TestBindingViewModel />
        </ContentPage.BindingContext>
        <StackLayout>
            <!-- Place new controls here -->
            <Label Text="{Binding LabelText}" Opacity="{Binding LabelOpacity}" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
            <Button Text="Change Text" Command="{Binding SetText}"/>
            <Button Text="Change Opacity" Command="{Binding ChangeOpacity}"/>
        </StackLayout>
    </ContentPage>
    
    using System;
    using System.ComponentModel;
    using System.Windows.Input;
    using Xamarin.Forms;
    
    namespace TestBinding
    {
        public class TestBindingViewModel : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            private int _labelOpacity { get; set; }
            private string _labelText { get; set; }
    
            public TestBindingViewModel()
            {
                LabelText = "Fudge";
                LabelOpacity = 1;
    
                ChangeOpacity = new Command(() =>
                {
                    LabelOpacity = LabelOpacity == 0 ? 1 : 0;
                });
    
                SetText = new Command(() =>
                {
                    LabelText = LabelText.Equals("Fudge") ? "DoubleFudge" : "Fudge";
                });
            }
    
            public int LabelOpacity
            {
                protected set
                {
                    _labelOpacity = value;
                    OnPropertyChanged("_labelOpacity");
                }
                get { return _labelOpacity; }
            }
    
            public string LabelText
            {
                protected set
                {
                    _labelText = value;
                    OnPropertyChanged("_labelText");
                }
                get { return _labelText; }
            }
    
            #region ICommand Implementations
            public ICommand ChangeOpacity { protected set; get; }
            public ICommand SetText { protected set; get; }
            #endregion
    
            #region OnPropertyChanged Implementation
            protected void OnPropertyChanged(string propertyName)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
                Console.WriteLine($"Property Changed: {propertyName}");
            }
            #endregion
        }
    }
    
  • TimothyTimothy USUniversity ✭✭

    That worked. Odd that it worked fine last week and then just stopped working though.

Sign In or Register to comment.