2016-11-18 2 views
0

에 바인딩이 작동하지 않습니다 우리는 사용자들이 그 설문 조사의 세부 정보 페이지로 이동합니다 측량을 선택 Surveys.If의 목록을 보여줍니다 ListView가 있습니다. 우리가 SelectedItem 특성에 바인딩 TwoWay를 사용, 그렇게, 컨트롤러에서의 selectedItem 속성에 세터를 추가합니다.자 마린 ListView에 양방향 우리의 자 마린 응용 프로그램에서 아이폰 OS

Xaml-

<ListView x:Name= "surveyList" ItemsSource= "{Binding Surveys}" SelectedItem= "{Binding SelectedSurvey, Mode=TwoWay}" BackgroundColor= "White" HorizontalOptions= "Fill" SeparatorColor= "Gray" RowHeight= "50" > 

C 번호 -이 Android에서 찾을 작동하지만 iOS에서 작동하지 않습니다

private SurveyListItem _selectedSurvey; 
public SurveyListItem SelectedSurvey 
{ 
    get { return _selectedSurvey; } 
    set 
    { 
     _selectedSurvey = value; 
     if (_selectedSurvey == null) 
     { 
      NotifyPropertyChanged(); 
      return; 
     } 
     OnSurveySelected(_selectedSurvey); 
     _selectedSurvey = null; 
     NotifyPropertyChanged(); 
    } 
} 

. 목록에서 항목을 탭하면 _selectedSurvey을 설정하거나 SelectedSurvey의 세터를 호출하지 않습니다.

나는이 문제를 일종의 탭 제스처로 변경할 수 있지만 다중 선택이 필요한 응용 프로그램의 다른 위치에서 ListView를 사용하고 있으며이 모든 것을 탭 제스처로 변경하면 고통이 될 것입니다.

Android 작동 왜 어떤 아이디어가 아니라 iOS?

편집 -

전체 Xaml-

<?xml version = "1.0" encoding= "utf-8" ?> 

< TabbedPage xmlns = "http://xamarin.com/schemas/2014/forms" 

      xmlns:x= "http://schemas.microsoft.com/winfx/2009/xaml" 

       x:Class= "MyApp.View.ModuleContentPage" 

      xmlns:vm= "clr-namespace:MyApp.ViewModel;assembly=MyApp" 

      xmlns:local= "clr-namespace:MyApp.View;assembly=MyApp" 

      Title= "Module Content Render" > 

    <TabbedPage.Children> 

    < ContentPage Title= "Summary" IsEnabled= "False" > 

     <ContentPage.Content> 

     < StackLayout Padding= "20, 20, 20, 0" > 

      < Label Text= "To Do: Render Module Content Here." ></ Label > 

     </ StackLayout > 

     </ ContentPage.Content > 

    </ ContentPage > 

    < ContentPage Title= "Related" > 

     <ContentPage.Content> 

     < StackLayout Padding= "20, 20, 20, 0" > 

      < Label Text= "To Do: Render Related Content Here." ></ Label > 

     </ StackLayout > 

     </ ContentPage.Content > 

    </ ContentPage > 

    < ContentPage Title= "Surveys" IsEnabled= "False" > 

     <ContentPage.Content> 

     < StackLayout Padding= "20" > 

      < ListView x:Name= "surveyList" ItemsSource= "{Binding Surveys}" SelectedItem= "{Binding SelectedSurvey, Mode=TwoWay}" BackgroundColor= "White" HorizontalOptions= "Fill" SeparatorColor= "Gray" RowHeight= "50" > 

      <ListView.Header> 

       < StackLayout Padding= "0, 0, 0, 10" VerticalOptions= "Center" > 

        < Label Text= "Surveys" FontSize= "20" TextColor= "Gray" LineBreakMode= "NoWrap" /> 

       </ StackLayout > 

      </ ListView.Header > 

      <ListView.ItemTemplate> 

       <DataTemplate> 

       <ViewCell> 

        <ViewCell.View> 

        < StackLayout VerticalOptions= "Center" > 

         < Grid ColumnSpacing= "20" > 

         <Grid.RowDefinitions> 

          < RowDefinition Height= "*" /> 

         </ Grid.RowDefinitions > 

         <Grid.ColumnDefinitions> 

          < ColumnDefinition Width= "*" /> 

          < ColumnDefinition Width= "50" /> 

         </ Grid.ColumnDefinitions > 

         < Label Text= "{Binding HydratedSurvey.Name}" FontSize= "12" TextColor= "Black" FontAttributes= "Bold" Grid.Row= "0" Grid.Column= "0" /> 

         < Label Text= "{Binding SurveyInstanceCount}" FontSize= "12" TextColor= "Green" FontAttributes= "Bold" Grid.Row= "0" Grid.Column= "1" /> 

         </ Grid > 

        </ StackLayout > 

        </ ViewCell.View > 

       </ ViewCell > 

       </ DataTemplate > 

      </ ListView.ItemTemplate > 

      </ ListView > 

     </ StackLayout > 

     </ ContentPage.Content > 

    </ ContentPage > 

    </ TabbedPage.Children > 

