xamarin.forms - 如何访问元素 xamarin 表单的属性?
问题描述
如何从 Android 和 IOS 项目访问 xamarin 表单中页面元素的属性?
例如:
Button1.IsVisible = false;
但自从Android项目。
解决方案
您可以在文件隐藏代码中访问页面元素的属性,但这通常不是一个好的或干净的策略。
一个简单的示例是 Page.xaml:
<Button x:Name="Button1" Content="Sample String"/>
然后在你的 Page.xaml.cs
Button1.IsVisible = false;
更好的方法是您可能想查看DataBinding机制: https ://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/
作为一个小示例,您将拥有一个实现 INotifyPropertyChanged 的 BindingContext/ViewModel。有了它,你可以这样:
private bool _isVisible;
public bool IsVisible
{
get => this._isVisible;
set
{
if(this._isVisible != value){
this.IsVisible;
}
}
然后在您的 Page.xaml 中:
<Button IsVisibile="{Binding IsVisible} Content="Sample String"/>
您可以在 Page.xaml.cs 中设置 BindingContext:
this.BindingContext = new ViewModel(); //If the properties are in a separate class (preferred)
this.BindingContext = this; //If the properties are in the page class
推荐阅读
- r - 错误信息 R CRAN 上传
- r - 如何避免/禁用 .crc 文件以在 sparklyr 中写入 csv 文件?
- out-of-memory - 如何有效管理 lightgbm 的输入数据集以避免内存错误
- reactjs - setState 在 handleClick 中不起作用
- wpf - 将按钮的内容模板绑定到路径的数据中
- python - 'int' 对象在创建字典时不可迭代
- r - R 没有采用 layout_with_sugiyama 中的参数 hgap
- android - Recyclerview 未在 alertdialog 中设置
- javascript - Chrome 扩展 json 文件
- html - 单选按钮不会向左对齐