c# - 使用 ac# 对象使用 xamarin 模板,而不是显式传递它的属性值
问题描述
我对 xamarin 相当陌生,我对如何使用来自对象的数据使用模板而不是向 xaml 文件传递所有模板绑定感到有点困惑。
到目前为止,我所拥有的是一个 xaml 文件,其中包含一个具有控制模板的 contentView。它还有一个附加的 c# 类,它实现了所有的属性绑定。(遗憾的是,由于 NDA 的原因,我无法分享代码)
到目前为止,我可以这样使用模板:
<ScrollView>
<StackLayout>
<controls:AppointmentCardViewTemplate
ClientImage="testLogo.png"
ClientName ="Mary Smith"
StartTime ="9:00am"
EndTime ="10:00am"
Details ="This is some text I am writing to fill in the space where details would normally go. This is because I want to test the functionality of the auto spacing when it consumes longer texts.">
</controls:AppointmentCardViewTemplate>
<controls:AppointmentCardViewTemplate
ClientImage="testLogo.png"
ClientName ="Mary Smith"
StartTime ="9:00am"
EndTime ="10:00am"
Details ="This is some text I am writing to fill in the space where details would normally go. This is because I want to test the functionality of the auto spacing when it consumes longer texts.">
</controls:AppointmentCardViewTemplate>
</StackLayout>
</ScrollView>
然而,这最终没有用,因为我想用来自客户端对象的一些数据填充这个绑定属性,而不是用这些占位符显式定义绑定。
我想要的最终功能是,当我切换到显示客户端的选项卡时,程序将收集所有必需的 clientData 对象,并为每个对象使用来自 clientData 对象的数据生成一个框架。
我一直在浏览 microsoft 文档和其他教程,但我还没有确切地确定如何实现这一点。与其给我任何形式的解决方案,我希望有人可以向我指出一个贯穿这个的教程,或者一个我可以蚕食的示例项目。
此外,我希望能够单击客户框架,它会将我带到客户的完整页面。我在这里苦苦挣扎的是如何将某个框架与 clientData 对象相关联。
解决方案
使用来自客户端对象的一些数据填充此绑定属性
在您的情况下,您可以使用BindableProperty。
在 AppointmentCardViewTemplate
添加可绑定属性,如下所示
public static readonly BindableProperty DetailProperty =
BindableProperty.Create (
"Detail", typeof(string), typeof(AppointmentCardViewTemplate), null, propertyChanged: OnDetailChanged);
public string Detail
{
get { return (string)GetValue (DetailProperty); }
set { SetValue (DetailProperty, value); }
}
static void OnDetailChanged (BindableObject bindable, object oldValue, object newValue)
{
// it will be invoked when details changed , you can handle your logic here
}
现在您可以将Detail的值绑定到ContentPage或ViewModel的属性
<controls:AppointmentCardViewTemplate
ClientImage="testLogo.png"
ClientName ="Mary Smith"
StartTime ="9:00am"
EndTime ="10:00am"
Details ="{Binding xxx}"> // xxx is a property in contentPage or ViewModel, you could set its value from other object dynamically in runtime .
</controls:AppointmentCardViewTemplate>
推荐阅读
- python - 如何知道哪个表单提交到了flask web服务器
- python - 删除文件中的倒数 3 分
- python - 函数定义中的 \* 和 \*\* 是什么意思?
- json - 内置 Azure 策略定义
- download - 无法下载 Cloudera
- java - java -version 显示的版本与路径中的版本不同
- cas - 如何在 JWT 中包含经过身份验证的用户角色?
- r - 在 R 中仅使用函数的一部分
- python - Python 请求 POST 缺少特定 URL 的 Content-Length
- session - 如果 ASP MVC 网络核心中的会话过期,则重定向到登录页面