【问题标题】:Binding to Source property of Frame doesn't work after Frame.GoBack在 Frame.GoBack 之后绑定到 Frame 的 Source 属性不起作用
【发布时间】:2010-08-06 10:09:17
【问题描述】:

我有三个页面,为了导航到每个页面,我将一个属性绑定到 Frame 的 Source 属性。如果我只是正常浏览页面,它工作得很好,但是在调用 GoBack 方法后,Frame 突然停止工作。如果我直接为 Source 属性设置 uri 而不是使用绑定,它可以正常工作,我实际上是使用 MVVM 实现的,所以我不想直接设置 Source 属性。

--xaml--

  <navigation:Frame x:Name="_frame" Source="{Binding CurrentPage}"/>

--隐藏代码--

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

有谁知道如何解决这个问题?我尝试了几种方法,但对我没有任何效果。

提前致谢,

【问题讨论】:

    标签: silverlight silverlight-4.0


    【解决方案1】:

    经过一段时间的测试,我找到了它不起作用的原因。问题是由于某种原因调用 GoBack 后,与 Source 属性的绑定被删除。因此,如果您想这样做,请以编程方式再次设置绑定,如下所示。

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

    但是你应该考虑何时重新设置绑定,否则它不能正常工作。

    【讨论】:

      【解决方案2】:

      我知道这个问题是很久以前提出的,并且您已经回答了。我在为我的项目中寻找完全相同的问题的解决方案时遇到了这个问题。

      我尝试在调用 GoBack() 和 GoForward() 后重新绑定 - 如果用户在地址栏中输入自己的路径,绑定也会中断。对我来说不幸的是,它有很多错误。

      我发现,通过将 Silverlight Frame 上的 Binding 更改为 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">
      

      我希望这也可以帮助其他一些寻找解决相同问题的可怜失落灵魂的人。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-24
        • 2015-01-04
        • 1970-01-01
        • 1970-01-01
        • 2011-01-18
        • 2021-12-01
        • 2017-11-18
        相关资源
        最近更新 更多