Binding to Source property of Frame doesn't work after Frame.GoBack
I have three pages, and to navigate to each page, I'm binding a property to the Source property of the Frame. It works pretty fine if I just navigate the pages normally, but after calling the GoBack method, the Frame suddenly stopped working. If I set a uri to the Source property directly instead of using binding, it works fine though, I'm actually implementing using MVVM, so I don't want to set the Source property directly.
--xaml--
<navigation:Frame x:Name="_frame" Source="{Binding CurrentPage}"/>
--Code behind--
Uri _currentPage;
public Uri CurrentPage
{
get { return _currentPage; }
set
{
_currentPage = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("CurrentPage"));
}
}
// back
private void Button_Click(ob开发者_StackOverflowject sender, RoutedEventArgs e)
{
if ( _frame.CanGoBack)
_frame.GoBack();
}
// test1
private void Button_Click_1(object sender, RoutedEventArgs e)
{
CurrentPage = new Uri("/TestPage1.xaml", UriKind.Relative);
}
// test2
private void Button_Click_2(object sender, RoutedEventArgs e)
{
CurrentPage = new Uri("/TestPage2.xaml", UriKind.Relative);
}
// test3
private void Button_Click_3(object sender, RoutedEventArgs e)
{
CurrentPage = new Uri("/TestPage3.xaml", UriKind.Relative);
}
Does anyone know how to work around this problem? I've tried several ways, but nothing works for me.
Thanks in advance,
YooAfter testing for a while, I found a reason why it doesn't work. The problem was that the binding to the Source property is dropped after calling GoBack for some reason. So if you want to do that, set the binding again programmatically like the following.
_frame.SetBinding(Frame.SourceProperty, new Binding() { Source = this, Path = new PropertyPath("CurrentPage") });
But you should consider when to set the binding again otherwise it doesn't works properly.
Yoo
I am aware that this question was asked a long time ago and that it was already answered by yourself. I came across this question while looking for a solution to exactly the same problem in my project.
I tried re-binding after the GoBack() and GoForward() calls - the binding also breaks if the user enters their own path in the address bar. Unfortunately for me it was quite buggy.
I found that by changing the Binding on the Silverlight Frame to Mode=TwoWay it accurately fixed the issue and has caused no aggro since.
<sdk:Frame x:Name="ContentFrame"
Style="{StaticResource ContentFrameStyle}"
Source="{Binding CurrentPage, Source={StaticResource ViewModel}, Mode=TwoWay}"
Navigated="ContentFrame_Navigated"
NavigationFailed="ContentFrame_NavigationFailed"
Navigating="ContentFrame_Navigating">
I hope this also helps some other poor lost soul looking for a solution to the same problem.
精彩评论