问题描述
经过一段时间的努力,我从ContentView继承了一个控件,该控件具有自己的viewmodel来管理各种参数,从父页面我只需要传递一个Property,但是我无法传递它跨入viewmodel中,如果我使用视图模型,则使用以下内容,并且效果很好
父页面
<controls:CountdownTimerControl EndDate="{ Binding BidEndDate}">
控制
public static readonly BindableProperty EndDateProperty =
BindableProperty.Create(nameof(EndDate),typeof(DateTime),typeof(CountdownTimerControl),default(DateTime),BindingMode.TwoWay,propertyChanged: (bindable,oldVal,newVal) => ((CountdownTimerControl)bindable).OnIsShown((DateTime)newVal));
public DateTime EndDate
{
get => (DateTime)GetValue(EndDateProperty);
set => SetValue(EndDateProperty,value);
}
但是,如果我更改控件以使用视图模型,则上述p [roperty不会通过BindableProperty传递
<controls:CountdownTimerControl EndDate="{ Binding BidEndDate}">
<controls:CountdownTimerControl.BindingContext>
<viewmodels:Countdownviewmodel></viewmodels:Countdownviewmodel>
</controls:CountdownTimerControl.BindingContext>
</controls:CountdownTimerControl>
那么它根本不会传递属性
苦苦挣扎,不胜感激
欢呼 A
解决方法
代替EndDate =“ {Binding BidEndDate}”,
写EndDate =“ {Binding Source = {x:Reference dem},Path = BindingContext.BidEndDate}”
其中'dem'是主页的名称。
<?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:Markup="clr-namespace:Demo.Helper.MarkupExtensions"
xmlns:viewModels="clr-namespace:Demo.ViewModels"
xmlns:controls="clr-namespace:Demo.Views.UserControls"
x:Name="dem"
x:Class="Demo.Views.Dem">
<ContentPage.BindingContext>
<viewModels:DemVM />
</ContentPage.BindingContext>
<controls:CountdownTimerControl EndDate="{Binding Source={x:Reference dem},Path=BindingContext.BidEndDate}">
<controls:CountdownTimerControl.BindingContext>
<viewModels:CountdownViewModel/>
</controls:CountdownTimerControl.BindingContext>
</controls:CountdownTimerControl>
</ContentPage>