Mvvm Prism在VigationAware OnNavigation中形成不更新ObservableCollection
我正在使用Prism,它可以帮助我将视图绑定到视图模型。使用INavigationAware,我希望将Listview中的可观察集合从onNavigationTo()更新。调试时,此方法是可访问的,但是它似乎不会更新绑定到视图的ObservableCollection 下面是从Prism的BindableBase和INavigationAware继承的ViewModel:Mvvm Prism在VigationAware OnNavigation中形成不更新ObservableCollection,mvvm,xamarin.forms,prism,Mvvm,Xamarin.forms,Prism,我正在使用Prism,它可以帮助我将视图绑定到视图模型。使用INavigationAware,我希望将Listview中的可观察集合从onNavigationTo()更新。调试时,此方法是可访问的,但是它似乎不会更新绑定到视图的ObservableCollection 下面是从Prism的BindableBase和INavigationAware继承的ViewModel: public class QuoteDetailPageViewModel : BindableBase, INavigat
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; }
}
public类QuotedTailPageViewModel:BindableBase,INavigationAware
{
私有字符串\u标题;
公共字符串标题
{
获取{return\u title;}
集合{SetProperty(ref _title,value);}
}
私有可观察收集消息;
私有可观察收集消息
{
获取{return\u messages;}
set{SetProperty(ref_messages,value);}
}
私人作者(你选择的作者),;
私人作者选择作者
{
获取{return\u selectedAuthor;}
set{SetProperty(ref _selectedAuthor,value);}
}
公共QuotedTailPageViewModel(INavigationService导航服务)
{
Title=“文本消息”;
}
导航到上的公共void(导航参数)
{
变量id=-1;
if(parameters!=null&¶meters.ContainsKey(“id”))
{
int.TryParse(参数[“id”].ToString(),out id);
}
如果(id>0)
{
Title=“联系信息”;
}
var msgs=新列表()
{
new Message(){Text=“对知识的投资回报最高
利息。”},
新消息(){Text=“早睡早起是一个好消息
健康、富有、智慧的人,
新消息()
{
Text=“庆祝成功固然好,但更重要的是
重要的是要吸取失败的教训。”
},
};
消息=新的可观察集合(msgs);
}
public void OnNavigatedFrom(NavigationParameters)
{
}
public void OnNavigatedTo(NavigationParameters)
{
}
}
公共类消息
{
公共字符串文本{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>
有人知道如何解决这个问题吗?原来我已经将
消息设置为私有而非公共。这需要是公共的,以便在视图中应用 首先,为什么要使用可观察的集合?如果可观察集合是正确的,消息
不应该有setter,而应该更改现有集合…只需清除
可观察集合,然后将项目添加到其中。这应该行得通。@Haukinger,为什么我不应该使用可观察的集合呢?记住,接下来我可能需要更新列表中的这些项目。另外,我不明白为什么二传手在这里是个问题。但是,无论如何,如果你有一个更好的解决方案,请使用它。通常,如果你使用一个可观察的集合,你会改变它,你不会替换它。也就是说,即使没有setter,您仍然可以通过getter添加或删除项。。。