2017-03-21 7 views
2

텍스트를 표시하는 사용자 정의보기를 만들었습니다.Xamarin DataBinding to bindableProperty (CustomView)

TestView.xaml

<?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="EodiRoad.TestView"> 
    <StackLayout> 

     <Label Text="{Binding Test}"/> 
     <Label Text="this is test view"/> 

    </StackLayout> 
</ContentView> 

에서 상기 숨김

TestView.xaml.cs

public partial class TestView : ContentView 
    { 


     public static BindableProperty TestProperty = 
      BindableProperty.Create(
       propertyName: "Test", 
       returnType: typeof(string), 
       declaringType: typeof(TestView), 
       defaultValue:"???" 
      ); 

     public string Test 
     { 
      get 
      { return (string)GetValue(TestProperty); } 
      set 
      { 
       Debug.WriteLine("test setted value = " + (string)value); 
       SetValue(TestProperty, value); 
      } 
     } 



     public TestView() 
     { 
      InitializeComponent(); 
      BindingContext = this; 
     } 
    } 

및 예약

I는이

<local:TestView Test="hjhkhjk"/> 
같은 다른 페이지는 사용할 때

it 잘 작동합니다. 내가 그것을

<local:TestView Test="{Binding post.uploader.username}"/> 

에 데이터를 바인딩 할 때 그것은 실 거예요 ... 아무것도 desplay

이 아니 값이 잘못 또는 일부 그런 종류의 post.uploader.username 문제.
<Label Text="{Binding post.uploader.username}"/> 

때문에 바로 그 고장 라인에서이 코드를 가지고 그냥

내가 잘못 여기서 뭐하는거야 .. 좋은 작품도? 문제를 해결하는 방법 ..?

+0

당신은 아마에'PropertyChanged' 기능을 구현해야 username 특성 문자열을 가지고 uploader 속성 개체가 post 속성 객체가 있어야합니다 귀하의 BindableObject 개체, UI가 귀하의 가치가 업데이트되었음을 ​​알리십시오 –

+0

괜찮습니다. 하지만 그 이유는 /// /// this 작동 및 /// uzu

+0

정적 값으로 설정하면 한 번만 설정되므로 그만큼입니다. 바인딩을 사용할 때 값은 기본값 (빈 값)을 먼저 가져오고 바인딩이 발생하면 값이 업데이트됩니다. 속성 변경 로직이 누락 되었기 때문에 UI에서 값이 업데이트되지 않습니다. –

답변

0

TestView 클래스의 BindableObject 속성 개체에서만 바인딩을 만들 수 있습니다. 예를 들어, 다음 코드를 작성한다면 :

<local:TestView Test="{Binding post.uploader.username}" /> 

+0

죄송합니다. 하지만 그건 내 질문에 대답하지 않는다. – uzu