Imports System.Windows Imports System.Windows.Input Imports System.Windows.Threading Imports EgtWPFLib5 Public Class LoadingWndVM Inherits VMBase #Region "FIELDS & PROPERTIES" Enum LoadingTypes As Integer NULL = 0 OPEN = 1 IMPORT = 2 End Enum Delegate Sub CallbackLoadingDlg(ByRef CurrStep As Integer, ByRef StepText As String, ByRef nProgress As Integer, ByRef nNextProgress As Integer, ByRef MainWindow_IsActive As Boolean, ByRef bClose As Boolean) Friend Event m_CloseWindow(bDialogResult As Boolean) Friend Event m_ActivateWindow() Private m_CallbackLoading As CallbackLoadingDlg Private m_Waiting_Timer As New DispatcherTimer ' numero di passaggi di caricamento Private m_nSteps As Integer ' titolo testo di caricamento Private m_TotText As String ' numero passaggio corrente Private m_CurrStep As Integer ' testo del passaggio corrente Private m_StepText As String ' indice d'inizio di questo passaggio Private m_nStartProgress As Integer ' indice d'inizio del prossimo passaggio Private m_nNextProgress As Integer ' variabile che indica richiesta chiusura della finestra Private m_bClosing As Boolean = False Private m_bMainWindow_IsActive As Boolean = True Private m_StepProgress_Value As Double = 0 Public Property StepProgress_Value As Double Get Return m_StepProgress_Value End Get Set(value As Double) m_StepProgress_Value = value End Set End Property Private m_TotProgress_Value As Double = 0 Public Property TotProgress_Value As Double Get Return Math.Floor(m_TotProgress_Value) End Get Set(value As Double) m_TotProgress_Value = value End Set End Property Private m_StepText_Visibility As Visibility Public ReadOnly Property StepText_Visibility As Visibility Get Return m_StepText_Visibility End Get End Property Private m_StepProgress_Visibility As Visibility Public ReadOnly Property StepProgress_Visibility As Visibility Get Return m_StepProgress_Visibility End Get End Property #Region "Messages" Public ReadOnly Property TotText_Msg As String Get Return If(m_nSteps > 0, m_TotText & " (" & m_CurrStep & "/" & m_nSteps & ")", m_TotText) End Get End Property Public ReadOnly Property StepText_Msg As String Get Return m_StepText End Get End Property Public ReadOnly Property TotProgress_Msg As String Get Return m_TotProgress_Value.ToString("0.#") & "%" End Get End Property #End Region ' Messages ' Definizione comandi Private m_cmdCancel As ICommand #End Region ' FIELDS & PROPERTIES Sub New(Steps As Integer, TotText As String, callback As CallbackLoadingDlg) m_nSteps = Steps m_TotText = TotText m_CallbackLoading = callback m_bClosing = False If Steps = 1 Then m_StepText_Visibility = Visibility.Collapsed m_StepProgress_Visibility = Visibility.Collapsed Else m_StepText_Visibility = Visibility.Visible m_StepProgress_Visibility = Visibility.Visible End If ' imposto contatore m_Waiting_Timer.Interval = TimeSpan.FromMilliseconds(200) AddHandler m_Waiting_Timer.Tick, AddressOf WaitingTimer_Tick End Sub Public Sub UpdateProgress(CurrStep As Integer, StepText As String, nStartProgress As Integer, nNextProgress As Integer) m_CurrStep = CurrStep m_StepText = StepText m_StepProgress_Value = nStartProgress m_nNextProgress = nNextProgress NotifyPropertyChanged(NameOf(StepText_Msg)) NotifyPropertyChanged(NameOf(TotText_Msg)) End Sub Public Overridable Sub StartFunction() m_Waiting_Timer.Start() End Sub Protected Overridable Sub WaitingTimer_Tick() ' leggo eventuali nuovi valori Dim CurrStep As Integer = 0 Dim StepText As String = "" Dim nStartProgress As Integer = 0 Dim nNextProgress As Integer = 0 Dim bMainWindow_IsActive As Boolean = True Dim bClose As Boolean = False m_CallbackLoading(CurrStep, StepText, nStartProgress, nNextProgress, bMainWindow_IsActive, bClose) If bMainWindow_IsActive <> m_bMainWindow_IsActive Then RaiseEvent m_ActivateWindow() End If If m_bClosing Then Close() Dispatcher.CurrentDispatcher.InvokeShutdown() Return ElseIf bClose Then m_bClosing = True ' mostro completamento caricamento m_CurrStep = m_nSteps m_StepProgress_Value = 100 m_TotProgress_Value = 100 m_Waiting_Timer.Interval = TimeSpan.FromMilliseconds(1000) Else If m_CurrStep <> CurrStep Then If m_StepProgress_Value < 100 Then m_StepProgress_Value = 100 ElseIf m_StepProgress_Value = 100 Then m_StepProgress_Value = 0 m_CurrStep = CurrStep m_StepText = StepText m_nStartProgress = nStartProgress m_nNextProgress = nNextProgress End If Else ' incremento Step corrente If m_StepProgress_Value < 80 Then m_StepProgress_Value += 1 ElseIf m_StepProgress_Value < 90 Then m_StepProgress_Value += 0.4 ElseIf m_StepProgress_Value < 98 Then m_StepProgress_Value += 0.1 Else m_StepProgress_Value += 0.01 End If End If ' calcolo totale Dim dStepStart As Double = If(m_CurrStep > 1, m_nStartProgress, 0) Dim dStepEnd As Double = m_nNextProgress m_TotProgress_Value = dStepStart + ((dStepEnd - dStepStart) / 100 * m_StepProgress_Value) End If ' aggiorno grafica NotifyPropertyChanged(NameOf(StepText_Msg)) NotifyPropertyChanged(NameOf(TotText_Msg)) NotifyPropertyChanged(NameOf(StepProgress_Value)) NotifyPropertyChanged(NameOf(TotProgress_Value)) NotifyPropertyChanged(NameOf(TotProgress_Msg)) End Sub Public Sub Close() ' fermo timer m_Waiting_Timer.Stop() ' chiudo finestra RaiseEvent m_CloseWindow(True) End Sub #Region "COMMANDS" #Region "Cancel" ''' ''' Returns a command that do Open. ''' Public ReadOnly Property Cancel_Command As ICommand Get If m_cmdCancel Is Nothing Then m_cmdCancel = New Command(AddressOf Cancel) End If Return m_cmdCancel End Get End Property ''' ''' Execute the Open. This method is invoked by the OpenCommand. ''' Friend Sub Cancel() End Sub #End Region ' Cancel #End Region ' COMMANDS End Class