2012-04-24 5 views
1

지난 몇 시간 동안 저항하는 문제가 있습니다. 여기에 ViewModel 코드가 있습니다 : (PS : BreakPoint로 테스트했기 때문에 URL 스트림을 공유 할 수는 없지만 행진에 대해 걱정하지 마십시오.'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> 

문제는 잘 가고 고객이 심지어 그녀가로드하지만 아무것도 표시되지 않는 것입니다! 누군가가 아이디어를 가지고 있습니까?

답변

1

Observable 컬렉션의 새 인스턴스로 Customers를 설정하고 있습니다!

새 인스턴스에 관찰 모음을 변경하면 컬렉션이 새로운 인스턴스에 를 변경한다는 견해 말할에서 INotifyPropertyChanged를 사용해야합니다 - 통보 컬렉션을 변경 컬렉션의 항목을 변경하지만를 ITSELF는 그렇지 않습니다. 이 작업을 수행 할 때

은 :

Customers = new ObservableCollection<CustomerPublic>(liste); 

뷰는 여전히 이전 컬렉션에 바인딩됩니다. 다음을 수행해야합니다.

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

또는 NotifyPropertyChanged 함수를 호출해야합니다.

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

행운과 :

더 많은 정보를 원하시면,이 비디오 또는 문서의 수표를 가지고 이것이 도움이되는지 알려주세요.

+0

안녕하세요, 피드 주셔서 감사합니다,하지만 귀하의 솔루션에 대한 예외를 생성 Custom.Clear(); –

+0

게다가 내가 아펙스와 함께 Windows phone coz에서 다운로드 할 수 없다는 것을 확신 할 수 있습니까? 고마워요 :) –

0

비슷한 문제가 있습니다.

public void FillList(List<StockItem> siList) 
    { 


     listBox.ItemsSource = siList; 


    } 

여기서 sIList는 올바르게 명명 된 속성이있는 X 항목의 채워진 목록입니다. 프로그램 빌드 &은 정상적으로 실행되지만 목록 상자는 표시되지 않습니다. (이 문제는 MVVM으로 전환 할 때 시작됨)

0

나는 그것을 얻었습니다.

데이터 인터페이스 확인 - null 일 것입니다. WP7에서 이와 똑같은 문제를 겪었습니다. PhoneApplicationPage의 생성자에서 다음을 수행하십시오.

DataContext = new Detail_AgenceViewModel();

그리고 초기화하십시오. WP7에서 XAML에서 datacontext를 만들면 null입니다. 이게 도움이 되나요?

+0

unfortunatly, 아니요 : ( –

+0

여전히 도움이!?! –

관련 문제