如何解决使用自己的ViewModel作为控件的ContentView
经过一段时间的努力,我从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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。