2012-06-06 3 views
0

Silverlight를 사용하여 이미지 위에 모양과 텍스트를 그립니다. 셰이프는 공통적 인 그래디언트 집합을 사용하므로 셰이프를 채우는 데 사용되는 브러시를 정의하는 데 사용하려는 GradientStopCollections의 미리 정의 된 집합이 있습니다. 이 기능은 각 GradientStopCollection을 최대 한 번 사용하는 한 사용할 수 있습니다. 두 번째로 GradientStopCollections 중 하나를 사용하여 LinearGradientBrush를 인스턴스화하려고하면 "값이 예상 범위 내에 들지 않습니다."라는 ArgumentException이 발생합니다.GradientStopCollection을 재사용하면 예외가 발생하는 이유는 무엇입니까? (값이 예상 범위에 들지 않습니다.)

 _yellowFill = new GradientStopCollection(); 
     _yellowFill.Add(new GradientStop(){ Color = Color.FromArgb(128, 255, 255, 0), Offset = 0 }); 
     _yellowFill.Add(new GradientStop() { Color = Color.FromArgb(128, 128, 128, 0), Offset = 1 }); 

...

 _shapeLinearFillBrush = new LinearGradientBrush(_yellowFill, 90); 
     ... 
     _shapeLinearFillBrush = new LinearGradientBrush(_yellowFill, 90); 

마지막 줄은 위의 예외가 발생합니다. 왜이 예외가 발생하며 GradientStopCollections를 사용하여 여러 그래디언트 브러쉬를 정의 할 수 있습니까?

+0

가 대신 정적 자원으로 컬렉션을 참조 해봤를 (예를 들어, 당신의 App.XAML에서)? –

+0

@HiTechMagic 아니요. 저는 캔버스에 배치 할 FrameworkElements를 동적으로 작성하므로 동적으로 브러시를 만들 수도 있습니다 (이후 이벤트에 개별적으로 수정하기 때문에). 내가 그랬다면 그 차이점은 무엇인가? – xr280xr

+0

브레인 스토밍 ... 한 부모에서만 GradientStopCollection()이 허용 될 수 있습니다. 나는 왜 GradientStopCollection의 해체를 조사해야 하는지를 알아야하고 다시 돌아올 필요가 있습니다. –

답변

2

이 문제는 Freezable 개체가없는 Silverlight와 관련이 있다고 생각합니다. WPF를 사용하는 경우 문제가되지 않습니다. Silverlight에는 동일한 GradientStopCollection을 다시 사용할 수있는 방법이 없습니다. 같은 GradientStop을 사용할 수도 있다고 생각하지 않습니다. 그 때문에 같은 GradientStopCollection 클론에 확장 메서드를 만들 수있는이 당신을 해결하려면 :

_yellowFill = new GradientStopCollection(); 
_yellowFill.Add(new GradientStop() { Color = Color.FromArgb(128, 255, 255, 0), Offset = 0 }); 
_yellowFill.Add(new GradientStop() { Color = Color.FromArgb(128, 128, 128, 0), Offset = 1 }); 

_shapeLinearFillBrush1 = new LinearGradientBrush(_yellowFill.Clone(), 90); 
_shapeLinearFillBrush2 = new LinearGradientBrush(_yellowFill.Clone(), 90); 

public static GradientStopCollection Clone(this GradientStopCollection stops) 
{ 
    var collection = new GradientStopCollection(); 

    foreach (var stop in stops) 
     collection.Add(new GradientStop() { Color = stop.Color, Offset = stop.Offset }); 

    return collection; 
} 
+0

감사합니다. @tjscience! 그것은 내가 온 같은 결론이었고 결국 GradientStopCollections를 템플릿으로 선언하고 직접 사용하는 대신 복제했습니다. 여전히 Freezable의 부족으로 인해 예외가 발생하는 이유는 아직도 이해할 수 없습니다. GradientStopCollection이 이미 사용 중이라는 것을 어떻게 알 수 있습니까? 왜 신경 써야합니까? – xr280xr

관련 문제