2017-08-01 68 views
1

我正在努力弄清楚我认为应该是非常基本的东西,我希望有人能帮助我。Xamarin依赖绑定

我试图根据数据是否提供给它来使自定义控件可见。

例如: 我有一个使用自定义控制如下主要页面:

<?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:controls="clr-namespace:XamarinDependantProperty.Controls;assembly=XamarinDependantProperty" 
      x:Class="XamarinDependantProperty.Pages.MainPage"> 
    <ContentPage.Padding> 
     <OnPlatform x:TypeArguments="Thickness"> 
      <On Platform="iOS" Value="0,20,0,0"/> 
     </OnPlatform> 
    </ContentPage.Padding> 
    <StackLayout> 
     <Label Text="Welcome to Xamarin Forms!" VerticalOptions="Center" HorizontalOptions="Center" /> 
     <controls:CustomEntry TextValue="Test" VerticalOptions="Center" HorizontalOptions="Center"></controls:CustomEntry> 
    </StackLayout> 
</ContentPage> 

自定义控件如下所示:

<?xml version="1.0" encoding="UTF-8"?> 
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      x:Class="XamarinDependantProperty.Controls.CustomEntry" IsVisible="{Binding TextIsVisible}"> 
    <StackLayout> 
     <Label Text="{Binding TextIsVisible}" /> 
     <Label Text="{Binding TextValue}" /> 
    </StackLayout> 
</ContentView> 

而后面的代码如下:

using System; 
using Xamarin.Forms; 
using Xamarin.Forms.Xaml; 

namespace XamarinDependantProperty.Controls 
{ 
    [XamlCompilation(XamlCompilationOptions.Compile)] 
    public partial class CustomEntry : ContentView 
    { 
     public CustomEntry() 
     { 
      InitializeComponent(); 

      BindingContext = this; 
     } 

     public string TextValue 
     { 
      get { return (string)GetValue(TextValueProperty); } 
      set { SetValue(TextValueProperty, value); } 
     } 

     public static BindableProperty TextValueProperty = BindableProperty.Create(nameof(TextValue), typeof(string), typeof(CustomEntry)); 

     public bool TextIsVisible => !String.IsNullOrEmpty(TextValue); 
    } 
} 

如果我将CustomEntry的TextValue设置为“Test”,则输出是:

欢迎来到Xamarin Forms!

测试

如果我把一个空字符串,那么就没有输出,应用程序启动,但不显示任何内容。

如果我设置了TextValueProperty的默认值设置为null,则输出为:

欢迎Xamarin形式!

从看来,当我设置TextValue,该TextIsVisible值在第一结合(ISVISIBLE)的工作,即使第二个结合(文本)输出假的,但为什么是假的产出?

如果我没有提供一个值,但我没有告诉它null是一个可接受的空值,那么它就完全搞砸了,但它并没有对此提出任何意见。没有错误,没有输出,什么都没有。有没有办法看到出现问题? (在iPhone模拟器上测试)

然后,如果我将这个概念从这种测试情境中解放出来并将其应用到实际情况中。然后设置TextValue和输出TextIsVisible仍然是错误的,但它不显示。

我在做什么错?我不了解什么?

+2

您需要在TextValue'setter'上为'TextIsVisible'提升'NotificationChanged'。 –

+1

@JesusAngulo您能详细解释一下吗? –

+0

我已经更新了我的答案 –

回答

2

您需要提高的PropertyChanged事件TextIsVisible通知认为这属性已更改

[XamlCompilation(XamlCompilationOptions.Compile)] 
public partial class CustomEntry : ContentView 
{ 
    public CustomEntry() 
    { 
     InitializeComponent(); 

     BindingContext = this; 
    } 

    public string TextValue 
    { 
     get { return (string)GetValue(TextValueProperty); } 
     set{SetValue(TextValueProperty, value);OnPropertyChanged(nameof(TextIsVisible));} 
    } 

    public static BindableProperty TextValueProperty = BindableProperty.Create(nameof(TextValue), typeof(string), typeof(CustomEntry),propertyChanged:OnTextChanged); 

    private static void OnTextChanged(BindableObject bindable, object oldvalue, object newvalue) 
    { 
     var entry = bindable as CustomEntry; 
     entry?.OnPropertyChanged(nameof(TextIsVisible)); 
    } 

    public bool TextIsVisible => !String.IsNullOrEmpty(TextValue); 
} 
+1

OnTextChanged部分是必要的,这是一个耻辱。 我觉得必须有一个更清洁的解决方案,但这个工程,所以谢谢。 –

+0

自动检测依赖链并不是很简单,但也许你可以看看Fody https:// github。com/Fody/PropertyChanged –

+1

太棒了!感谢您的链接! –