首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ContentProperty中使用Xamarin.Forms?

如何在ContentProperty中使用Xamarin.Forms?
EN

Stack Overflow用户
提问于 2018-06-19 11:27:02
回答 1查看 1.4K关注 0票数 2

我有下面的代码我的页面XAML:

代码语言:javascript
复制
<?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:effects="clr-namespace:Coodo.Effects;assembly=Coodo"
             x:Class="Coodo.Pages.MessagePage">


    <effects:ContainerWithShadow>
        <Label Text="123" />
    </effects:ContainerWithShadow>

</ContentPage>

和用于存储视图的ContainerWithShadow

代码语言:javascript
复制
using Xamarin.Forms;

namespace Coodo.Effects
{
    [ContentProperty("ContainerContent")]
    public class ContainerWithShadow : ContentView
    {
        public View ContainerContent { get; set; }
    }
}

但我的标签XAML没有绑定到ContainerWithShadow.ContainerContent。如果设置断点,代码不会停止在setter上。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-19 11:50:36

ContainerContent属性必须是BindableProperty.需要更改以下代码:

代码语言:javascript
复制
[ContentProperty("Conditions"), Preserve(AllMembers = true)]
public class ContainerWithShadow : ContentView
{

    public static readonly BindableProperty StateProperty = BindableProperty.Create(nameof(ContainerWithShadowChild), typeof(object), typeof(View), propertyChanged:PropertyChanged);

    private static void PropertyChanged(BindableObject bindable, object oldvalue, object newvalue)
    {
    }

    public View ContainerWithShadowChild
    {
        get => (View)GetValue(StateProperty);
        set => SetValue(StateProperty, value);
    }
}

代码就能正常工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50927289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档