发布:2023/12/7 15:31:04作者:大数据 来源:大数据 浏览次数:501
数据绑定名称不是强类型,所以按字符串注意大小写,不会有代码提示功能。
1、后台代码cs绑定
利用SetBinding方法,将属性绑定到对应的源目标属性上,将x:name为slider的Value属性值绑定到Label的Scale属性上,在构造函数中绑定。
1 2 |
InitializeComponent(); label.SetBinding(Label.ScaleProperty, new Binding("Value", source: slider)); |
或者拆分如下
1 2 |
label.BindingContext = slider; label.SetBinding(Label.RotationProperty, "Value"); |
2、前台xaml文件绑定
格式:{Binding Source={x:Reference 控件名(x:name=的名称)},Path=属性}
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<StackLayout Padding="10, 0"> <Label Text="TEXT" FontSize="40" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" <strong>Scale="{Binding Source={x:Reference slider}, Path=Value}"</strong> /> <Slider x:Name="slider" Minimum="-2" Maximum="2" VerticalOptions="CenterAndExpand" /> </StackLayout> |
或者:
1 2 3 4 5 6 7 8 9 10 11 12 |
<StackLayout Padding="10, 0"> <Label Text="TEXT" FontSize="80" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" <strong>BindingContext="{x:Reference Name=slider}" Rotation="{Binding Path=Value}"</strong> /> <Slider x:Name="slider" Maximum="360" VerticalOptions="CenterAndExpand" /> </StackLayout> |
3、编译的绑定,通常需要一个ViewModel类,如PersonViewModel.cs,MyViewModel.cs名称可自定,然后实现,如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System.ComponentModel; using System.Runtime.CompilerServices; namespace App2.ViewModels { public class PersonViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } } |
完整代码实例xaml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?xml version="1.0" encoding="UTF-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:DataBindingDemos" x:Class="DataBindingDemos.CompiledColorSelectorPage" Title="Compiled Color Selector"> <ContentPage.Resources> <ResourceDictionary> <Style TargetType="Slider"> <Setter Property="VerticalOptions" Value="CenterAndExpand" /> </Style> <Style TargetType="Label"> <Setter Property="HorizontalTextAlignment" Value="Center" /> </Style> </ResourceDictionary> </ContentPage.Resources> <StackLayout x:DataType="local:HslColorViewModel"> <span style="color: #ff0000;"><StackLayout.BindingContext> <local:HslColorViewModel Color="Sienna" /> </StackLayout.BindingContext></span> <BoxView Color="{Binding Color}" VerticalOptions="FillAndExpand" /> <StackLayout Margin="10, 0"> <Label Text="{Binding Name}" /> <Slider Value="{Binding Hue}" /> <Label Text="{Binding Hue, StringFormat='Hue = {0:F2}'}" /> <Slider Value="{Binding Saturation}" /> <Label Text="{Binding Saturation, StringFormat='Saturation = {0:F2}'}" /> <Slider Value="{Binding Luminosity}" /> <Label Text="{Binding Luminosity, StringFormat='Luminosity = {0:F2}'}" /> </StackLayout> </StackLayout> </ContentPage> |
viewmodel文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
using System.ComponentModel; using Xamarin.Forms; namespace DataBindingDemos { public class HslColorViewModel : INotifyPropertyChanged { Color color; string name; public event PropertyChangedEventHandler PropertyChanged; public double Hue { set { if (color.Hue != value) { Color = Color.FromHsla(value, color.Saturation, color.Luminosity); } } get { return color.Hue; } } public double Saturation { set { if (color.Saturation != value) { Color = Color.FromHsla(color.Hue, value, color.Luminosity); } } get { return color.Saturation; } } public double Luminosity { set { if (color.Luminosity != value) { Color = Color.FromHsla(color.Hue, color.Saturation, value); } } get { return color.Luminosity; } } public Color Color { set { if (color != value) { color = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Hue")); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Saturation")); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Luminosity")); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Color")); Name = NamedColor.GetNearestColorName(color); } } get { return color; } } public string Name { private set { if (name != value) { name = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name")); } } get { return name; } } } } |
编译绑定类放到xaml文件中的红色代码,可实现绑定属性自动提示功能,此代码等同于在xaml.cs文件的构造函数里设置:
1 2 |
InitializeComponent(); BindingContext = new HslColorViewModel(); |
官方数据绑定github地址:
https://github.com/xamarin/xamarin-forms-samples/tree/master/DataBindingDemos/DataBindingDemos/DataBindingDemos
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4