2010-08-06 78 views
0

我有三個頁面,並導航到每一頁,我將一個屬性綁定到框架的源屬性。如果我只是正常瀏覽頁面,但它在調用GoBack方法後突然停止工作,它工作得很好。如果我直接將URI設置爲Source屬性而不是使用綁定,但它工作正常,但實際上我正在使用MVVM實現,因此我不想直接設置Source屬性。綁定到幀的源屬性不起作用後Frame.GoBack

--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(object 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); 
    } 

有誰知道如何解決這個問題呢?我嘗試了幾種方法,但沒有爲我工作。

由於提前,

回答

0

測試了一段時間後,我發現一個原因,這是行不通的。問題在於,由於某種原因,在調用GoBack之後,綁定到Source屬性的操作會丟失。所以,如果你想這樣做,像下面這樣以編程方式重新設置綁定。

_frame.SetBinding(Frame.SourceProperty, new Binding() { Source = this, Path = new PropertyPath("CurrentPage") }); 

但是你應該考慮何時再次設置綁定,否則它不能正常工作。

0

我知道,這個問題被問得很久以前,它已經自己回答。我遇到了這個問題,同時尋找解決方案來解決我的項目中完全相同的問題。

在GoBack()和GoForward()調用之後,我嘗試重新綁定 - 如果用戶在地址欄中輸入自己的路徑,綁定也會中斷。不幸的是,對我來說這是相當多的錯誤。

我發現通過將Silverlight Frame上的綁定更改爲Mode = TwoWay,它可以準確地解決問題,並且不會引起仇恨。

<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"> 

我希望這也能幫助一些其他可憐的失去靈魂尋找解決同一問題的解決方案。