【问题标题】:Control template Binding Not working in MVVM Cross Xamarin Forms控件模板绑定在 MVVM Cross Xamarin 表单中不起作用
【发布时间】:2020-09-21 19:34:57
【问题描述】:

我目前正在开发一个使用 MVVMCross 的 Xamarin Forms 项目。在 App.xaml 我创建了一个控制模板如下

   <ControlTemplate x:Key="ContentPageTemplate">
            <Grid HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" RowSpacing="0" RowDefinitions="Auto,Auto,*">

                <!--StackLayout as Header-->
                <StackLayout Grid.Row="0" HeightRequest="6" BackgroundColor="{StaticResource SecondaryBrandColor}"></StackLayout>
               <Frame Padding="0" Grid.Row="1" >
                <Label Grid.Row="1" Text="Server Not Available" BindingContext="{TemplateBinding BindingContext}"
                       Style="{StaticResource ServerAvailableLabel}"
                       IsVisible="{TemplateBinding Parent.BindingContext.IsServerAvailableLabelVisible}"/>
                </Frame>
                <!--Content Page Body-->
                <ContentPresenter Grid.Row="2" BackgroundColor="{StaticResource PrimaryBodyColor}">

                </ContentPresenter>
            </Grid>

        </ControlTemplate>

现在我在 contentPage 中使用这个控件模板如下

<views:MvxContentPage  xmlns:views="clr-namespace:MvvmCross.Forms.Views;assembly=MvvmCross.Forms"
                       xmlns="http://xamarin.com/schemas/2014/forms"
                       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                       x:Class="Project.UI.Views.ReqView"
                       ControlTemplate="{StaticResource ContentPageTemplate}"
                       Title="Reqs">
</views:MvxContentPage>

在视图模型中,我试图使标签可见和不可见,如下所示

    public bool _isServerAvailableLabelVisible = false;
        public bool IsServerAvailableLabelVisible
        {
            get
            {
                return _isServerAvailableLabelVisible;
            }
            set
            {
                SetProperty(ref _isServerAvailableLabelVisible, value);
            }
        }

我现在面临的问题是,默认情况下标签是可见的,即使我将值设置为 false,它也不会更新。

我错过了什么?

【问题讨论】:

    标签: xamarin xamarin.forms mvvm mvvmcross


    【解决方案1】:

    我遇到了完全相同的问题! 一天后,我找到了以下适合我的解决方案。

    我将BindingContext="{TemplateBinding BindingContext.DataContext}"添加到网格中并将IsVisible属性更改为IsVisible="{Binding IsServerAvailableLabelVisible}"

    这里是你可以尝试的回顾,希望它也对你有用! 有关信息,我使用Xamarin.Forms 4.8.0 & MvvmCross 6.3.1

      <ControlTemplate x:Key="ContentPageTemplate">
            <Grid HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" RowSpacing="0" RowDefinitions="Auto,Auto,*"
                  BindingContext="{TemplateBinding BindingContext.DataContext}">
    
                <!--StackLayout as Header-->
                <StackLayout Grid.Row="0" HeightRequest="6" BackgroundColor="{StaticResource SecondaryBrandColor}"></StackLayout>
               <Frame Padding="0" Grid.Row="1" >
                <Label Grid.Row="1" Text="Server Not Available"
                       Style="{StaticResource ServerAvailableLabel}"
                       IsVisible="{Binding IsServerAvailableLabelVisible}"/>
                </Frame>
                <!--Content Page Body-->
                <ContentPresenter Grid.Row="2" BackgroundColor="{StaticResource PrimaryBodyColor}">
    
                </ContentPresenter>
            </Grid>
    
        </ControlTemplate>
    

    【讨论】:

      【解决方案2】:

      您确定OnPropertyChanged 事件在您更改值时会被触发吗?

      如果是,则需要检查视图的绑定上下文

      如果没有,您需要手动触发它(如 MVVMCross documentation 中所述)

      private string _myProperty;
      public string MyProperty
      {
          get => _myProperty;
          set
          {
              _myProperty = value;
              RaisePropertyChanged(() => MyProperty);
              // take any additional actions here which are required when MyProperty is updated
          }
      }
      

      【讨论】:

      • 是的,它被解雇了,我尝试了您提供的代码。还是不行
      • 因为您在标签中设置了绑定上下文属性,所以我认为您不需要在 is visible 属性中设置 Parent.Binding 上下文
      • 试了还是改不了。
      猜你喜欢
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      • 2015-10-25
      • 2020-03-06
      • 1970-01-01
      • 1970-01-01
      • 2018-10-20
      • 2018-09-15
      相关资源
      最近更新 更多