2015-02-04 5 views
0

을 사용할 때 이벤트가 실행되지 않습니다. 속성에 바인딩 된 목록이 있습니다. "ListLoaded"로드 이벤트도 있습니다.ListView loaded 이벤트 팩토리에서 C#

<ListView ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" BorderThickness="0" HorizontalAlignment="Left" ItemsSource="{Binding DoctorDetailsBooking,Mode=TwoWay}"> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="Loaded"> 
      <cmd:EventToCommand Command="{Binding ListLoaded}" PassEventArgsToCommand="True" /> 
      </i:EventTrigger> 
    </i:Interaction.Triggers> 

로드 이벤트에서 첫 번째 선택한 항목을 true로 설정하고 해당 항목의 배경색을 변경합니다.

일부 조작 및 API 호출은 ViewModel의 생성자에서 수행됩니다. 로드 이벤트도 생성자에서 설정됩니다. 화면을로드하는 데 약간의 시간이 걸립니다. 그래서 작업 팩토리에 생성자에 전체 코드를 추가하고 그에 따라 진행률 표시 줄을 설정했습니다.

Task tskOpen = Task.Factory.StartNew(() => 
      { 
       ProgressBarVisibility = Visibility.Visible; 

       DataAccess data = new DataAccess(); 
       DoctorDetailsBooking = data.GetDoctorsList(Convert.ToDateTime(BookingDate).Date); 
       FillSlots(); 
       **ListLoaded = new RelayCommand<RoutedEventArgs>(ListViewLoaded);** 

      }).ContinueWith(t => 
       { 
        ProgressBarVisibility = Visibility.Hidden; 
       }, TaskScheduler.FromCurrentSynchronizationContext());  

문제는 Task 내에 코드를 제공 할 때 ListeViewLoaded 이벤트가 실행되지 않습니다. 따라서 목록보기가 제대로로드되지 않습니다. 작업 코드 부분을 제거하면 이벤트가 시작되고 모든 것이 잘 작동합니다.

스레딩 및 작업 개념을 잘 모르겠습니다. 내가 여기서 뭔가를 놓치고 있니?

+1

아마도 'ListLoaded' 명령이 작업 내에 설정되기 전에 컨트롤이로드 될 것입니다. 따라서 작업 전에 ListLoaded 할당을 이동하는 것이 좋습니다. – Grx70

+0

그러나 ListLoaded는 FilleSlots() 메서드에서 설정되는 일부 속성을 사용합니다. 그래서 과제 위에 과제를 옮겼을 때, 과제가 제대로로드되지 않았습니다. 마찬가지로 작업 후 할당을 이동하면 목록이 먼저로드 된 다음 메서드가 실행됩니다 (태스크 내부에 있음). –

답변

1

올바르게 이해하면 지연된 이벤트 처리에 직면하게됩니다. 즉, 즉시 처리되지 않지만 일부 조건이 충족 된 후에 처리됩니다. 내가 한 것은 콜렉션에 해고 된 이벤트의 인수를 저장 한 다음 조건이 충족 된 후에 핸들러를 호출하는 것입니다. 귀하의 경우는이 비슷한 것 :

//create a queue to store event args 
var deferredEventArgs = new Queue<RoutedEventArgs>(); 
//temporarily assign a handler/command that will store the args in the queue 
ListLoaded = new RelayCommand<RoutedEventArgs>(e => deferredEventArgs.Enqueue(e)); 

Task tskOpen = Task.Factory.StartNew(() => 
{ 
    ProgressBarVisibility = Visibility.Visible; 
    DataAccess data = new DataAccess(); 
    DoctorDetailsBooking = data.GetDoctorsList(Convert.ToDateTime(BookingDate).Date); 
    FillSlots(); 

    //assign the proper handler/command once its ready 
    ListLoaded = new RelayCommand<RoutedEventArgs>(ListViewLoaded); 

}).ContinueWith(t => 
{ 
    //"flush" the queue - handle previous events 
    //decide whether it should be done on the UI thread 
    while(deferredEventArgs.Any()) 
     ListViewLoaded(deferredEventArgs.Dequeue()); 
    ProgressBarVisibility = Visibility.Hidden; 
}, TaskScheduler.FromCurrentSynchronizationContext()); 

당신은 하나의 변수 대신 큐의 충분이 경우 발생한 모든 이벤트 또는 마지막 하나를 처리할지 여부를 고려할 수 있습니다.

+0

그게 효과가! 감사합니다 전리품 @ Grx70 ... 크게 도움을 주셔서 감사합니다 .. –

+0

나는 아래 링크에 또 다른 의심을 게시했습니다. 시간이 있으면 도와주세요. http://stackoverflow.com/questions/28320641/task-in-click-event-does-not-handle-ui-related-changes-wpf –