【问题标题】:Prism Forms INavigationAware OnNavigatingTo not updating ObservableCollectionPrism Forms INavigationAware OnNavigatingTo 不更新 ObservableCollection
【发布时间】:2017-06-14 09:18:30
【问题描述】:

我正在使用 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>

有人知道如何解决这个问题吗?

【问题讨论】:

  • 你为什么首先使用 observable 集合?如果可观察集合是正确的,Messages 应该没有设置器,而是更改现有集合...
  • 只需Clear 可观察集合,然后将项目添加到其中。那应该可以。
  • @Haukinger,为什么我不应该使用 observable 集合?请记住,我可能需要更新列表中的这些项目。另外,我不明白为什么 Setter 在这里是个问题。但是,如果您有更好的解决方案,请务必提出来。
  • 通常,如果你使用一个可观察的集合,你改变它,你不替换它。也就是说,即使没有 setter,您仍然可以通过 getter 添加或删除项目...

标签: mvvm xamarin.forms prism


【解决方案1】:

原来我已将Messages 设置为私有而不是公开。这需要公开,以便在视图中应用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 2018-10-16
    • 2019-09-12
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多