2017-09-12 1 views
0

Xamarin을 처음 사용하며 현재 아래의 문제에 직면 해있는 솔루션에서 작업 중입니다. 클래스 A가 내 Model 클래스이고 클래스 B가 내 viewModel입니다. 모델 클래스Observablecollection 개체에 대해 Xamarin 명령이 작동하지 않습니다.

Class A : INotifyPropertyChanged 
{ 
    public string sampleprop { get; set; } 
    public event PropertyChangedEventHandler PropertyChanged; 
    public virtual void OnPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged == null) 
       return; 
     PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
} 

내가 메인 뷰 페이지 내 바인딩 컨텍스트로 클래스 B를 할당 클래스 B

Class B 
    { 
    public Command<string> CallCommand { get; set; } 
    public ObservableCollection<A> AobjectsCollection { get; set; } 

    public B() 
     { 
      AobjectsCollection = new ObservableCollection<A>(); 
      CallCommand = new Command<string>((string arg) => 
    DoMakeCall(arg)); 
     } 
    public void DoMakeCall(string phNumber) 
     { 
      string s = phNumber; 
     } 
} 

에서 관찰 할 수있는 컬렉션으로 클래스 A의 객체를 생성하고있다.

public partial class Mainview : ContentPage 
{ 
    InitializeComponent(); 
     BindingContext = new B(); 
    } 

기본보기 (xaml)에서 ClassB의 관찰 가능한 컬렉션 속성을 사용하여 목록보기를 만들고 있습니다. 이제 메인 페이지에서 버튼 (btnClick)를 클릭

 <ListView x:Name="MessagesListView" 
       ItemsSource="{Binding AobjectsCollection }" 
       HasUnevenRows="True" > 
       <ListView.ItemTemplate> 
        <DataTemplate> 
        <ViewCell > 
         <ViewCell.View> 
      <Button x:Name="btnClick" Text="ClickMe" 
        Command="{Binding CallCommand}" 
        CommandParameter="sampleprop"/> 
      </ViewCell.View> 
         </ViewCell> 
        </DataTemplate> 
       </ListView.ItemTemplate> 
     </ListView> 

내 뷰 모델의 명령을 호출하고 내 방법 DoMakeCall (문자열들)을 실행하지 않습니다.

아무도이 코드의 잘못된 점을 이해할 수 있도록 도와 주시겠습니까? 이 시나리오를 어떻게 달성 할 수 있습니까?

내 Command 속성은 VM 클래스가 아니라 Model 클래스에 있습니다. 내가 알아 내야 할 것은 컨텍스트를 올바르게 설정하여 올바르게 작동시키는 것입니다. 나는 릴레이 명령을 사용하고 싶지 않다.

+0

가능한 중복 https://stackoverflow.com/questions/31812035/relaycommand-parameter -passing-in-xamarin) – Unlockedluca

답변

0

현재 각 ViewCell을 A 모델에 바인딩하고 있습니다. B ViewModel에 대한 참조가 없습니다 (이 명령은 호출하려는 명령이있는 곳입니다.)이 문제를 해결하려면 ViewCell 단추에 MessagesListView의 BindingContext (이 경우에는 B ViewModel)에서 명령을 찾도록 지시해야합니다.

BindingContext="{Binding Source={x:Reference MessagesListView}, Path=BindingContext}" 

을 전체 맥락에서 : 즉,이 같을 것이다

<ListView x:Name="MessagesListView" 
      ItemsSource="{Binding AobjectsCollection }" 
      HasUnevenRows="True" > 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <ViewCell x:Name="viewCell"> 
         <ViewCell.View> 
          <Button x:Name="btnClick" Text="ClickMe" 
           BindingContext="{Binding Source={x:Reference MessagesListView}, Path=BindingContext}" 
           Command="{Binding CallCommand}" 
           CommandParameter="{Binding Source={x:Reference viewCell}, Path=BindingContext}"/> 
         </ViewCell.View> 
        </ViewCell> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
</ListView> 
[자 마린에서 RelayCommand의 매개 변수 전달 (의
+0

대단히 감사합니다.이 시나리오에서 작동했습니다. 그러나 Sampleprop 대신 List 를 classB, 어떻게 현재의 객체 (someotherclassobject)를 commandParameter로 설정할 수 있습니까? ModelView에 대한 Bindingcontext 버튼을 설정함에 따라 현재 채워진 listview의 현재 객체를 가져올 수 없습니다. CommandParameter = {Bindig.}로 시도했지만 효과가 없습니다. (도와 주시겠습니까? – Thavudu

+0

CommandAttameter를 셀의 원래 viewModel에 바인딩하도록 코드를 추가했는데, 이는 ClassA의 인스턴스가 될 것입니다. 다음 명령은 ViewCell에 바인딩 된 ClassA의 인스턴스를 허용합니다. –

+0

답장을 보내 주셔서 감사합니다 바인딩 ViewCell 작동하지 않습니다, 그것은 런타임 예외를 throw합니다. 나는 ViewCell 및 ViewCell.BindingContext 함께 시도했다 ...하지만 행운을 : '( – Thavudu

0

버튼 대신에 ItemSelected="Handle_ItemSelected을 사용하시기 바랍니다.

<ListView x:Name="MessagesListView" 
      ItemsSource="{Binding AobjectsCollection }" 
      HasUnevenRows="True" 
      ItemSelected="Handle_ItemSelected"> 

당신은 여기를 참조 할 수 있습니다 : Tap Gesture on List View Items

explaination은 매우 상세하다. 희망이 도움이됩니다.

관련 문제