【问题标题】:Xamarin.Forms: XAML property pass-throughXamarin.Forms:XAML 属性传递
【发布时间】:2018-08-24 14:17:14
【问题描述】:

如何在不编写代码的情况下将属性值传递给自定义控件子项?

内容页面:

<views:ButtonView Image="newViolationSmall.png" Text="{Binding ParentsText}/>

然后是ButtonView.xaml

<StackLayout 
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="SmartwebsCrossPlatform.Portable.Views.ButtonView">

    <Button Image="{Binding Image}"/>
    <Label Text="{Binding Text}" />

</StackLayout>

还有 ButtonView.cs

public partial class ButtonView : StackLayout {

...

    //text
    public static readonly BindableProperty TextProperty = BindableProperty.Create( "Text", typeof( string ), typeof( ButtonView ), null, BindingMode.Default, null, null, null, null );
    public string Text {
        get {
            return (string)GetValue( TextProperty );
        }
        set {
            SetValue( TextProperty, value );
        }
    }

    //image
    public static readonly BindableProperty ImageProperty = BindableProperty.Create( "Image", typeof( string ), typeof( ButtonView ) );
    public string Image {
        get {
            return (string)GetValue( ImageProperty );
        }
        set {
            SetValue( ImageProperty, value );
        }
    }

这不显示任何文本或图像。一个相当烦人的替代方法是同时命名 Button 和 Label,覆盖 ButtonView.OnPropertyChanged,并从代码中显式设置它们的属性,但是当我浏览此页面时,它会导致 System.ObjectDisposedException(对象名称:'Android.Graphics.Bitmap')。

@yyou 的解决方案适用于 const 值,但不适用于 parent 的属性。

【问题讨论】:

    标签: xamarin.forms inotifypropertychanged


    【解决方案1】:

    唯一缺少的是设置此自定义布局的 BindingContext。 即。

       //ButtonView.xaml.cs
            public ButtonView ()
            {
                InitializeComponent ();
                BindingContext = this;
            }
    
        //to use this ButtonView component in a page
        //ie. DetailPage.xaml
        <ContentPage  ...>
    
            <app2:ButtonView Text="{Binding ParentText}" Image="{Binding ButtonImage }"/>
    
        </ContentPage>
    
    //in DetailPage.xaml.cs
        public DetailPage() {
            InitializeComponent ();
            BindingContext = new DetailViewModel() {
                    ParentText = "sjldfdkfdjd",
                    ButtonImage = "my-button-image.png"
            };
        }
    
        //in the file DetailViewModel.cs
        namespace App2.ViewModels
        {
            public class DetailViewModel : BaseViewModel {
                private string _parentText;
                public string ParentText {
                    get { return _parentText; }
                    set { SetProperty<string>(ref _parentText, value); }
                }
    
            private string _buttonImage;
            public string ButtonImage {
                get { return _buttonImage; }
                set { SetProperty<string>(ref _buttonImage, value); }
            }
    
            //other stuff
        }
    }
    

    【讨论】:

    • 这适用于 const 值,但如何从父绑定传递值?例如。 。它给了我在“ButtonView”上找不到的属性,目标属性:ParentsText。
    • @AntonDuzenko 我已经更新了我的答案。 ParentText 应该是父 BindingContext 对象的属性。
    • 你测试过这个吗?因为对我来说它试图在 ButtonView 中查找 ParentText 属性,而不是在 DetailPage 中。
    • 绑定:在“SmartwebsCrossPlatform.Portable.Views.ButtonView”上找不到“NewViolationCommand”属性,目标属性:“SmartwebsCrossPlatform.Portable.Views.ButtonView.ButtonCommand”
    猜你喜欢
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多