</ TabbedPage > 

코드 Behind-

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

using Xamarin.Forms; 

namespace MyApp.View 
{ 
    public partial class ModuleContentPage : TabbedPage 
    { 
     private Page PreviousPage { get; set; } 

     public ModuleContentPage() 
     { 
      InitializeComponent(); 

      // TODO hard coded to disabled the first two tabs and select Surveys. Remove when other tabs are finished 
      DisableTab(Children[0]); 
      DisableTab(Children[1]); 

      //Children[0].IsEnabled = false; 
      //Children[1].IsEnabled = false; 
      //PreviousPage = Children[2]; 
      //CurrentPage = PreviousPage; 
      CurrentPage = Children[2]; 

      CurrentPageChanged += ModuleContentPage_CurrentPageChanged; 
      PagesChanged += ModuleContentPage_PagesChanged; 
     } 

     private void DisableTab(Page page) 
     { 
      page.IsEnabled = false; 
      //page.Unfocus(); 
      page.Opacity = 50.0; 
     } 

     private void ModuleContentPage_PagesChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
     { 
      CurrentPage = Children[2]; 
     } 

     private void ModuleContentPage_CurrentPageChanged(object sender, EventArgs e) 
     { 
      CurrentPage = Children[2]; 
     } 
    } 
} 

**보기 모델 - **

using Newtonsoft.Json; 
using MyApp.DataModel.TransferObjects; 
using MyApp.DataAccess.UoW; 
using MyApp.Services; 
using MyApp.SQLiteAccess.Repository; 
using MyApp.SQLiteAccess.Tables; 
using MyApp.ViewModel; 
using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Input; 
using Xamarin.Forms; 

namespace MyApp.ViewModel 
{ 
    public class SurveyListItem : ViewModelBase 
    { 
     public SurveyDTO HydratedSurvey { get; set; } 
     private int _surveyInstanceCount; 
     public int SurveyInstanceCount { get { return _surveyInstanceCount; } set { _surveyInstanceCount = value; NotifyPropertyChanged(); } } 
    } 

    public class ModuleContentPageViewModel : ViewModelBase 
    { 
     private ModuleHydratedDTO _module; 
     public ModuleHydratedDTO Module { get { return _module; } set { _module = value; NotifyPropertyChanged(); } } 

     private ModuleContentHydratedDTO _moduleContent; 
     public ModuleContentHydratedDTO ModuleContent { get { return _moduleContent; } set { _moduleContent = value; NotifyPropertyChanged(); } } 

     SqlSurveyInstanceRepository _sqlSurveyInstanceRepository; 
     SqlSurveyInstanceRepository SqlSurveyInstanceRepository { get { return _sqlSurveyInstanceRepository ?? (_sqlSurveyInstanceRepository = new SqlSurveyInstanceRepository()); } } 

     private ObservableCollection<SurveyListItem> _surveys; 
     public ObservableCollection<SurveyListItem> Surveys { get { return _surveys; } set { _surveys = value; NotifyPropertyChanged(); } } 

     public ModuleContentPageViewModel(ModuleHydratedDTO module, ModuleContentHydratedDTO moduleContent) : base() 
     { 
      _module = module; 
      _moduleContent = moduleContent; 
      _surveys = GetSurveys(); 

      MessagingCenter.Subscribe<Stores.SurveyStore, SurveyDTO>(this, "UpdateSurveyInstanceCount", (sender, hydratedSurvey) => 
      { 
       SurveyListItem survey = _surveys.FirstOrDefault(s => s.HydratedSurvey.SurveyId == hydratedSurvey.SurveyId); 
       if (survey != null) 
       { 
        survey.SurveyInstanceCount = UoW.SurveyInstances.GetCountForSurveyIdAsync(hydratedSurvey.MasterSurveyId ?? hydratedSurvey.SurveyId, ModuleContent.ModuleContentId).Result; 
        NotifyPropertyChanged(); 
       } 
      }); 
     } 

     private ObservableCollection<SurveyListItem> GetSurveys() 
     { 
      ObservableCollection<SurveyListItem> surveyList = new ObservableCollection<SurveyListItem>(); 
      List<SurveyDTO> surveys = UoW.Surveys.GetHydratedSurveysForUser(_module.Module.ModuleId); 

      if (surveys.Count > 0) 
      { 
       foreach (var survey in surveys) 
       { 
        SurveyListItem item = new SurveyListItem(); 
        item.HydratedSurvey = survey; 
        item.SurveyInstanceCount = UoW.SurveyInstances.GetCountForSurveyIdAsync(survey.MasterSurveyId.HasValue ? survey.MasterSurveyId.Value : survey.SurveyId, ModuleContent.ModuleContentId).Result; 
        surveyList.Add(item); 
       } 
      } 
      return surveyList; 
     } 

     private SurveyListItem _selectedSurvey; 
     public SurveyListItem SelectedSurvey 
     { 
      get { return _selectedSurvey; } 
      set 
      { 
       _selectedSurvey = value; 
       if (_selectedSurvey == null) 
       { 
        NotifyPropertyChanged(); 
        return; 
       } 
       OnSurveySelected(_selectedSurvey); 
       _selectedSurvey = null; 
       NotifyPropertyChanged(); 
      } 
     } 

