2017-01-06 5 views
0

어떻게 바인딩 오브젝트를 문자열로 변환 하시겠습니까? 나는 바인딩 속성을 사용하여 속성에 텍스트를 결합하기 위해 노력하고있어,하지만 난이 잡은 것Xamarin.Forms.Binding을 System.string으로 변환하십시오.

cannot convert from Xamarin.Forms.Binding to System.string.

내가 대해서 typeof (문자열)을 BindableProperty returnType이 가정라는 오류를 받고 있어요.

다음
<local:MenuItem ItemTitle="Length" ItemImage="icons/blue/next" ItemSubText="{Binding App.rug.Length}" PageToo="{Binding lengthpage}"/> 

내 백엔드 코드입니다 : 여기

public class MenuItem : ContentView 

    { 
     private string itemsubtext { get; set; } 


     public static BindableProperty SubTextProperty = BindableProperty.Create("ItemSubText", typeof(string), typeof(MenuItem), null, BindingMode.TwoWay); 

     public MenuItem() 
     { 
      var menuTapped = new TapGestureRecognizer(); 
      menuTapped.Tapped += PushPage; 


      StackLayout Main = new StackLayout 
      { 
       Children = { 

        new SectionLine(), 
        new StackLayout { 

         Padding = new Thickness(10), 
         Orientation = StackOrientation.Horizontal, 
         HorizontalOptions = LayoutOptions.Fill, 
         Children = { 

          new Label { 

           Margin = new Thickness(10, 2, 10, 0), 
           FontSize = 14, 
           TextColor = Color.FromHex("#c1c1c1"), 
           HorizontalOptions = LayoutOptions.End, 
           Text = this.ItemSubText 

          } 
         } 
        } 
       } 
      }; 

      Main.GestureRecognizers.Add(menuTapped); 
      Content = Main; 
     } 

     public string ItemSubText 
     { 
      get { return itemsubtext; } 
      set { itemsubtext = value; } 
     } 
    } 

오류된다

다음

내 프런트 엔드 코드입니다 (App.rug.Length은 문자열입니다)

Xamarin.Forms.Xaml.XamlParseException: Position 26:68. Cannot assign property "ItemSubText": type mismatch between "Xamarin.Forms.Binding" and "System.String"

답변

0

문제는 아마도 subtext 속성에 사용한 바인딩 때문일 수 있습니다.

귀하의 변수 App.rug.Length

xmlns:local="clr-namespace:{App Namespace here}"

은 또한 대한 속성 접근을 고칠 경우 정적 변수가 너무 그러므로 당신이 아래

<local:MenuItem ItemTitle="Length" ItemImage="icons/blue/next" ItemSubText="{Binding Source={x:Static local:App.rug.Length}}" PageToo="{Binding lengthpage}"/>

처럼 바인딩에를 지정해야합니다입니다 ItemSubText 속성

public string ItemSubText 
{ 
    get { return (string)GetValue (SubTextProperty); } 
    set { SetValue (SubTextProperty, value); } 
}