2012-03-01 3 views
3

WPFLocalizationExtension (Available on CodePlex)을 사용하여 WPF 응용 프로그램의 문자열을 현지화합니다. 이 간단한 MarkupExtension은 다음과 같이 간단한 시나리오에서 잘 작동 :MarkupExtension : 간단한 속성을 DependencyProperty로 변환

<Button Content="{lex:LocText MyApp:Resources:buttonTitle}" /> 

하지만 같은 좀 더 복잡한 일을 시도로서 내가 바로 붙어있어 (자원 windowTitle = "MyApp v{0}"으로)

<Window Title="{lex:LocText MyApp:Resources:windowTitle, FormatSegment1={Binding Version}}" /> 

.

FormatSegment1은 일반 INotifyPropertyChange 속성이므로 아무 것도 바인딩 할 수 없습니다. FormatSegment1이 DependencyProperty 인 경우 가능하므로 소스를 다운로드하고 patch'em을 시도했습니다.

나는 MarkupExtension에서

[MarkupExtensionReturnType(typeof(string))] 
public class LocTextExtension : BaseLocalizeExtension<string> 
{ 

    // ---- OLD property 

    //public string FormatSegment1 
    //{ 
    // get { return this.formatSegments[0]; } 
    // set 
    // { 
    //  this.formatSegments[0] = value; 
    //  this.HandleNewValue(); 
    // } 
    //} 

    // ---- NEW DependencyProperty 

    /// <summary> 
    /// The <see cref="FormatSegment1" /> dependency property's name. 
    /// </summary> 
    public const string FormatSegment1PropertyName = "FormatSegment1"; 

    /// <summary> 
    /// Gets or sets the value of the <see cref="FormatSegment1" /> 
    /// property. This is a dependency property. 
    /// </summary> 
    public string FormatSegment1 
    { 
     get 
     { 
      return (string)GetValue(FormatSegment1Property); 
     } 
     set 
     { 
      SetValue(FormatSegment1Property, value); 
     } 
    } 

    /// <summary> 
    /// Identifies the <see cref="FormatSegment1" /> dependency property. 
    /// </summary> 
    public static readonly DependencyProperty FormatSegment1Property = DependencyProperty.Register(
     FormatSegment1PropertyName, 
     typeof(string), 
     typeof(LocTextExtension), 
     new UIPropertyMetadata(null)); 

    // ... 
} 

BaseLocalizeExtension 클래스는 상속 수정 : 내가 빌드 할 때

public abstract class BaseLocalizeExtension<TValue> : MarkupExtension, IWeakEventListener, INotifyPropertyChanged 

, 나는 보통 "GetValue/SetValue does not exist in current context" 오류가 발생합니다. 나는 클래스를 DependencyObject에서 상속 받도록하려고 노력하지만 많은 오류가 발생합니다.

MarkupExtension에서 xaml Y 인드 가능 DependencyProperty (또는 Y 인드 가능)를 사용할 수 있습니까? 당신은 당신이 대신 연결된 속성에 갈 수

+0

[** 너무 오래 클래스는 ** DependencyObject에서 파생로, 당신이 그것을 종속성 속성 수 있도록함으로써 DependencyProperty 식별자와 속성을 백업하는 옵션이 있습니다. (http://msdn.microsoft .com/en-us/library/ms753358.aspx) –

+0

@jberger DependencyObject에서 클래스를 파생시키는 경우 어떻게 MarkupExtension으로 동작합니까? –

+0

나는 할 수 있다고 생각하지 않는다. 패턴을 변경해야합니다. Simon의 대답을 수정 해 볼 수 있습니다. –

답변

0

힌트에 대한

는 내가보기로는 유일한 옵션입니다, 감사합니다.

public static readonly DependencyProperty FormatSegment1Property = DependencyProperty.RegisterAttached(
     "FormatSegment1", typeof(string), typeof(LocTextExtension), new PropertyMetadata(default(string))); 

public static void SetFormatSegment1(DependencyObject element, string value) 
{ 
    element.SetValue(FormatSegment1Property, value); 
} 

public static string GetFormatSegment1(DependencyObject element) 
{ 
    return (string)element.GetValue(FormatSegment1Property); 
} 

.

<Window Title="{lex:LocText MyApp:Resources:windowTitle}" lex:LocText.FormatSegment1="{Binding Version}" /> 
+0

귀하의 솔루션을 시도하고 디자이너에서 NullRefException이 발생합니다. System.NullReferenceException 개체 참조가 개체의 인스턴스로 설정되지 않았습니다. MS.Internal.Design.Markup.MarkupExtensionParser.ParseNamedArguments (TypeNode 형식, List'1 인수) –