ContentView 中 class 的数据绑定属性
Databind properties of a class in ContentView
我正在努力实现我认为可能非常简单的目标,但由于我是 Xamarin 和数据绑定的新手,我认为我正在陷入困境。
我有一个非常简单的 ContentPage
,它只有一个数据绑定到我的页面视图模型和我的 ContentView
,TotalsTemplate。
<ContentPage.BindingContext>
<vm:DealsTodayViewModel />
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout>
<template:TotalsTemplate></template:TotalsTemplate>
</StackLayout>
</ContentPage.Content>
我的视图模型有一个 public 属性 我的 class,Totals,它有基本的 int、string、decimal 道具。
public class DealsTodayViewModel
{
Public string ViewModelPeriod;
public PeriodTotals Totals;
public DealsTodayViewModel()
{
ViewModelPeriod = "TODAY";
Totals = new PeriodTotals
{
Period = "DAILY",
ClientServices_Deals_Chicago = 1,
ClientServices_Deals_Manchester_Na = 1,
ClientServices_Deals_Manchester_Uk = 1,
ClientServices_Ramp_Chicago = 1.2m,
ClientServices_Ramp_Manchester_Na = 1.3m,
ClientServices_Ramp_Manchester_Uk = 1.4m
};
}
}
现在在我的 TotalsTemplte
ContentView
我有一个 Grid
里面有下面的东西。
<Label Text="{***Binding ViewModelPeriod***}" FontAttributes="Bold"/>
<Frame OutlineColor="Black" Grid.Row="0" Grid.Column="1">
<Label Text="{Binding ***Totals.Period***}" FontAttributes="Bold"/>
</Frame>
我的 DealsTodayViewModel
上的字符串 属性 在我的 ContentView
中可见,但在我的总计 属性 中的 Perod
属性 中不可见,我是不是绑定错了?
从 document 开始,数据绑定应该绑定在 properties
之间而不是字段:
Data binding is the technique of linking properties of two objects so
that changes in one property are automatically reflected in the other
property. Data binding is an integral part of the Model-View-ViewModel
(MVVM) application architecture.
所以解决方案是将虚拟机中的字段更改为属性:
public class DealsTodayViewModel
{
public string ViewModelPeriod { get; set; }
public PeriodTotals Totals { get; set; }
public DealsTodayViewModel()
{
...
}
}
参考:field and property
我正在努力实现我认为可能非常简单的目标,但由于我是 Xamarin 和数据绑定的新手,我认为我正在陷入困境。
我有一个非常简单的 ContentPage
,它只有一个数据绑定到我的页面视图模型和我的 ContentView
,TotalsTemplate。
<ContentPage.BindingContext>
<vm:DealsTodayViewModel />
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout>
<template:TotalsTemplate></template:TotalsTemplate>
</StackLayout>
</ContentPage.Content>
我的视图模型有一个 public 属性 我的 class,Totals,它有基本的 int、string、decimal 道具。
public class DealsTodayViewModel
{
Public string ViewModelPeriod;
public PeriodTotals Totals;
public DealsTodayViewModel()
{
ViewModelPeriod = "TODAY";
Totals = new PeriodTotals
{
Period = "DAILY",
ClientServices_Deals_Chicago = 1,
ClientServices_Deals_Manchester_Na = 1,
ClientServices_Deals_Manchester_Uk = 1,
ClientServices_Ramp_Chicago = 1.2m,
ClientServices_Ramp_Manchester_Na = 1.3m,
ClientServices_Ramp_Manchester_Uk = 1.4m
};
}
}
现在在我的 TotalsTemplte
ContentView
我有一个 Grid
里面有下面的东西。
<Label Text="{***Binding ViewModelPeriod***}" FontAttributes="Bold"/>
<Frame OutlineColor="Black" Grid.Row="0" Grid.Column="1">
<Label Text="{Binding ***Totals.Period***}" FontAttributes="Bold"/>
</Frame>
我的 DealsTodayViewModel
上的字符串 属性 在我的 ContentView
中可见,但在我的总计 属性 中的 Perod
属性 中不可见,我是不是绑定错了?
从 document 开始,数据绑定应该绑定在 properties
之间而不是字段:
Data binding is the technique of linking properties of two objects so that changes in one property are automatically reflected in the other property. Data binding is an integral part of the Model-View-ViewModel (MVVM) application architecture.
所以解决方案是将虚拟机中的字段更改为属性:
public class DealsTodayViewModel
{
public string ViewModelPeriod { get; set; }
public PeriodTotals Totals { get; set; }
public DealsTodayViewModel()
{
...
}
}
参考:field and property