私はsetterが呼び出されたときにカスタムナビゲーションイベント(NavigateTo
)をトリガーするプロパティを設定しました。これまでのナビゲーションは、最初の試行で正しく動作します。つまり、選択したアイテムがプロパティにバインドされているリストビューからアイテムが選択された後、プロパティセッターが起動し、ナビゲーションイベントがトリガーされます。ナビゲーションイベントでデータコンテキストを保持する方法は?
しかし、メインページに戻ってリストビューから項目を選択すると、second時間が経過してもナビゲーションイベントは起動しません。
私はメインページに戻って移動して、リストビューから選択した二度目と呼ばれていない「NavigateTo」の呼び出しをデバッグしてきました。これは、ページのコンストラクタが呼び出されておらず、VMへのデータコンテキストが設定されていないことを示しています。これは、setterナビゲーションイベントが呼び出されないことを意味します。
質問:
どのように私は、ページ間のナビゲーションのライフサイクルの間にデータコンテキストを保存することができますか?
MainViewModel - ナビゲーションイベントがSelectedCouncilName
セッターで呼び出され、ナビゲーション用のint型のparamを下に渡します
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プロパティでセッターがトリガーされた後、ナビゲーションイベントが発生します。 (私はデータコンテキストが設定されている方法の詳細とナビゲーション方法を示すために、後ろのページ全体のコードを含めました):
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
を削除しますか?それも試しましたか? – ArchanaNavicationCacheModeをRequiredに設定したため、コンストラクターが呼び出されませんでした。あなたが戻ったときにDataContextが失われたとは思わない – Archana
@LovetoCodeナビゲーションのライフサイクル中に呼び出されたコンストラクタを許可する 'NavicationCacheMode = required'行を削除すると答えました。 –