0

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

自定义控件视图

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>

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

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始终可见。

我该如何解决这个问题?

4

2 回答 2

1

如果您在 中具有静态属性ContentView,则预计会发生奇怪的问题,因为它们在实例之间共享,在您的情况下与_LoadingContainer

即使这不能解决您的问题,也可能会导致巨大的问题并且不应该这样做。

于 2019-01-29T12:09:45.263 回答
0

问题是当你打电话时:

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

此行正在创建XAMLGridx:Name="LoadingContainer". 然后,当Mostra属性更改时,您正在执行以下操作:

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

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

用 替换所有实例_LoadingContainerLoadingContainer您的问题应该得到解决。

此外,您可以删除对 . 的IsVisibleIsEnabled属性的绑定Grid。这些将导致编译代码中不必要的复杂性。

编辑

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

private static void MostraPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
    var control = (VolosLoading)bindable;
    if (control != null)
    {
        control.LoadingContainer.IsEnabled = (bool)newValue;
        control.LoadingContainer.IsVisible = (bool)newValue;
    }
}
于 2019-01-29T09:34:50.953 回答