나는 보통 역 뷰와 뷰 모델에 대해 알 수 있도록 모델 사이의 관계. 이 예에서는 진행 대화 상자에 IProgress
인터페이스에 대한 참조가 있습니다. 그런 다음 ProgressChanged
이벤트에 연결되므로 뷰는 필요한 경우 자체를 업데이트 할 수 있습니다. 이것의 가장 큰 장점은 여러 클래스 내의 코드가 중복되지 않는다는 것입니다. 얼마나 많은 코드가 남았는지 알려주는 코드 만 해당 클래스 안에 있습니다. 이렇게하면 진행 상황을 자주 내보내는 클래스의 진행 업데이트를 매우 쉽게 클램핑 할 수 있습니다.
interface IProgress
{
event EventHandler ProgressChanged;
int ProgressTarget { get; }
int CurrentProgress { get; }
}
그리고 구현 클래스 :
그냥 당신에게 나는 보통 무엇 아이디어를 제공합니다. 나는 그것이 작동해야하는지조차 모르겠다 -이 인터페이스를 구현하는 방법에 대한 인상을 주기만하면된다.
class StreamCopier: IProgress
{
private Stream _source;
private Stream _destination;
public StreamCopier(Stream source, Stream destination)
{
_source = source;
_destination = destination;
}
public void WriteAll()
{
int b;
while ((b = _source.ReadByte()) != -1)
{
_destination.WriteByte((byte)b);
EventRaiser.Raise(ProgressChanged, this); // Just one call here! Can't be less
}
}
public event EventHandler ProgressChanged;
public int ProgressTarget {
get { return (int)_source.Length; }
}
public int CurrentProgress {
get { return (int)_destination.Position; }
}
}
그리고 나서 EventRaiser
클래스입니다. 핸들러 참조가 매개 변수 스택에서 전달되는 방식에 주목하십시오. 따라서 'tmp'에 대한 스레드 안전 복사가 필요하지 않습니다! :)
static class EventRaiser
{
public static void Raise(EventHandler handler, object sender, EventArgs args)
{
handler(sender, args);
}
public static void Raise(EventHandler handler, object sender)
{
Raise(handler, sender, EventArgs.Empty);
}
}
그런 기능이 기본 클래스에 속하지 않는 이유는 무엇입니까? 기본 클래스의 핵심은 파생 클래스간에 공유 기능을 제공하는 것입니다. –
모든 클래스가 동일한 방식으로 진행률을보고하는 이유는 무엇입니까? – vidstige
@vidstige 업데이트 할 진행률 표시 줄을 지정하고 진행률 막대를 업데이트 할 수 있습니다. 모든 클래스가 지정된 진행률 막대를 업데이트합니다. 그들은 모두이 예에서 같은 방법으로 진행 상황을보고합니다. –