2017-01-06 2 views
0

Как бы вы преобразовали объект привязки в строку? Я пытаюсь связать текст свойства, используя связываемую собственность, но я получаю сообщение об ошибке:Преобразование Xamarin.Forms.Binding в System.string

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

Я принял BindableProperty ReturnType 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

Проблема, которую вы получаете, вероятно, вызвана связыванием, которое вы использовали для свойства подтекста.

Ваша переменная 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}"

исправить Также ваши Accessors недвижимость для ItemSubText property

public string ItemSubText 
{ 
    get { return (string)GetValue (SubTextProperty); } 
    set { SetValue (SubTextProperty, value); } 
} 
Смежные вопросы