【问题标题】:WPF - destroy page after unloaded has runWPF - 卸载后销毁页面已运行
【发布时间】:2016-01-29 19:43:32
【问题描述】:

在 WPF 应用程序中,我们有时需要创建一个新选项卡,其中包含一个 Frame 内的 Page..

一旦打开页面(初始化一次),它似乎仍会保留在导航历史记录中并尝试加载当时可能不相关的数据。

我尝试了无数方法,包括 NavigationService.RemoveBackEntry,但它仍然存在 :-(

这是一个如何打开选项卡/页面的示例

 Private Sub CashFlow_Edit(sender As Object, e As RoutedEventArgs)
    Try
        Dim DGV As DGVx = ReportsCashFlow_Grid.FindName("CashFlow_DGV")
        e.Handled = True
        IsNewRecord = False

        If DGV.SelectedItems.Count = 1 Then
            Dim row As System.Data.DataRowView = DGV.SelectedItems(0)
            Form_ID = row("ID")
            Dim vName As String = row("Name")
            Dim vTab As STC_Tabx = Application.Current.MainWindow.FindName(TabName)
            Dim TabControl As STCx = Application.Current.MainWindow.FindName("AccountingReports_TabControl")
            If Not vTab Is Nothing Then
                vTab.Close()
            End If
            Dim MCFrame As New Frame
            Dim MCTab As New STC_Tabx
            With MCTab
                .Name = TabName
                .Header = " " & vName & " "
                .ImageSource = ReturnImageAsString("Edit.png", 16)
                .CloseButtonVisibility = DevComponents.WpfEditors.eTabCloseButtonVisibility.Visible
                .TabToolTip = "View or edit the " & vName & " template"
                .Content = MCFrame
            End With
            RemoveHandler MCTab.Closing, AddressOf TabControl_TabClosing
            AddHandler MCTab.Closing, AddressOf TabControl_TabClosing

            Dim vGrid As Grid = Application.Current.MainWindow.FindName("MainGrid_Accounting")
            RegisterControl(vGrid, MCTab)
            TabControl.Items.Add(MCTab)
            Dim MCPage As New ReportCashFlow_Page
            MCFrame.NavigationService.Navigate(MCPage)
            LoadedTabs(TabName)
            MCTab.IsSelected = True


        End If

    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub

【问题讨论】:

    标签: wpf vb.net navigation


    【解决方案1】:

    要删除所有后面的条目,请执行以下操作:

    while(NavigationService.CanGoBack)
    {
        NavigationService.RemoveBackEntry();
    }
    

    【讨论】:

    • 试过了 - 没有效果
    【解决方案2】:

    这不是我想要的干净的代码,但它可以工作 - 创建一个全局布尔值 - 当打开选项卡/页面的子被调用时,它被设置为 true 并且加载事件将仅在此情况下运行加载代码为真 - 最后设置为假。

    Private Sub ReportCashFlow_Page_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
        Try
    
    
            If IsNewTab = False Then
                Exit Sub
            End If
     'Run all the loading code here
    
    
      Catch ex As Exception
            EmailError(ex)
        Finally
            IsNewTab = False
            Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, CType(Sub() CashFlow_LoadBudget(), SendOrPostCallback), Nothing)
            Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, CType(Sub() ToggleReserve(), SendOrPostCallback), Nothing)
        End Try
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多