xamarin - 如何在我创建的 XAML 模板中切换开关时捕获?
问题描述
我有这个模板:
<?xml version="1.0" encoding="utf-8"?>
<StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Test;assembly=Test"
x:Class="Test.Templates.SwitchTemplate"
x:Name="this" >
<Switch IsToggled="{Binding IsToggled, Source={x:Reference this}}" />
</StackLayout>
我的 CS 后端如下所示:
public partial class SwitchTemplate : StackLayout
{
public SwitchTemplate()
{
InitializeComponent();
}
public static readonly BindableProperty IsToggledProperty =
BindableProperty.Create(
nameof(IsToggled),
typeof(bool),
typeof(SwitchTemplate),
default(bool));
public bool IsToggled
{
get { return (bool)GetValue(IsToggledProperty); }
set { SetValue(IsToggledProperty, value); }
}
}
我想做的是在 XAML 的后端 CS 中调用一个方法,在切换状态更改时使用模板。
有人可以给我一些关于如何编码 XAML 模板、其后端 CS 和使用该模板的 XAML 的 CS 的建议,以便我可以在切换状态更改时执行一些操作吗?
解决方案
尝试将此 (propertyChanged) 添加到您的 Create
public static readonly BindableProperty IsToggledProperty =
BindableProperty.Create(
nameof(IsToggled),
typeof(bool),
typeof(SwitchTemplate),
default(bool),
propertyChanged: PropertyChanged);
并添加此方法:
private static void PropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var control = (SwitchTemplate)bindable;
//Do something :)
}
推荐阅读
- python - 如何在python中将回溯消息打印到新行
- css - 我需要帮助使几个元素对我网站上的视口更具响应性。(CSS)
- postgresql - 在修饰符反对 JS 中使用关系映射
- amazon-web-services - AWS SAM - 在另一个 SAM 模板中部署新的 Lambda 函数时,有什么方法可以附加现有的 API 网关
- python - 部署后Django heroku编程错误数据库不存在
- c# - 如何在 C# 中创建运行命令行实用程序的绝对路径
- google-cloud-platform - 如何将谷歌云虚拟机链接到子域
- python - 如何在python中获得相当于top的TIME+列
- bash - 您可以将 Bash 别名与不带空格的字符串连接起来吗?
- google-chrome - 禁用功能:Chrome 标志中的默认 cookie 相同的站点