2017-01-06 133 views
0

如何将绑定对象转换为字符串?我试图绑定文本使用绑定属性的属性,但我发现了,说将Xamarin.Forms.Binding转换为System.string

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

我以为BindableProperty返回类型typeof运算(字符串)会陷入这样的错误。

这里是我的前端代码(App.rug.Length是一个字符串):

<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; } 
     } 
    } 

以下是错误:

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是一个静态变量,因此你会因此需要像下面

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

绑定指定它在那里

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

还修复了你的属性访问器的ItemSubText属性

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