简体   繁体   中英

Problem with binding data in Xamarin Forms

I am a newbie in Xamarin Forms (XF). When I try to bind data, I face a problem that I do not understand. If I update any property by interface (like picker), PropertyChanged exist. But if I update any property by code, PropertyChanged do not exist. I try to fix it by assigning PropertyChanged = delegate {} , PropertyChanged exist but handler(this, new PropertyChangedEventArgs(propertyName)); can't update value of property on my interface. Here is my example:

  • in MainPage.xaml:
<Picker Grid.Row="1" Title="Select a component" ItemsSource="{Binding ComponentTree.Children}" ItemDisplayBinding="{Binding Name}" SelectedItem="{Binding SelectedGrandFatherComponent}" />
<Picker Grid.Row="2" Title="Select a component" ItemsSource="{Binding SelectedGrandFatherComponent.Children}" ItemDisplayBinding="{Binding Name}" SelectedItem="{Binding SelectedFatherComponent}" />
<Picker Grid.Row="3" x:Name="SelectedComponentPicker" Title="Select a component" ItemsSource="{Binding SelectedFatherComponent.Children}" ItemDisplayBinding="{Binding Name}" SelectedItem="{Binding SelectedComponent}" />
<ScrollView Grid.Row="4">
  <interfaces:SimpleTreeView BindingContext="{Binding SelectedComponent}" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
</ScrollView>
<Label Text="{Binding SelectedComponentStructure}"></Label>
  • in TreeCardView.xaml:
<Button Grid.Column="1" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Command="{Binding Appear}"  BackgroundColor="Transparent" CommandParameter="{Binding Name}" />
  • in TreeNode.cs (like controller of each node in a tree (TreeCardView.xaml)):
public TreeNode()
{
  Appear = new Command<string>((x) => ShowUp(x));
}
public static void ShowUp(string name)
{
  StructureDesignViewModel instance = new StructureDesignViewModel();
  StructureDesignViewModel.HandleSelectedComponent delegateIns = new StructureDesignViewModel.HandleSelectedComponent(instance.SetSelectedComponent);
  delegateIns(name);
}
  • in StructureDesignViewModel.cs (like controler of MainPage.xaml):
public class StructureDesignViewModel : ObservableObject, INotifyPropertyChanged
    {
        private static readonly StructureDesignViewModel instance = new StructureDesignViewModel();

        public static StructureDesignViewModel Instance
        {
            get
            {
                return instance;
            }
        }
        public static IList<Component> Components
        {
            get
            {
                return ComponentData.Components;
            }
        }
        TreeNode selectedGrandFatherComponent;
        TreeNode selectedFatherComponent;
        TreeNode selectedComponent;
        string selectedComponentStructure;

        public TreeNode BaseTree
        {
            get; set;
        }

        public TreeNode ComponentTree
        {
            get; set;
        }


        void GrowUp(TreeNode node, List<Component> components)
        {
            int len = components.Count;
            for (int i = 0; i < len; i++)
            {
                var currentNode = node.Children.Add(new TreeNode { Name = components[i].Name, Status = components[i].Status, IsExpanded = false, Modals = components[i].Modals });
                GrowUp((TreeNode)currentNode, components[i].Childs);
            }
        }
        public StructureDesignViewModel()
        {
            ComponentTree = new SimpleTreeView().ViewModel.MyTree;
            ComponentTree = new TreeNode { Name = "Component Root", Status = 0, IsExpanded = true };
            GrowUp(ComponentTree, (List<Component>)Components);
        }
        public TreeNode SelectedGrandFatherComponent
        {
            get { return selectedGrandFatherComponent; }
            set
            {
                if (selectedGrandFatherComponent != value)
                {
                    selectedGrandFatherComponent = value;
                    OnPropertyChanged();
                }
            }
        }

        public TreeNode SelectedFatherComponent
        {
            get
            {
                if (selectedGrandFatherComponent?.Children.Count > 0)
                {
                    selectedFatherComponent = (HMIStudio.Shared.Interfaces.TreeNode)selectedGrandFatherComponent.Children[0];
                }
                return selectedFatherComponent;
            }
            set
            {
                if (selectedFatherComponent != value)
                {
                    selectedFatherComponent = value;
                    OnPropertyChanged();
                }
            }
        }

        public TreeNode SelectedComponent
        {
            get
            {
                if (selectedFatherComponent?.Children.Count > 0)
                {
                    selectedComponent = (HMIStudio.Shared.Interfaces.TreeNode)selectedFatherComponent.Children[0];
                }
                return selectedComponent;
            }
            set
            {
                if (selectedComponent != value)
                {
                    selectedComponent = value;
                    OnPropertyChanged();
                }
            }
        }

        public string SelectedComponentStructure
        {
            get
            {
                return selectedComponentStructure;
            }
            set
            {
                if (selectedComponentStructure != value)
                {
                    Set("SelectedComponentStructure", ref selectedComponentStructure, value);
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
                if (propertyName == "SelectedGrandFatherComponent")
                {
                    OnPropertyChanged(nameof(SelectedFatherComponent));
                }
                else if (propertyName == "SelectedFatherComponent")
                {
                    OnPropertyChanged(nameof(SelectedComponent));
                }
            }
        }
        public delegate void HandleSelectedComponent(string Name);
        public void SetSelectedComponent(string name)
        {
            Console.WriteLine("Showing selected component {0}", name);
            SelectedComponentStructure = name;
        }
    }
  • in ObservableObject.cs:
public class ObservableObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        protected virtual void Set<T>(string propertyName, ref T backingField, T newValue, Action beforeChange = null, Action afterChange = null)
        {
            if (string.IsNullOrEmpty(propertyName))
                throw new ArgumentException("propertyName");

            if (backingField == null && newValue == null)
                return;

            if (backingField != null && backingField.Equals(newValue))
                return;

            if (beforeChange != null)
                beforeChange();

            backingField = newValue;

            OnPropertyChanged(propertyName);

            if (afterChange != null)
                afterChange();
        }
    }

My program's flow:

  • StructureDesignViewModel get data from ComponentData.Components (tree structure data) to pass to ItemsSource in view.
  • When I choose GrandFatherComponent , GrandFatherComponent.Children is ItemSource of next picker.
  • When I click on a node of SimpleTreeView/SelectedComponent , I call function Appear to assign name of node to SelectedComponentStructure .

My problem is SelectedComponentStructure was changed but interface do not update it.

Thanks for reading! Best regards!

StructureDesignViewModel

public class StructureDesignViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

     string selectedComponentStructure;
     public string SelectedComponentStructure
     {
        get
        {
            return selectedComponentStructure;
        }
        set
        {
            selectedComponentStructure = value;
            SetPropertyValue(ref selectedComponentStructure, value);
        }

     }

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

    protected void SetPropertyValue<T>(ref T bakingFiled, T value, [CallerMemberName] string proertyName = null)
    {
        bakingFiled = value;
        OnPropertyChanges(proertyName);

    }

}

Try this way..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM