我设置了一个属性,当调用它的setter时,它会触发自定义导航事件(NavigateTo
)。到目前为止,导航在第一次尝试时工作正常,即从列表视图中选择一个项目,其中所选项目绑定到属性,然后属性设置器启动,进而触发导航事件。
但是,当我导航回主页并从列表视图中选择一个项目时,导航事件不会触发秒。
我已经调试了"NavigateTo"的调用,第二次导航回主页并从列表视图中选择时没有调用它。这向我指出,页面的构造函数没有被调用,VM的数据上下文也没有设置,这反过来意味着不会调用setter导航事件。
问题:
如何在页面之间的导航生命周期中保留数据上下文?
MainViewModel-在SelectedCouncilName
setter中调用导航事件,该事件传递一个用于导航的int参数:
private CouncilName _selectedCouncilName;
public CouncilName SelectedCouncilName
{
get
{
return _selectedCouncilName;
}
set
{
if (_selectedCouncilName != value)
{
_selectedCouncilName = value;
RaisePropertyChanged("SelectedCouncilName");
_navCallBack.NavigateTo(_selectedCouncilName.ID);
}
}
}
MainPage.xaml列表视图-所选项目绑定到SelectedCouncilName属性:
<ListView x:Name="ZonesListView"
Margin="0,0,-12,0"
ItemsSource="{Binding CouncilNameItems}"
SelectedItem="{Binding SelectedCouncilName,
Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17">
<TextBlock Style="{ThemeResource ListViewItemTextBlockStyle}"
Text="{Binding CouncilAcronym}"
TextWrapping="Wrap" />
<TextBlock Margin="12,-6,12,0"
Style="{ThemeResource ListViewItemSubheaderTextBlockStyle}"
Text="{Binding CouncilFullName}"
TextWrapping="Wrap" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
MainPage.xaml.cs-在SelectedCouncilName属性上触发setter之后,在其中触发导航事件的代码。(我在后面包含了整个页面的代码,以显示如何设置数据上下文和导航方法的详细信息):
public sealed partial class MainPage : Page, INavigationCallback
{
MainViewModel vm;
private NavigationHelper navigationHelper;
public MainPage()
{
this.navigationHelper = new NavigationHelper(this);
this.navigationHelper.LoadState += navigationHelper_LoadState;
this.navigationHelper.SaveState += navigationHelper_SaveState;
//init data context
this.NavigationCacheMode = NavigationCacheMode.Required;
this.InitializeComponent();
vm = new MainViewModel(this);
this.DataContext = vm;
vm.LoadCouncilNamesData();
}
private void navigationHelper_SaveState(object sender, SaveStateEventArgs e)
{
throw new NotImplementedException();
}
void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
throw new NotImplementedException();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// TODO: Prepare page for display here.
// TODO: If your application contains multiple pages, ensure that you are
// handling the hardware Back button by registering for the
// Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
// If you are using the NavigationHelper provided by some templates,
// this event is handled for you.
}
void INavigationCallback.NavigateTo(string ItemID)
{
Frame.Navigate(typeof(RequestTagPage), ItemID);
}
}
删除this.NavigationCacheMode = NavigationCacheMode.Required
用于在导航回时调用构造函数
如果问题是没有调用构造函数,那么我建议将视图模型设置为StaticResource或将其放置在viewmodel定位器中。当您在XAML中绑定它时,数据上下文将保持不变。
然后可以在视图模型的构造函数中安装填充,因为它将在需要时生成。