【问题标题】:INPC and bind data during the run time windows phone 8运行时Windows Phone 8期间的INPC和绑定数据
【发布时间】:2014-04-27 16:39:48
【问题描述】:

我从服务器请求数据,在得到响应后,我将它们显示在网格和列表框 -TextBlock(如表格)中。直到这里一切正常,我完成了显示功能,然后我必须调用新 URL 并期望用于更新我的网格表值的新 JSON 数据,例如:- 我的小型应用程序请求登录成功后首先登录我请求新的 URL,该 URL 用卖价和买价检索(JSON 数组)中的项目 ==> 在这里我用这个数据就像我之前告诉你的那样,最后我需要请求新的 URL 检索服务器上更改为新价格的项目 ===> 我不知道如何在我的网格文本块表中搜索以更新所需的行,请帮助我(请检查我下面的代码,他们告诉我 INPC 和 for 循环有错误,因为当我请求第二个 URL 两次时,我的表中未更新新数据 ---> 请提供建议)


这是我的代码,只有第二个 URL 调用我不知道如何实现第三个调用以及如何在运行时搜索我的表:-

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);
}

这是我的 XAML:-

<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>

这是 INotifyPropertyChanged 类

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
}

请帮帮我

【问题讨论】:

  • 简化你的问题,没有人会照原样回答。
  • @Aybe,这是一个“俄罗斯娃娃”的问题。他的最后 5 个问题都在同一个应用程序上,每次回答后他都会“卡”在下一个问题上。每个问题都是不同的,但随着他的进步,它们会按顺序出现。阅读他的历史。还有这个:meta.stackexchange.com/questions/188625/…
  • +1 他正在努力学习:D

标签: c# wpf xaml windows-phone-7 windows-phone-8


【解决方案1】:

在这个片段中...

                    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 });


                    }

您正在通过重新初始化 DataReceivedCollection 来销毁所有以前的数据。所以它是空的。然后在调度程序线程中绑定到它,然后添加到它。而且您在每次通过或 while (jsonArray_Item != null) 循环时都在重复整个过程。

可观察的集合和绑定应该在初始化时设置一次。不是每次你通过一个循环。如果要将集合设置为空,请使用 `DataReceivedCollection.Clear();

将这些行移至一次性初始化...

DataReceivedCollection = new ObservableCollection<Data>();
myList.ItemsSource = DataReceivedCollection;

此外,由于您正在调度“添加”(这是正确的),因此您正在引发闭包问题,即在调度程序线程执行之前变量可能超出范围。

将这些行移到调度程序线程中...

                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");

您的 INPC 实现看起来不错。此时,需要调整的只是您的逻辑流程。

【讨论】:

  • Garry 我在 WPF 房间聊天你在吗
  • 现在那里见
猜你喜欢
  • 1970-01-01
  • 2014-01-15
  • 2013-12-30
  • 2017-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-30
  • 1970-01-01
相关资源
最近更新 更多