문제

.NET 4.0 베타 2 가지다 소개 그만큼 Iobservable 그리고 iobserver 인터페이스.

고전적인 .NET 이벤트와 비교하여 장점은 무엇입니까? 이것이 같은 문제를 해결하지 않습니까?

도움이 되었습니까?

해결책

IOBServable을 이벤트로 사용할 수 있으며 이벤트를 유형 IOBServable의 속성으로 노출시키는 코드를 대체 할 수 있지만 실제로는 요점이 아닙니다.

iObservable에 대해 이해해야 할 두 가지 중요한 사항이 있습니다.

  1. 그것은 우리가 이전에 통일하는 방법을 몰랐던 두 가지 개념을 통일합니다.: 비동기 연산 (일반적으로 단일 값을 반환) 및 이벤트 (일반적으로 영원히 계속).

  2. 합성 가능합니다. CLR 이벤트와 달리 IASyncresult 또는 InotifyCollectionChanged를 사용하면 일반 이벤트 및 비동기 작업에서 특정 이벤트를 구축 할 수 있습니다.

오늘 오후에 직장에서 만난 예가 있습니다.

Silverlight에는 일반 제어에 적용 할 수없는 이미지 컨트롤에 적용 할 수있는 효과가 있습니다. 컨트롤 컨텐츠가 변경 될 때 이러한 제한 사항을 해결하려면 시각적 외관이 업데이트 될 때까지 기다릴 수 있으며 스크린 샷을 찍을 수 있습니다. 그런 다음 시각적 표현을 숨기고 스냅 샷으로 바꾸고 이미지에 시각적 효과를 적용하고 싶습니다. 이제 이미지 효과를 컨트롤에 적용 할 수 있습니다 (대화식이 아니라고 가정).

이 프로그램은 사소한 것이지만 비동기 적이어야한다는 사실 때문에. 이미지에 효과를 적용하기 전에 두 번의 연속 비동기 작업을 완료 할 때까지 기다려야합니다.

  1. 컨트롤의 내용이 변경되었습니다
  2. 컨트롤의 시각적 모양이 업데이트됩니다

rx를 사용 하여이 문제를 해결하는 방법은 다음과 같습니다.

// A content control is a control that displays content.  That content can be
// anything at all like a string or another control.  Every content control contains
// another control: a ContentPresenter.  The ContentPresenter's job is to generate
// a visual representation of the Content property. For example, if the Content property
// of the ContentControl is a string, the ContentPresenter creates a TextBlock and inserts
// the string into it.  On the other hand if the Content property is another control the 
// ContentPresenter just inserts it into the visual tree directly.
public class MyContentControl : ContentControl
{
   // A subject implements both IObservable and IObserver.  When IObserver methods
   // are called, it forwards those calls to all of its listeners.
   // As a result it has roughly the same semantics as an event that we can "raise."
   private Subject<object> contentChanged = new Subject<object>();

   // This is a reference to the ContentPresenter in the ContentControl's template
   private ContentPresenter contentPresenter; 

   // This is a reference to the Image control within ContentControl's template.  It is displayed on top of the ContentPresenter and has a cool blur effect applied to it.
   private Image contentImageControl; 

   public MyContentControl()
   {
      // Using Rx we can create specific events from general events.
      // In this case I want to create a specific event ("contentImageChanged") which
      // gives me exactly the data I need to respond and update the UI.
      var contentImageChanged = 
         // get the content from the content changed event
         from content in contentChanged
         where content != null
         // Wait for the ContentPresenter's visual representation to update.
         // ContentPresenter is data bound to the Content property, so it will
         // update momentarily.
         from _ in contentPresenter.GetLayoutUpdated().Take(1)
         select new WritableBitmap(contentPresenter, new TranslateTransform());

      contentImageChanged.Subscribe(
         contentImage => 
         {
            // Hide the content presenter now that we've taken a screen shot              
            contentPresenter.Visibility = Visibility.Collapsed; 

            // Set the image source of the image control to the snapshot
            contentImageControl.ImageSource = contentImage;
         });
   }

   // This method is invoked when the Content property is changed.
   protected override OnContentChanged(object oldContent, object newContent)
   {
      // show the content presenter before taking screenshot
      contentPresenter.Visibility = Visibility.Visible;  

      // raise the content changed "event"
      contentChanged.OnNext(newContent);   

      base.OnContentChanged(oldContent, newContent);
   }
}

이 예제는 시퀀스에 대한 두 번의 연속 작업 만 있다는 점을 감안할 때 특히 간단합니다. 이 간단한 예에서도 rx가 값을 추가한다는 것을 알 수 있습니다. 그것 없이는 이벤트가 특정 순서로 발사되고 있는지 확인하기 위해 상태 변수를 사용해야했을 것입니다. 또한 레이아웃 업 이벤트에서 분리하기 위해 꽤 못생긴 코드를 작성해야했을 것입니다.

RX로 프로그래밍 할 때 트릭은 "내 프레임 워크가 제공되기를 바랍니다."라고 생각하는 것입니다. 그런 다음 만들어주세요. 우리는 이벤트를 간단하고 입력 중심의 것들 ( "마우스 오버", "마우스 클릭", "키 업"등으로 생각하도록 훈련했습니다. 그러나 이벤트가 앱에 매우 복잡하고 구체적 일 수없는 이유는 없습니다 ( "GOOGLEMSDNMASHUPSTOCKDAARIVED", "DRAGSTARTING"및 "IMAGECONTENTCHANGEND"). 프로그램을 이런 식으로 구성 할 때 (필요한 이벤트를 정확히 만드십시오. 그리고 상태를 바꾸어 응답)) 상태 버그가 적고, 더 많이 주문되며, 더 많은 자기 설명이라는 것을 알게 될 것입니다.

알았어요? :-)

다른 팁

장점은 확실하지 않지만 고전적인 .NET 이벤트와 다음과 같은 차이점이 있습니다.

오류 알림

클래식 이벤트는 이것에 대한 별도의 이벤트가 필요하거나 EventArgs 수업 Error 확인 해야하는 속성.

통지 종료 알림

클래식 이벤트는 이것에 대한 별도의 이벤트가 필요합니다. EventArgs a Final 확인 해야하는 속성.

이벤트 기반 프로그래밍 모델의 확장 일뿐입니다. 당신은 iobserver를 구현하는 무언가를 만들고 기본적으로 당신은 "컬렉션의 무언가가 변할 때 내가 일어나고 싶은 일이 있습니다"라고 말합니다. 그런 식으로, 그것은 우리 모두가 이벤트로하고있는 일의 표준화 일뿐입니다.

그들은 ienumerable 패턴과 비교하여 큰 면적 인 것처럼 밀고 있습니다. iObservable은 "PULL"인 반면 ienumerable은 "pull"입니다.

직선 이벤트에서 볼 수있는 유일한 장점은 표준화 된 인터페이스라는 것입니다. 나는 여기에서 관측형 수집과 큰 겹치는 것을 본다 (그리고 inotifyCollectionChanged). 어쩌면 그들은 .NET을 사용하여 Perl 모토를 채택하려고 할 수도 있습니다.

당신은 확실히보아야합니다 RX 워크숍 : 관찰 대기업 대 이벤트 첨부 된 도전을 비디오하고 완료하십시오

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top