簡體   English   中英

基本視圖的Xamarin綁定屬性

[英]Xamarin binding property of base view

我有一個基本的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始終可見。

我該如何解決?

如果您在ContentView具有靜態屬性,那么在與_LoadingContainer有關的情況下,由於它們在實例之間共享,因此可能會出現奇怪的問題

即使這不能解決您的問題,那也可能會導致巨大的問題,因此不應該這樣做。

問題是當您打電話時:

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

該行正在創建XAML中x:Name="LoadingContainer"定義的Grid容器的副本 然后,當Mostra屬性更改時,您將執行以下操作:

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

上面是訪問和更改Grid視圖副本的屬性,而不是實際的LoadingContainer本身。

替換的所有實例_LoadingContainerLoadingContainer ,你的問題應該得到解決。

另外,您可以刪除對GridIsVisibleIsEnabled屬性的綁定。 這些將導致編譯后的代碼不必要的復雜性。

編輯

此外,在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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM