首页 > 解决方案 > 基础视图的 Xamarin 绑定属性

问题描述

我有一个基本 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始终可见。

我该如何解决这个问题?

标签: c#xamlxamarinxamarin.formsbindable

解决方案


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

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


推荐阅读