2014-01-25 1 views
2

PropertyChangedCallback을 사용하여 종속성 속성이있는 WinRT CustomControl을 만듭니다. 그 콜백 메서드에서는 GetTemplateChild() 메서드를 사용하여 OnApplyMethod에서 검색 한 일부 컨트롤의 값을 설정하려고합니다.PropertyChangedCallback은 WinRT에서 OnApplyTemplate보다 먼저 호출합니다.

문제는 OnApplyTemplate 전에 PropertyChangedCallback을 호출하여 컨트롤 파트가 여전히 null 인 것입니다.

내가 발견 한 한 가지 해결 방법은 사용자 지정 컨트롤의로드 이벤트에서이 DP를 호출하는 것입니다. 이 경우 모든 것이 제대로 작동합니다. 그러나 적용 할 수없는 모든 상황. 누군가가 xaml을 통해 값을 바인딩하려는 경우 문제가 다시 발생한다고 가정합니다.

이 문제에 대해 영구적 인 해결 방법이있는 사람이 있습니까?

private void OnFooChanged(...) 
{ 
    if (someNamedPart != null && someOtherNamedPart != null && ...) 
    { 
     // Do something to the named parts that are impacted by Foo 
     // when Foo changes. 
    } 
} 

private void FooChangedCallback(...) 
{ 
    // Called by WinRT when Foo changes 
    OnFooChanged(...) 
} 

protected override void OnApplyTemplate(...) 
{ 
    // Theoretically, this can get called multiple times - every time the 
    // consumer of this custom control changes the template for this control. 
    // If the control has named parts which must react to the properties 
    // this control exposes, all that work must be done here EVERY TIME 
    // a new template is applied. 

    // Get and save named parts as local variables first 

    OnFooChanged(...) 
} 

는 의사 코드가 도움이되기를 바랍니다 :

답변

4

여기에 내가 당신이 설명한 일을 할 때 따라야 일반적인 패턴이다!

+0

이러한 상황이 발생할 수있는 시나리오는 무엇입니까? 템플릿을 적용하기 전에 콜백 메소드가 트리거 될 때? –

+0

XAML에서 속성 값을 설정하면 즉시 콜백이 트리거됩니다. OnApplyTemplate이 XAML에 의해 호출되기 전에 일반적으로 발생합니다. –