2017-04-21 4 views
0

初めて投稿するので、間違っている場合は、書式を申し訳ありません。フィルターObservableCollection日付に基づいてXamarinフォーム

与えられた日付に基づいて観察可能なコレクションをフィルタリングするのに問題があります。アプリケーションには、ユーザーが日付をクリックできるカレンダーがあり、その日付の予定が表示されます。

Azureからデータを取得するdataManagerと、予定ページ自体の2つのクラスがあります。ここで

がappointmentPageクラスです:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Input; 
using Xamarin.Forms; 
using Xamarin.Forms.Xaml; 
using XamForms.Controls; 

namespace TodoAzure 
{ 
    [XamlCompilation(XamlCompilationOptions.Compile)] 
    public partial class AppointmentPage : ContentPage 
    { 
     TodoItemManager manager; 
     CalendarVM vm = new CalendarVM(); 
     public AppointmentPage() 
     { 
      InitializeComponent(); 
      manager = TodoItemManager.DefaultManager; 
      calendar.DateClicked += (sender, e) => 
      { 
       System.Diagnostics.Debug.WriteLine(calendar.SelectedDates); 
      }; 
      calendar.SetBinding(Calendar.DateCommandProperty, nameof(vm.DateChosen)); 
      calendar.SetBinding(Calendar.SelectedDateProperty, nameof(vm.DateSelected)); 
      calendar.BindingContext = vm; 
     } 
     protected override async void OnAppearing() 
     { 
      base.OnAppearing(); 
      // Set syncItems to true in order to synchronize the data on startup when running in offline mode 
      await RefreshItems(true, syncItems: false); 
     } 
     //PULL TO REFRESH 
     public async void OnRefresh(object sender, EventArgs e) 
     { 
      var list = (ListView)sender; 
      Exception error = null; 
      try 
      { 
       await RefreshItems(false, true); 
      } 
      catch (Exception ex) 
      { 
       error = ex; 
      } 
      finally 
      { 
       list.EndRefresh(); 
      } 
      if (error != null) 
      { 
       await DisplayAlert("Refresh Error", "Couldn't refresh data (" + error.Message + ")", "OK"); 
      } 
     } 
     public async void OnSyncItems(object sender, EventArgs e) 
     { 
      await RefreshItems(true, true); 
     } 
     private async Task RefreshItems(bool showActivityIndicator, bool syncItems) 
     { 
      using (var scope = new ActivityIndicatorScope(syncIndicator, showActivityIndicator)) 
     { 
      appointmentPage.ItemsSource = await manager.GetAppointmentItemsAsync(syncItems);  
     } 
    } 
    private class ActivityIndicatorScope : IDisposable 
    { 
     private bool showIndicator; 
     private ActivityIndicator indicator; 
     private Task indicatorDelay; 
     public ActivityIndicatorScope(ActivityIndicator indicator, bool showIndicator) 
     { 
      this.indicator = indicator; 
      this.showIndicator = showIndicator; 
      if (showIndicator) 
      { 
       indicatorDelay = Task.Delay(2000); 
       SetIndicatorActivity(true); 
      } 
      else 
      { 
       indicatorDelay = Task.FromResult(0); 
      } 
     } 
     private void SetIndicatorActivity(bool isActive) 
     { 
      this.indicator.IsVisible = isActive; 
      this.indicator.IsRunning = isActive; 
     } 
     public void Dispose() 
     { 
      if (showIndicator) 
      { 
       indicatorDelay.ContinueWith(t => SetIndicatorActivity(false), TaskScheduler.FromCurrentSynchronizationContext()); 
      } 
     } 
    } 
} 

そしてここでは、データマネージャクラスです:

using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Diagnostics; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Microsoft.WindowsAzure.MobileServices; 
using Microsoft.WindowsAzure.MobileServices.Sync; 
#if OFFLINE_SYNC_ENABLED 
using Microsoft.WindowsAzure.MobileServices.SQLiteStore; 
using Microsoft.WindowsAzure.MobileServices.Sync; 
#endif 

namespace TodoAzure 
{ 
    public partial class TodoItemManager 
    { 
     static TodoItemManager defaultInstance = new TodoItemManager(); 
     MobileServiceClient client; 
     IMobileServiceTable<TodoItem> todoTable; 
     IMobileServiceTable<AppointmentItem> appointmentTable; 
     private TodoItemManager() 
     { 
      this.client = new MobileServiceClient (
       Constants.ApplicationURL); 
      this.todoTable = client.GetTable<TodoItem>(); 
      this.appointmentTable = client.GetTable<AppointmentItem>(); 
     } 
     public static TodoItemManager DefaultManager 
     { 
      get { return defaultInstance; } 
      private set { defaultInstance = value; } 
     } 
     public MobileServiceClient CurrentClient 
     { 
      get { return client; } 
     } 
     public bool IsOfflineEnabled 
     { 
      get { return appointmentTable is Microsoft.WindowsAzure.MobileServices.Sync.IMobileServiceSyncTable<AppointmentItem>;  } 
    } 
    // INSERT AND UPDATE METHODS 
    public async Task SaveTaskAsync (TodoItem item) 
    { 
     if (item.Id == null) 
      await todoTable.InsertAsync (item); 
     else 
      await todoTable.UpdateAsync (item); 
    } 
    public async Task SaveTaskAsync(AppointmentItem appointment) 
    { 
     if (appointment.Id == null) 
      await appointmentTable.InsertAsync(appointment); 
     else 
      await appointmentTable.UpdateAsync(appointment); 
    } 
    public async Task<ObservableCollection<AppointmentItem>> GetAppointmentItemsAsync(bool syncItems = false) 
    { 
     try 
     { 
      IEnumerable<AppointmentItem> items = await appointmentTable 
        .ToEnumerableAsync(); 
      return new ObservableCollection<AppointmentItem>(items); 
     } 
     catch (MobileServiceInvalidOperationException msioe) 
     { 
      Debug.WriteLine(@"Invalid sync operation: {0}", msioe.Message); 
     } 
     catch (Exception e) 
     { 
      Debug.WriteLine(@"Sync error: {0}", e.Message); 
     } 
     return null; 
    } 
} 

すべてのヘルプは非常に高く評価されます。日付でのIEnumerableをフィルタリングする

+0

を試してみてください - あなたはより具体的な可能性を?エラーや例外が発生していますか?それは奇妙に動作しているのか、クラッシュしていますか?私はあなたのコードに日付をフィルタリングしようとするいかなる論理も見ませんか? – Jason

+0

ObservableCollectionをアポイントメントページの 'vm.SelectedDates'で与えられた日付でフィルタリングする方法がわかりません。私は他のスタックオーバーフローの質問で与えられたメソッドを試してみましたが、成功しませんでした。 –

答えて

0

、「困った」この

// items is ObservableCollection<AppointmentItem> 
var filtered = items.Where(x => x.Date == SelectedDate); 
+0

これは機能しませんでした):これは、ユーザーがSelectedDateを定義する場所であるため、AppointmentPageクラスでフィルタリングを実行する必要がありますか? –

+0

私の編集を参照してください - 私は私の例ではインスタンス名の代わりにクラス名を持っていました。それでも動作しない場合は、より具体的にしてください - デバッガで、フィルタリングされたデータリストが返されていることを確認しましたか?フィルタ式をどこに置くかは、あなた次第です。 – Jason

+0

私は今このエラーを表示しています " エラーCS1061 \t 'タスク>'に 'Where'と拡張メソッドの定義が含まれていません 'Where' 'タイプの最初の引数を受け入れる' Task > 'が見つかりました(使用するディレクティブまたはアセンブリ参照がありませんか?) "ゆっくりと、しかし確かにそこに着いた。 –

関連する問題