1

我有一个过去几个小时无法解决的问题,这里是 ViewModel 代码:(PS:我无法共享 url 流,但不要担心它的行进,因为我用 BreakPoint 对其进行了测试)

private ObservableCollection<CustomerPublic> customers;
    List<CustomerPublic> liste = new List<CustomerPublic>();
    public ObservableCollection<CustomerPublic> Customers
    {
        get
        { return customers; }
        set
        {
            if (customers != value)
            {
                customers = value;
                RaisePropertyChanged("Customers");
            }
        }
    }
    private int id;
    public int ID
    {
        get
        {
            return id;
        }
        set
        {
            id = value;
            RaisePropertyChanged("ID");
        }
    }
    public Detail_AgenceViewModel(int id)
    {
        this.ID = id;
        PopulateCollection();  
    }
    public Detail_AgenceViewModel()
    {

    }

    private void PopulateCollection()
    {
        ParseFeedRequest();
    }


    private void ParseFeedRequest()
    {
        RestClient client = new RestClient();
        client.BaseUrl = "....";

        RestRequest request = new RestRequest();

        .......

        client.ExecuteAsync(request, ParseFeedCallBack);
    }

    public void ParseFeedCallBack(IRestResponse response)
    {
        if (response.StatusCode == HttpStatusCode.OK)
        {
            ParseXMLFeed(response.Content);
        }
    }

    private void ParseXMLFeed(string feed)
    {
        if (feed == null)
            return;
        XElement xmlItems = XElement.Parse(feed);

        liste = (from response in xmlItems.Descendants("result")
                 let lib = response.Element("lib")
                 let adresse = response.Element("adresse")

                 select new CustomerPublic
                 {
                     lib = lib == null ? null : lib.Value,
                     adresse = adresse == null ? null : adresse.Value,


                 }).ToList();

        Customers = new ObservableCollection<CustomerPublic>(liste);
                       }

风景:

   <phone:PhoneApplicationPage.DataContext>
    <vm:Detail_AgenceViewModel/>
  </phone:PhoneApplicationPage.DataContext>
    <Grid x:Name="LayoutRoot"
      Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel"
                Grid.Row="0"
                Margin="12,17,0,28">
        <TextBlock x:Name="ApplicationTitle"
                   Text="MY APPLICATION"
                   Style="{StaticResource PhoneTextNormalStyle}" />
        <TextBlock x:Name="PageTitle"
                   Text="page name"
                   Margin="9,-7,0,0"
                   Style="{StaticResource PhoneTextTitle1Style}" />
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <StackPanel x:Name="ContentPanel" Grid.Row="2" Margin="12,0,12,0" Orientation="Vertical">

        <!--TextBox Text="{Binding Count, Mode=TwoWay}" x:Name="tbCount" />
        <TextBlock Text="{Binding Count}" /-->
        <ListBox x:Name="Agences" ItemsSource="{Binding Customers}"  >

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{Binding lib}" />
                        <TextBlock Text="{Binding adresse}" />


                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
  </Grid>

问题是一切顺利客户即使她已加载但什么也没有出现!有人有想法吗?

4

3 回答 3

1

您正在将客户设置为可观察集合的新实例!

当您将 observable 集合更改为新实例时,您必须使用 INotifyPropertyChanged 告诉视图该集合已更改为新实例- 尽管会通知对集合中项目的更改,但不会通知对集合 ITSELF 的更改。

当你这样做时:

Customers = new ObservableCollection<CustomerPublic>(liste);

视图仍然绑定到 OLD 集合。你应该做:

Customers.Clear();
foreach(var item in liste)
  Customers.Add(item);

或确保 Customers 属性调用 NotifyPropertyChanged 函数。

查看此视频或文章以获取更多信息:

http://www.codeproject.com/Articles/371217/Apex-Part-1-Create-Your-First-MVVM-Application http://www.youtube.com/watch?v=m4cx9w5fiwk&feature=youtu.be

祝你好运,如果这有帮助,请告诉我!!

于 2012-04-24T09:15:48.120 回答
0

我有类似的问题;

public void FillList(List<StockItem> siList)
    {


        listBox.ItemsSource = siList;


    }

其中 sIList 是 X 项的填充列表,具有正确命名的属性。程序构建和运行良好,但未显示列表框。(这个问题是在过渡到 MVVM 时开始的)

于 2012-04-24T12:22:01.130 回答
0

我懂了。

检查您的数据上下文 - 我敢打赌它是空的。我在 WP7 中遇到过同样的问题。在 PhoneApplicationPage 的构造函数中执行以下操作:

DataContext = new Detail_AgenceViewModel();

并在那里初始化它。在 WP7 中,当我在 XAML 中创建数据上下文时,它为空。这有帮助吗?

于 2012-04-24T13:20:25.273 回答