2017-08-01 6 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フォームへようこそ!

テスト

私は空の文字列に入れた場合は、まったく出力されないが、アプリケーションは起動しますが、何が表示されます。

私がnullにTextValuePropertyのデフォルト値を設定した場合、出力は次のとおりです。

はXamarinフォームへようこそ!私がTextValueを設定すると、TextIsVisible値は第二の結合(テキスト)がFalseの出力にもかかわらず、第一の結合(のisVisible)で働いているが、なぜそれが虚偽であることが表示されます出力から

私は値を提供せず、nullが許容される空の値であるとは言わない場合、それは完全にねじ込みますが、それについては何も言いません。エラーは出力されず、何も出力されません。何かが間違っているのを見る方法はありますか? (iphoneシミュレータでのテスト)

そして、このコンセプトをこのテストの状況から取り出して、実際の状況に置いてください。 TextValueを設定してTextIsVisibleを出力することはまだ偽ですが、表示されません。

私は間違っていますか?私は何を理解していないのですか?

+2

TextValueの「setter」に 'TextIsVisible'の' NotificationChanged'を呼び出す必要があります。 –

+1

@JesusAnguloそれを詳しく教えてください。 –

+0

私の回答を更新しました –

答えて

2

あなたはこのプロパティがを変更されたことをビューに通知するTextIsVisibleためにPropertyChangedイベントを発生させる必要があります。

[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

それは素晴らしいです!リンクありがとう! –

関連する問題