Xamarin形式的绑定数据问题

问题描述 投票:0回答:1

我是Xamarin Forms(XF)的新手。当我尝试绑定数据时,遇到了一个我不理解的问题。如果我通过接口(如选择器)更新任何属性,则PropertyChanged存在。但是,如果我通过代码更新任何属性,则PropertyChanged不存在。我尝试通过分配PropertyChanged = delegate {}来解决它,PropertyChanged存在,但是handler(this, new PropertyChangedEventArgs(propertyName));无法更新我界面上的属性值。这是我的示例:

  • 在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>
  • 在TreeCardView.xaml中:
<Button Grid.Column="1" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Command="{Binding Appear}"  BackgroundColor="Transparent" CommandParameter="{Binding Name}" />
  • 在TreeNode.cs中(类似于树中每个节点的控制器(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);
}
  • 在StructureDesignViewModel.cs中(例如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;
        }
    }
  • 在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();
        }
    }

我的程序的流程:

  • [StructureDesignViewModelComponentData.Components获取数据(树结构数据)以传递到视图中的ItemsSource
  • [当我选择GrandFatherComponent时,GrandFatherComponent.Children是下一个选取器的ItemSource
  • [当我单击SimpleTreeView/SelectedComponent的节点时,我调用函数Appear将节点的名称分配给SelectedComponentStructure
  • 我的问题是SelectedComponentStructure已更改,但界面未更新。

感谢您的阅读!最好的问候!

我是Xamarin Forms(XF)的新手。当我尝试绑定数据时,遇到了一个我不理解的问题。如果我通过接口(如选择器)更新任何属性,则存在PropertyChanged。但是,如果我更新任何...

c# xamarin.forms
1个回答
2
投票

StructureDesignViewModel

© www.soinside.com 2019 - 2024. All rights reserved.