class MyPageViewModel : ObservableObject
{
// 更新时调用父类ObservableObject.SetProperty更新值并通知UI更新
private bool _isVal = false;
public bool IsVal{ get => _isVal ; set => SetProperty(ref _isVal , value); }
// 数组,调用Xyz[0]=1实际是调用get,只有Xyz = new double[]{0,0,0}才会调用set,才会通知UI更新, 或者调用自定义函数SetXyz,内部在调用OnPropertyChanged通知更新
private double[] _xyz = {3.2, 5.6, 9.5 };
public double[] Xyz { get => _xyzRxyz; set =>SetProperty(ref _xyzRxyz, value); }
public void SetXyz(int index, double newVal)
{
if (_xyz[index] == newVal)
{
return;
}
OnPropertyChanging(nameof(Xyz));
_xyz[index]= newVal;
OnPropertyChanged(nameof(Xyz));
}
//
public ICommand MyCommand{ get; private set; }
MyPageViewModel()
{
// 参数为一个整数
MyCommand= new RelayCommand<int>(IntParameterCommand);
}
private void IntParameterCommand(int val)
{
}
}
<!--Label绑定多个数值并格式化显示-->
<Label FontSize="18" HorizontalOptions="Center" >
<Label.Text>
<MultiBinding StringFormat="{}({0:F2},{1:F2},{2:F2})">
<Binding Path="Xyz[0]"/>
<Binding Path="Xyz[1]"/>
<Binding Path="Xyz[2]"/>
</MultiBinding>
</Label.Text>
</Label>
<!--Command中传递一个整数参数-->
<Button
Text="一个按钮"
Command="{Binding MyCommand}"
>
<Button.CommandParameter>
<x:Int32>2</x:Int32>
</Button.CommandParameter>
</Button>
<!--本来想写如下格式的,觉得内部可以自动转,但是运行时报错,说要求传入int32但是传入的是string-->
<Button
Text="一个按钮"
Command="{Binding MyCommand}"
CommandParameter="2"
/>
<!--Command中传递一个json字符串参数,内部再解析Json,变相的传递多个参数-->
<Button
Text="json按钮"
Command="{Binding MyCommandJson}"
>
<Button.CommandParameter>
<x:String>{"A":"b","C":123}</x:String>
</Button.CommandParameter>
</Button>
Json部分
判断是否存在项
jsonRoot["body"].SelectToken("aaa") != null
判断项值是否为null
jsonRoot["body"]["aaa"].Type != JTokenType.Null