2017-06-14 109 views
0

我使用棱鏡,它可以幫助我將視圖綁定到視圖模型。使用INavigationAware,我希望從OnNavigatingTo()更新ListView中的Observable集合。在調試此方法是可訪問的,但它似乎並沒有更新綁定到視圖的ObservableCollection。棱鏡形式INavigationAware OnNavigatingTo不更新ObservableCollection

下面是視圖模型,即從棱鏡的BindableBase和INavigationAware繼承:

public class QuoteDetailPageViewModel : BindableBase, INavigationAware 
{ 

    private string _title; 

    public string Title 
    { 
     get { return _title; } 
     set { SetProperty(ref _title, value); } 
    } 

    private ObservableCollection<Message> _messages; 
    private ObservableCollection<Message> Messages 
    { 
     get { return _messages; } 
     set { SetProperty(ref _messages, value); } 
    } 

    private Author _selectedAuthor; 
    private Author SelectedAuthor 
    { 
     get { return _selectedAuthor; } 
     set { SetProperty(ref _selectedAuthor, value); } 
    } 


    public QuoteDetailPageViewModel(INavigationService navigationService) 
    { 
     Title = "Text Messages"; 
    } 

    public void OnNavigatingTo(NavigationParameters parameters) 
    { 
     var id = -1; 

     if (parameters != null && parameters.ContainsKey("id")) 
     { 
      int.TryParse(parameters["id"].ToString(), out id); 
     } 

     if (id > 0) 
     { 
      Title = "Contact Message"; 
     } 

     var msgs = new List<Message>() 
     { 
      new Message() {Text = "An investment in knowledge pays the best 
       interest."}, 
      new Message() {Text = "Early to bed and early to rise makes a 
       man healthy, wealthy, and wise."}, 
      new Message() 
      { 
       Text = "It's fine to celebrate success but it is more 
        important to heed the lessons of failure." 
      }, 
     }; 

     Messages = new ObservableCollection<Message>(msgs); 
    } 

    public void OnNavigatedFrom(NavigationParameters parameters) 
    { 
    } 

    public void OnNavigatedTo(NavigationParameters parameters) 
    { 
    } 
} 

public class Message 
{ 
    public string Text { get; set; } 
} 

而且下面是XAML代碼:

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms" 
      prism:ViewModelLocator.AutowireViewModel="True" 
      x:Class="PrismAppTutorial.Views.QuoteDetailPage" 
      Title="{Binding Title}"> 

    <StackLayout Margin="0,20,0,0"> 

     <ListView x:Name="lvAuthorQuotes" 
        ItemsSource="{Binding Messages}" 
        HasUnevenRows="True"> 

      <ListView.ItemTemplate> 
       <DataTemplate> 
        <ViewCell> 
         <ViewCell.View> 
          <StackLayout Padding="20,10" 
             Orientation="Vertical" 
             HorizontalOptions="FillAndExpand" 
             VerticalOptions="StartAndExpand"> 

           <Label Text="{Binding Text}" 
             HorizontalOptions="StartAndExpand" 
             VerticalOptions="StartAndExpand" 
             LineBreakMode="WordWrap" /> 

          </StackLayout> 
         </ViewCell.View> 
        </ViewCell> 
       </DataTemplate> 
      </ListView.ItemTemplate> 

     </ListView> 
    </StackLayout> 

</ContentPage> 

任何人知道如何解決這一問題?

+0

爲什麼您首先使用可觀察集合?如果可觀察的集合是正確的,'Messages'應該沒有setter,而是改變現有的集合... – Haukinger

+0

只需清除可觀察的集合,然後將項目添加到它。這應該工作。 –

+0

@Haukinger,爲什麼我不應該使用可觀察集合?請記住,我可能需要在列表中更新這些項目。另外,我不明白爲什麼Setter是這裏的一個問題。但是,無論如何,如果你有更好的解決方案,請把它帶上。 –

回答

0

事實證明我已將Messages設置爲私密而非公開的。這需要公開以便在視圖中應用。

+0

一個詭計問題! ;) –