0

バインディング オブジェクトをどのように文字列に変換しますか? バインド可能なプロパティを使用してテキストをプロパティにバインドしようとしていますが、次のようなエラーが表示されます

Xamarin.Forms.Binding から System.string に変換できません。

BindableProperty returnType typeof(string) がこれをキャッチしたと思いました。

これが私のフロントエンドコードです (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: 位置 26:68。プロパティ "ItemSubText" を割り当てることができません: "Xamarin.Forms.Binding" と "System.String" の型が一致しません

4

1 に答える 1

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

ItemSubText プロパティのプロパティ アクセサーも修正します。

public string ItemSubText
{
    get { return (string)GetValue (SubTextProperty); }
    set { SetValue (SubTextProperty, value); }
}
于 2017-01-09T03:49:40.700 に答える