首页 文章

如何使用xaml设置类属性的子项? (Xamarin.forms)

提问于
浏览
-1

我正在使用xamarin.forms制作应用程序 .

我有一个名为AAStackLayout的类,它内部有一个Image .

我在使用xaml的名为AAContentPage的页面上添加了AAStackLayout .

那么,如何使用xaml从AAContentPage访问布局的图像,以便我可以更改其可绑定属性?

像这样的东西:

AAContentPage.xaml

<ContentPage>
  <local:AAStackLayout>
    <local:AAStackLayout.MyImage>
      <source="AAA.jpg"/>
    </local:AAStackLayout.MyImage>
  </local:AAStackLayout> 
</ContentPage>

AAStackLayout.xaml

<StackLayout>
  <Image x:Name="MyImage"/>
</StackLayout>

我想要的是编辑已添加的Image的可绑定属性 . 我不是想添加新的图片 .

此外,我知道我可以添加与Image相同的可绑定属性并将其应用于Image . 但我不认为这是最好的方式 .

谢谢 .

1 回答

  • 3

    您可以在 BindableProperty 中的自定义 AAStackLayout 中使用其中一个参数 BindableProperty.BindingPropertyChangedDelegate

    private static void OnImageChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var yourStack = bindable as AAStackLayout;
        yourStack.MyImage = // use newValue
        // or
        yourStack.MyImage.Source = // use newValue
        // Don't know the type of your BindableProperty
    }
    

    我的意思是:

    public partial class AAStackLayout : StackLayout
    {
        public static readonly BindableProperty MyImageProperty =
                BindableProperty.Create(nameof(MyImage),
                                        typeof(ImageSource),
                                        typeof(AAStackLayout),
                                        default(ImageSource),
                                        propertyChanged: OnImageChanged);
    
        public ImageSource MyImage
        {
            get { return (ImageSource)GetValue(MyImageProperty); }
            set { SetValue(MyImageProperty, value); }
        }
    
        public AAStackLayout()
        {
            InitializeComponent();
        }
    
        private static void OnImageChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var stack = bindable as AAStackLayout;
            stack.Image.Source = (ImageSource)newValue;
        }
    }
    
    
    <?xml version="1.0" encoding="utf-8" ?>
    <StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="CommonSample.AAStackLayout">
      <Image x:Name="Image" />
    </StackLayout>
    
    
    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:local="clr-namespace:CommonSample;assembly=CommonSample"
                 x:Class="CommonSample.Page1">
      <local:AAStackLayout MyImage="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcReayFrJYeKHSMTsZcp4GA78zygyG7SBColupjra_YVLhznN2tx" />
    </ContentPage>
    

    我不确定你能从XAML到达 <Image x:Name="Image" /> ,但你可以做下一件事:

    public partial class AAStackLayout : StackLayout
    {
        public Image MyImage
        {
            get
            {
                return this.Image;
            }
        }
    
        public AAStackLayout()
        {
            InitializeComponent();
        }
    }
    

    从代码后面的 AAStack 工作:

    var stack = new AAStackLayout();
    stack.MyImage
    

相关问题