当ObservableCollection <object>更改属性值Xamrin Forms时,标签绑定不更新

时间:2018-06-20 03:41:56

标签: c# xamarin xamarin.forms

一旦List属性值更改,我想更新绑定值:

MyContentView.xmal

    

    Spacing="1">
    <ffimageloading:CachedImage 
        FadeAnimationEnabled="true" 
        Source="{Binding ImageProduit}" 
        HeightRequest="{Binding ThumbnailHeight}" 
        Aspect="AspectFill"/>
    <Label x:Name="lbldisp"  Text="{Binding ArticleQt}" VerticalOptions="Center" HorizontalOptions="Center"></Label>
    <Stepper Minimum="0" Maximum="10" Increment="1" HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand" ValueChanged="OnValueChanged" />

MyContentPage.xmal 对象将在此处加载

    <ContentPage 
        xmlns="http://xamarin.com/schemas/2014/forms" 
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
        x:Class="Fit_Plans.ProductsView"
        xmlns:artina="clr-namespace:UXDivers.Artina.Shared;assembly=UXDivers.Artina.Shared"
        xmlns:fitPlans="clr-namespace:Fit_Plans;assembly=Fit_Plans"
        Title="wwe">

   <StackLayout   Grid.Row="0">
            <Label Text="{fitPlans:TranslateExtension Text=nbrJour}"

                   FontAttributes="Bold"
                   HorizontalOptions="Center" />

            <Label Text="{Binding Source={x:Reference NbrJour},
                              Path=Value ,
                StringFormat={fitPlans:TranslateExtension Text=dragnbrJour}}"
                   x:Name="LabelNbrJour"
                   FontSize="Small"
                   HorizontalOptions="Center"
                   VerticalOptions="CenterAndExpand" />

            <Slider x:Name="NbrJour"

                    Maximum="7"
                    Minimum="1"
                     ValueChanged="NbrJour_OnValueChanged"
                    VerticalOptions="CenterAndExpand" />

        </StackLayout>

                    <StackLayout VerticalOptions="FillAndExpand"
                                 HorizontalOptions="FillAndExpand"
                        x:Name="ProduitsLayout">

                     <!--Here products will be displayed ---> 

                    </StackLayout>
        /ContentPage>

现在在 MyContentPage.xaml

namespace Fit_Plans
{
    public partial class ProductsView : ContentPage , INotifyPropertyChanged
    {
        private TapGestureRecognizer tapGestureRecognizer = new TapGestureRecognizer();

        private HttpClient _Client = new HttpClient();
        private List<PlanModel> listproduits = new List<PlanModel>();
        private string api;
        private string siteUrl;
        private PlanModel planModel;
        private List<Product> listProduits;
        public   string titreProduit { get; set; }
        private ArticleViewModel articleView;
        private List<PlanDefinition> listArticles;
        private List<PlanDefinition> listArticlesFinal;
        private ObservableCollection<PlanDefinition> timeStepDataCollection;
        private Stepper stepper;
        public event EventHandler<ValueChangedEventArgs> ValueChanged;

        private List<Label> lbLabel;
        public event PropertyChangedEventHandler PropertyChanged;
        private PlanOrderTemplate item;


        public ProductsView()
        {


            planModel = new PlanModel();
              articleView = new ArticleViewModel();


            lbLabel = new List<Label>();
            stepper =new Stepper();


            InitializeComponent ();

        }
        public ObservableCollection<PlanDefinition> TimeStepDataCollection
        {
            get
            {
                if (timeStepDataCollection == null)
                {
                    timeStepDataCollection = new ObservableCollection<PlanDefinition>();
                }
                return timeStepDataCollection;
            }
            set
            {
                timeStepDataCollection = value;
            }
        }

        public void HandlelbTexChanged(object sender, EventArgs e)
        {
            //Do something here
        }


        async protected override void OnAppearing()
        {

            listArticles= articleView.getArticlesFromPlan();

            var evan = CultureInfo.CurrentCulture;
            timeStepDataCollection = new ObservableCollection<PlanDefinition>(listArticles);
            PopulateProductsLists(timeStepDataCollection);
            PopulateOrderLists(timeStepDataCollection);

            base.OnAppearing();
        }

        protected override void OnDisappearing()
        {
            listproduits = null;
            base.OnDisappearing();
        }
        public void OnPropertyChanged([CallerMemberName] string name = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }

