1

私は過去数時間抵抗する問題を抱えています.ViewModelコードは次のとおりです.

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

監視可能なコレクションの新しいインスタンスに Customers を設定しています!

監視可能なコレクションを新しいインスタンスに変更する場合は、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

私はそれを持っている。

あなたのデータコンテキストを確認してください - きっと null です。WP7でもまったく同じ問題が発生しました。PhoneApplicationPage のコンストラクターで次のようにします。

DataContext = new Detail_AgenceViewModel();

そこで初期化します。WP7 では、XAML で datacontext を作成すると null になります。これは役に立ちますか?

于 2012-04-24T13:20:25.273 に答える