【问题标题】:Communication between behavior and viewmodel in xamarinxamarin 中行为和视图模型之间的通信
【发布时间】:2018-09-30 17:13:41
【问题描述】:

我有一个自定义行为来验证附加到条目的视图中的电话号码:

<Entry x:Name="phoneNumber" Text="{Binding TelephoneNum, Mode=TwoWay}">
    <Entry.Behaviors>
        <behaviors:TelNumBehavior x:Name="NumValidatorUser" />
    </Entry.Behaviors>
</Entry>

我正在使用带有视图和视图模型的 mvvm 模式。 该行为具有可绑定的 isValid 属性。如何在我的 Viewmodel 中使用该值?这两个类如何通信?消息服务对我来说不是一个选项,因为我有多种行为,我需要全部验证它们。有没有办法访问行为的 isValid-att ?

【问题讨论】:

    标签: xamarin mvvm behavior


    【解决方案1】:

    为您的页面命名,以便稍后引用:

    <ContentPage x:Name="Root" etc, etc>
    

    在您的行为中将路径和来源设置为页面的绑定上下文:

    <Entry x:Name="phoneNumber" Text="{Binding TelephoneNum, Mode=TwoWay}">
        <Entry.Behaviors>
                 <behaviors:TelNumBehavior x:Name="NumValidatorUser" />
                IsValid="{Binding Source={x:Reference Root}, 
                          Path=BindingContext.YourPropertyIsValid, Mode=TwoWay}"/>
        </Entry.Behaviors>        
    </Entry>
    

    【讨论】:

      猜你喜欢
      • 2011-09-06
      • 2020-01-09
      • 1970-01-01
      • 2012-06-28
      • 1970-01-01
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 2015-02-11
      相关资源
      最近更新 更多