从子视图传递可绑定属性值';使用Xamarin表单的父页面中的可绑定属性?
我创建了一个具有图像的视图(BlankScreen),并在该视图的代码后面创建了一个BindableProperty图标从子视图传递可绑定属性值';使用Xamarin表单的父页面中的可绑定属性?,xamarin,xamarin.forms,bindable,bindableproperty,Xamarin,Xamarin.forms,Bindable,Bindableproperty,我创建了一个具有图像的视图(BlankScreen),并在该视图的代码后面创建了一个BindableProperty图标 <customviews:BlankScreen Icon="chat" /> 视图的Xaml代码: <ContentView x:Class="Demo.CustomViews.BlankScreen" xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://sc
<customviews:BlankScreen Icon="chat" />
视图的Xaml代码:
<ContentView
x:Class="Demo.CustomViews.BlankScreen"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<ContentView.Content>
<StackLayout>
<Label Text="Hello Xamarin.Forms!" />
<Image x:Name="image"/>
</StackLayout>
</ContentView.Content>
</ContentView>
然后我使用这个视图来创建页面,所以我用BindableProperty图标包含了这个视图
<customviews:BlankScreen Icon="chat" />
但问题是聊天图标无法设置在我的视图上,这是从父页面的视图传递过来的
那么我该如何解决这个问题,请帮助?我认为它不起作用的原因是您似乎没有设置它 您的可绑定属性应如下所示:
public static readonly BindableProperty IconProperty =
BindableProperty.Create(nameof(Icon), typeof(ImageSource), typeof(BlankScreen), null, propertyChanged: IconChanged);
public ImageSource Icon
{
get => (ImageSource)GetValue(IconProperty);
set => SetValue(IconProperty, value);
}
完成此操作后,添加以下方法并更新图像:
private static void IconChanged(BindableObject bindable, object oldValue, object newValue)
{
BlankScreen screen= bindable as BlankScreen ;
screen.image.Source= newValue.ToString();
}
如果有查询,请告诉我,我发现空白屏幕中有图像,但您没有为其设置源。我绑定此图像的图标
<ContentView.Content>
<StackLayout>
<Label Text="Hello Xamarin.Forms!" />
<Image x:Name="image" Source="{Binding Icon}" />
</StackLayout>
</ContentView.Content>
最后,我在Contentpage中使用这个自定义视图
<ContentPage.Content>
<StackLayout>
<customview:BlankScreen Icon="a1.jpg" />
</StackLayout>
</ContentPage.Content>
工作正常。谢谢@FreakyAli的回复,我这样做了,但图像不可见。你确定你的应用程序的本机资源中有一个名为“聊天”的东西吗?是的,我有,当我放置另一个图像时,它具有相同的聊天图标,但直接没有可绑定属性,它是可见的。无法使用BindableProperty获取图标。重复的
<ContentPage.Content>
<StackLayout>
<customview:BlankScreen Icon="a1.jpg" />
</StackLayout>
</ContentPage.Content>