        private void PopulateOrderLists(ObservableCollection<PlanDefinition> listArticles)
        {
             OrderTemplate prod = new OrderTemplate();
           ;


            for (var i = 0; i < listArticles.Count; i++)
            {
                  prod = new OrderTemplate();

                prod.BindingContext = listArticles[i].Quantity;
                 OrderInformation.Children.Add(prod);

                for (int j = 0; j < listArticles[i].ArticlesAvailable.Count; j++)
                {
                    prod = new OrderTemplate();
                    prod.BindingContext = listArticles[i].ArticlesAvailable[j].ArticleQt;
                    OrderInformation.Children.Add(prod);

                }
            }

        }

        //TODO - adapter la product lsit pour faire afficher les items du menu
        private void PopulateProductsLists(ObservableCollection<PlanDefinition> listArticles)
        {
            int labelIndex = 0;
            int count = 0;
            for (var i = 0; i < listArticles.Count; i++)
            {
                item = new PlanOrderTemplate();

                long quanityArticles = listArticles[i].Quantity;


                var lastHeight = "100";
                var y = 0;
                int column = 1;
                var productTapGestureRecognizer = new TapGestureRecognizer();




                ProduitsLayout.Children.Add((new Label
                {
                    Text = listArticles[i].Name,
                    Style = Device.Styles.TitleStyle,
                    FontAttributes = FontAttributes.Bold,
                    TextColor = Color.Black
                }));


                for (int j = 0; j < listArticles[i].ArticlesAvailable.Count; j++)
                {

                    productTapGestureRecognizer.Tapped += OnProductTapped;


                    MyGrid = new Grid();

                 listArticles[i].ArticlesAvailable[j].ImageProduit =
                        "https://fit-plans.com/" + listArticles[i].ArticlesAvailable[j].UrlSmallImage;
                    listArticles[i].ArticlesAvailable[j].Description =
                        Html.FromHtml(listArticles[i].ArticlesAvailable[j].Description).ToString();


                    item = new PlanOrderTemplate();
                    if (j % 2 == 0)
                    {
                        column = 0;
                        y++;
                    }
                    else
                    {

                        column = 1;
                    }

                    listArticles[i].ArticlesAvailable[j].ThumbnailHeight = lastHeight;
                    item.BindingContext = listArticles[i].ArticlesAvailable[j];
                    item.GestureRecognizers.Add(productTapGestureRecognizer);
                    item.lbldispChanged  += item.OnHashtagLife;


                    ProduitsLayout.Children.Add(item);



                }


            }


        }








        void OnValueChanged(object sender, ValueChangedEventArgs e)
       {

         /*  lbldisp.Text = e.NewValue.ToString();
           //Fire the event - notifying all subscribers
           if (lbldispChanged != null)
               lbldispChanged(this, null);*/
       }







        private async void OnProductTapped(Object sender, EventArgs e)
        {


            var selectedItem = (ArticlesAvailable)((PlanOrderTemplate)sender).BindingContext;

            DisplayAlert("Info", selectedItem.Description, "OK");

        }


        private void NbrJour_OnValueChanged(object sender, ValueChangedEventArgs e)
        {


            LabelNbrJour.Text = Math.Round(e.NewValue).ToString();

            for (var i = 0; i < timeStepDataCollection.Count; i++)
            {

                timeStepDataCollection[i].MaximumQuantity = timeStepDataCollection[i].Quantity * (long)Math.Round(e.NewValue);


                for (var j = 0; j < timeStepDataCollection[i].ArticlesAvailable.Count; j++)
                {
                    timeStepDataCollection[i].ArticlesAvailable[j].ArticleQt = 5;


                }


            }




        }





    }
}

无论何时item.lbldispChanged += item.OnHashtagLife;

触发此方法没有任何问题:

  private void NbrJour_OnValueChanged(object sender, ValueChangedEventArgs e)
        {


            LabelNbrJour.Text = Math.Round(e.NewValue).ToString();

            for (var i = 0; i < timeStepDataCollection.Count; i++)
            {

                timeStepDataCollection[i].MaximumQuantity = timeStepDataCollection[i].Quantity * (long)Math.Round(e.NewValue);


                for (var j = 0; j < timeStepDataCollection[i].ArticlesAvailable.Count; j++)
                {
                    timeStepDataCollection[i].ArticlesAvailable[j].ArticleQt = 5;


                }


            }

但是timeStepDataCollection[i].ArticlesAvailable[j].ArticleQt = 5;并没有在对应标签中得到更新

 <Label x:Name="lbldisp"  Text="{Binding ArticleQt}" VerticalOptions="Center" HorizontalOptions="Center"></Label>

如何在滑块NbrJour发布值更改时使timeStepDataCollection动态通知View并更新值?

0 个答案:

没有答案