生命周期

在 Android 上,若主活动的 [Activity()] 属性缺少 ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,旋转时及首次启动应用程序时,将调用 OnStart 方法。

 

数据绑定

数据绑定在用户界面和应用程序之间建立连接。

官网:https://docs.microsoft.com/zh-cn/xamarin/xamarin-forms/app-fundamentals/data-binding/

原理参考:Xamarin.From中的Data binding(数据绑定)(一)

基本绑定

下图说明了这种绑定关系:

的绑定框架源会将源对象中的更改自动推送到目标对象,且目标对象中的更改可选择性地推送回源对象。

建立数据绑定的过程分为两个步骤:

  • BindingContext 属性必须设置为源。
  • 必须在目标和源之间建立绑定。 

实现绑定有两种方式:仅在xaml文件中或者仅在cs中,每种又分使用BindingContext与否。

1、在 XAML 中设置【常用方式】

Binding 标记扩展实现。

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="DataBindingDemos.AlternativeXamlBindingPage"
             Title="Alternative XAML Binding">
    <StackLayout Padding="10, 0">
        <Label Text="TEXT"
               FontSize="40"
               HorizontalOptions="Center"
               VerticalOptions="CenterAndExpand"
               Scale="{Binding Source={x:Reference slider},
                               Path=Value}" />

        <Slider x:Name="slider"
                Minimum="-2"
                Maximum="2"
                VerticalOptions="CenterAndExpand" />
    </StackLayout>
</ContentPage>
View Code

相关文章:

猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-24
  • 2021-10-27
相关资源
相似解决方案