     private void OnSurveySelected(SurveyListItem selectedSurvey) 
     { 
      NavigationService.PushAsync(new SurveyInstanceListVM(selectedSurvey.HydratedSurvey, _moduleContent.ModuleContentId), selectedSurvey.HydratedSurvey.Name); 
     } 
    } 
} 
0 여기

편집 편집 -

을하는 데 도움이

희망은 frankenstiened 뷰 모델/viewmodelbase 조합입니다.

using MyApp.DataModel.TransferObjects; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 
using System.Runtime.CompilerServices; 
using MyApp.ViewModelFramework.Spooling; 

namespace MyApp.ViewModel 
{ 
    public class SurveyListItem 
    { 
     public string Name { get { return "foo"; } set { var x = value; } } 
     private int _surveyInstanceCount; 
     public int SurveyInstanceCount { get { return _surveyInstanceCount; } set { _surveyInstanceCount = value; } } 
    } 

    public class ModuleContentPageViewModel 
    { 
     private ObservableCollection<SurveyListItem> _surveys; 
     public ObservableCollection<SurveyListItem> Surveys { get { return _surveys; } set { _surveys = value; NotifyPropertyChanged(); } } 


     public ModuleContentPageViewModel(ModuleHydratedDTO module, ModuleContentHydratedDTO moduleContent) : base() 
     { 
      _surveys = GetSurveys(); 
     } 

     private ObservableCollection<SurveyListItem> GetSurveys() 
     { 
      ObservableCollection<SurveyListItem> surveyList = new ObservableCollection<SurveyListItem>(); 
      surveyList.Add(new SurveyListItem()); 
      return surveyList; 
     } 

     private SurveyListItem _selectedSurvey; 
     public SurveyListItem SelectedSurvey 
     { 
      get { return _selectedSurvey; } 
      set 
      { 
       _selectedSurvey = value; 
       if (_selectedSurvey == null) 
       { 
        NotifyPropertyChanged(); 
        return; 
       } 
       _selectedSurvey = null; 
       NotifyPropertyChanged(); 
      } 
     } 

     public void NotifyPropertyChanged([CallerMemberName] string propertyName = "") 
     { 
      InvokeNotifyPropertyChanged(propertyName); 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     public NotifyPropertyChangedSpooler NotificationSpooler = new NotifyPropertyChangedSpooler(); 

     private NotificationPropertyChangedStateEnum _notificationPropertyChangedState = NotificationPropertyChangedStateEnum.Active; 

     // Gets and sets change notification spooling and blocking features. 
     public NotificationPropertyChangedStateEnum NotificationPropertyChangedState 
     { 
      get { return _notificationPropertyChangedState; } 
      set 
      { 
       _notificationPropertyChangedState = value; 
       if (_notificationPropertyChangedState == NotificationPropertyChangedStateEnum.Active && !NotificationSpooler.IsEmpty) 
       { 
        NotificationSpooler.Unwind(); 
       } 
       NotifyPropertyChanged(); 

      } 
     } 


     void InvokeNotifyPropertyChanged(string propertyName) 
     { 
      switch (NotificationPropertyChangedState) 
      { 
       case NotificationPropertyChangedStateEnum.Active: 
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
        break; 
       case NotificationPropertyChangedStateEnum.Spooling: 
        NotificationSpooler.Enqueue(this, propertyName); 
        break; 
       case NotificationPropertyChangedStateEnum.Inactive: 
        break; 
      } 
     } 
    } 

    public enum NotificationPropertyChangedStateEnum 
    { 
     Inactive = 0, 
     Active = 1, 
     Spooling = 3 
    } 
} 
+0

어떻게 당신은 당신의 목록을 정의 할 수 있습니까? 단지 목록인가 아니면 ObservableCollection인가? 더 많은 코드와 xaml이 도움이 될 것입니다. 당신이 모델을 포함 뒤에 해당 페이지와 코드에 대한 전체 코드를 게시 할 수있는 이상적 경우 우리는이 ObservableCollection에있다 –

+0

더 빨리 도움이 될 수 있습니다. xaml, 코드 숨김 및 모델보기로 업데이트했습니다. –

+0

시도해 보겠습니다 ... 장치 또는 시뮬레이터에서 테스트 중이십니까? iOS 버전은 무엇입니까? –

답변

1

일부 조사 결과 xaml에서 페이지가 비활성화되었으며 xaml이나 코드 숨김에서 활성화 된 적이 없었습니다. 어떤 이유로 안드로이드에서 문제가되지 않았습니다 (이는 다소 잘못 되었기 때문에 더 자세히 조사해야합니다).

+0

감사합니다, 유리, 너무 많은 시간을 보면서이 문제를 해결하고 도움을 주셔서 감사합니다! - 내가이 사람에게 1+ 이상을 줄 수 있다면 : –

+0

@MicahWilliamson 내 기쁨 –