Prism Forms INavigationAware OnNavigatingTo not update ObservableCollection

时间:2017-06-14 09:18:31

标签: mvvm xamarin.forms prism

我正在使用Prism帮助我将视图绑定到视图模型。使用INavigationAware,我希望从OnNavigatingTo()更新Listview中的Observable集合。在调试时,可以访问此方法,但它似乎不更新绑定到视图的ObservableCollection。

以下是ViewModel,它继承自Prism的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>

任何人都知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

原来我将Messages设为私有而非公开。这需要公开才能在视图中应用。