Question

I have problem that i don't know how to bind data in windows phone 8 and the scenario is :-

I request data from server and after I got response I display them in grid and list boxes (like a table).
Until here every thing is okay after I finish the display function there is a new function that retrieves the update to last request.

Here is my problem:
When I receive the new data I don't know how bind this new data to same old table.
For example: the first request is return the gold price 1500$---> I display in table--->then new request--> the update function return the new price of gold is 1502$.
How to update desired row that have gold price textblock with the new price while the application running.

This the first request:-

public ObservableCollection<Data> DataReceivedCollection { get; set; }

private void FireRequest2()
    {

        var request = HttpWebRequest.Create(new Uri("http://74.54.46.178/vertexweb10/webservice.svc/getallsymbols?AccountID=1122336675")) as HttpWebRequest;
        request.Method = "GET";
        request.CookieContainer = cookieJar;
        request.BeginGetResponse(ar =>
        {
            HttpWebRequest req2 = (HttpWebRequest)ar.AsyncState;
            using (var response = (HttpWebResponse)req2.EndGetResponse(ar))
            {
                using (Stream stream = response.GetResponseStream())
                {
                    using (var reader = new StreamReader(stream))
                    {
                        var outerRoot1 = JsonConvert.DeserializeObject<OuterRootObject1>(reader.ReadToEnd());
                        JArray jsonArray = JArray.Parse(outerRoot1.d);
                        JToken jsonArray_Item = jsonArray.First;
                        while (jsonArray_Item != null)
                        {

                            string Name = jsonArray_Item.Value<string>("Name");
                            string Bid = jsonArray_Item.Value<string>("Bid");
                            string Ask = jsonArray_Item.Value<string>("Ask");
                            string ID = jsonArray_Item.Value<string>("ID");



                            DataReceivedCollection = new ObservableCollection<Data>();


                            DispatchInvoke(() =>
                            {
                                myList.ItemsSource = DataReceivedCollection;
                                // and to add data you do it like this:
                                DataReceivedCollection.Add(new Data() { symid = ID, textFirst = Name, textSecond = Bid, textThird = Ask });


                            }
);

                            //Be careful, you take the next from the current item, not from the JArray object.
                            jsonArray_Item = jsonArray_Item.Next;
                        }



                    }
                }

            }

        }, request);
    }

And here is my XAML that i want to dsiaply the requested data from firerequest2();

<Grid Background="#FFC9DC97" x:Name="ContentPanel" Grid.Row="1" Margin="12,140,12,0">
        <ListBox Name="myList" Background="#FFC9DC97">
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="1*"/>
                            <ColumnDefinition  Width="1*"/>
                            <ColumnDefinition Width="1*"/>
                            <ColumnDefinition Width="1*"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock x:Name="ide" Text="{Binding symid}" Grid.Column="3" HorizontalAlignment="Center"/>
                        <TextBlock Text="{Binding textFirst}" Grid.Column="0" HorizontalAlignment="Left" Foreground="#FF1C69D8"/>
                        <TextBlock Text="{Binding textSecond}" Grid.Column="1" HorizontalAlignment="Center"/>
                        <TextBlock Text="{Binding textThird}" Grid.Column="2" HorizontalAlignment="Right"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

To here every thing is working fine i don't know to update the grid with the new data from the next function

public class Data : INotifyPropertyChanged
    {
        private string _textFirst;
        public string textFirst
        {
            [DebuggerStepThrough]
            get { return _textFirst; }
            [DebuggerStepThrough]
            set
            {
                if (value != _textFirst)
                {
                    _textFirst = value;
                    OnPropertyChanged("textFirst");
                }
            }
        }
        private string _textSecond;
        public string textSecond
        {
            [DebuggerStepThrough]
            get { return _textSecond; }
            [DebuggerStepThrough]
            set
            {
                if (value != _textSecond)
                {
                    _textSecond = value;
                    OnPropertyChanged("textSecond");
                }
            }
        }

        private string _textThird;
        public string textThird
        {
            [DebuggerStepThrough]
            get { return _textThird; }
            [DebuggerStepThrough]
            set
            {
                if (value != _textThird)
                {
                    _textThird = value;
                    OnPropertyChanged("textThird");
                }
            }
        }

        private string _symid;
        public string symid
        {
            [DebuggerStepThrough]
            get { return _symid; }
            [DebuggerStepThrough]
            set
            {
                if (value != _symid)
                {
                    _symid = value;
                    OnPropertyChanged("symid");
                }
            }
        }
        #region INotifyPropertyChanged Implementation
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string name)
        {
            var handler = System.Threading.Interlocked.CompareExchange(ref PropertyChanged, null, null);
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
        #endregion
    }
Était-ce utile?

La solution

Your dataReceived collection needs to be declared like this because it is the subject of a binding...

    public ObservableCollection<Data> DataReceivedCollection { get; set; } 

And in the initialization code, it needs to be instantiated like this...

        DataReceivedCollection = new ObservableCollection<Data>();

And your data class should be declared something like this (not all properties declared)

public class Data : INotifyPropertyChanged
{
    private string _textFirst;
    public string TextFirst
    {
        [DebuggerStepThrough]
        get { return _textFirst; }
        [DebuggerStepThrough]
        set
        {
            if (value != _textFirst)
            {
                _textFirst = value;
                OnPropertyChanged("TextFirst");
            }
        }
    }
    private string _textSecond;
    public string TextSecond
    {
        [DebuggerStepThrough]
        get { return _textSecond; }
        [DebuggerStepThrough]
        set
        {
            if (value != _textSecond)
            {
                _textSecond = value;
                OnPropertyChanged("TextSecond");
            }
        }
    }
    #region INotifyPropertyChanged Implementation
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string name)
    {
        var handler = System.Threading.Interlocked.CompareExchange(ref PropertyChanged, null, null);
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
    #endregion
}

Doing these things will ensure that the binding engine gets the information it needs to populate your List Box.

This is only a start that will give you some better results. As mentioned in the commentary, your next port of call is to take up a study of INotifyPropertyChanged.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top