【发布时间】:2018-07-26 14:24:20
【问题描述】:
我有一个使用 XamarinForms 和 Prism MVVM 的小项目。 在设置页面上,我从 Picker 中保存了作者的 ID。 当我返回设置页面时,我希望在选择器中默认选择该作者。
这是我在 Xaml 中的选择器:
<Picker x:Name="authorPicker" Title="Select Author" FontSize="Medium"
HorizontalOptions="StartAndExpand" VerticalOptions="Center"
ItemsSource="{Binding NoteAuthors}"
ItemDisplayBinding="{Binding Name}"
SelectedItem="{Binding SelectedAuthor, Mode=TwoWay}"
Grid.Row="0" Grid.Column="1" />
When Author is selected I got this in ViewModel and it works fine:
private NoteAuthor _selectedAuthor;
public NoteAuthor SelectedAuthor
{
get { return _selectedAuthor; }
set
{ if (_selectedAuthor != value)
{
SetProperty(ref _selectedAuthor, value);
}
}
}
在 ViewModel > OnNavigatingTo 函数中,我调用 GetAuthor 函数,该函数根据之前保存的 ID 返回作者。
public async void GetAuthor(int author_id)
{
NewNoteAuthor = await App.Database.GetAuthorById(author_id);
if(NewNoteAuthor != null && NewNoteAuthor.ID > 0)
{
SelectedAuthor = NewNoteAuthor;
}
}
页面打开时如何“跳转”到该作者? GetAuthor 函数中的赋值对我不起作用。
【问题讨论】:
标签: xamarin xamarin.forms prism