基本视图的Xamarin绑定属性

时间:2019-01-29 09:21:29

标签: c# xaml xamarin xamarin.forms bindable

我有一个基本的XAML视图,该视图具有一个具有可绑定属性的自定义控件,该控件用于显示/隐藏Grid控件。 我需要从继承自基本View的XAML View中更改此属性。

自定义控件视图

using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Syncfusion.SfBusyIndicator.XForms;
using System.Runtime.CompilerServices;

namespace TEST_SF
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class VolosLoading : ContentView
{
    private static Grid _LoadingContainer = null;

    public bool Mostra
    {
        get { return (bool)GetValue(MostraProperty); }
        set { SetValue(MostraProperty, value); OnPropertyChanged(nameof(Mostra)); }
    }

    public static BindableProperty MostraProperty = BindableProperty.Create(
       propertyName: nameof(Mostra),
       returnType: typeof(bool),
       declaringType: typeof(VolosLoading),
       defaultValue: false,
       defaultBindingMode: BindingMode.TwoWay
       , propertyChanged: MostraPropertyChanged
   );

    private static void MostraPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {

        _LoadingContainer.IsEnabled = (bool)newValue;
        _LoadingContainer.IsVisible = (bool)newValue;
    }

    public VolosLoading()
    {
        InitializeComponent();
        _LoadingContainer = (Grid)FindByName("LoadingContainer");
        OnPropertyChanged(nameof(Mostra));
    }
 }
}

其视图

<?xml version="1.0" encoding="UTF-8"?>
<ContentView 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:busyindicator="clr-namespace:Syncfusion.SfBusyIndicator.XForms;assembly=Syncfusion.SfBusyIndicator.XForms"
    x:Class="TEST_SF.VolosLoading">

    <ContentView.Content>        
        <Grid x:Name="LoadingContainer" IsEnabled="{Binding Mostra}" IsVisible="{Binding Mostra}">            
            <Grid BackgroundColor="LightGray" Opacity="0.6" />
            <busyindicator:SfBusyIndicator x:Name="Loading" AnimationType="DoubleCircle" 
                                           ViewBoxWidth="150" ViewBoxHeight="150" 
                                           TextColor="Green" BackgroundColor="Transparent"  
                                           HorizontalOptions="Center" VerticalOptions="Center" />
        </Grid>
    </ContentView.Content>

</ContentView>

基类

   namespace TEST_SF.Base
    {
      public class BaseClass
      {
          public static VolosLoading PageLoading = new VolosLoading { Mostra = false };
      }
    }

基本视图

 <?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:local="clr-namespace:TEST_SF"
             x:Class="TEST_SF.BasePage">
    <ContentPage.ControlTemplate>
        <ControlTemplate>
            <StackLayout>
                <Label BackgroundColor="Red" Text="Welcome to Xamarin.Forms!"
                    VerticalOptions="StartAndExpand" 
                    HorizontalOptions="CenterAndExpand" />
                <local:VolosLoading></local:VolosLoading>

                <ContentPresenter></ContentPresenter>

            </StackLayout>
        </ControlTemplate>

    </ContentPage.ControlTemplate>
    </ContentPage>

然后我有一个从基本View继承的视图,该视图带有一个按钮,该按钮调用执行该代码的命令:

PageLoading.Mostra = !PageLoading.Mostra;

课程是:

class MainPage_Repo : Base.BaseClass

其视图:

 <local:BasePage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:TEST_SF"
             x:Class="TEST_SF.MainPage">

        <ContentPage.BindingContext>
            <local:MainPage_Repo />
        </ContentPage.BindingContext>

            <StackLayout>
                <Button VerticalOptions="Center" HorizontalOptions="Center" Text="Loading SF" Command="{Binding MyMockCommand}" CommandParameter="1" />
            </StackLayout>
    </local:BasePage>

问题在于Grid在开始时是可见的,并且当按下按钮时没有任何变化,Mostra的值被正确更改,但他的Grid始终可见。 / p>

我该如何解决?

2 个答案:

答案 0 :(得分:1)

如果您在ContentView中具有静态属性,那么在与_LoadingContainer相关的情况下,由于它们在实例之间共享,因此可能会出现奇怪的问题

即使这不能解决您的问题,那也可能导致巨大的问题,因此不应该这样做。

答案 1 :(得分:0)

问题是当您打电话时:

_LoadingContainer = (Grid)FindByName("LoadingContainer");

此行正在创建Grid在XAML中定义的x:Name="LoadingContainer"容器的副本。然后,当Mostra属性更改时,您正在执行以下操作:

_LoadingContainer.IsEnabled = (bool)newValue;
_LoadingContainer.IsVisible = (bool)newValue;

以上内容是访问和更改Grid视图的副本的属性,而不是实际的LoadingContainer本身。

_LoadingContainer的所有实例替换为LoadingContainer,您的问题应得到解决。

此外,您可以删除与IsVisible的{​​{1}}和IsEnabled属性的绑定。这些将导致编译后的代码不必要的复杂性。

编辑

此外,在您的Grid处理程序中,您应该将其更改为以下内容:

MostraPropertyChanged
相关问题