2014-01-27 71 views
0

我試圖從我的SQL Server Compact 3.5中顯示數據。在我的OnNavigateTo函數中,我已經說明了這些代碼,但我不確定它爲什麼無法加載它。我正在使用Pivot App,是否可以使用它顯示我的數據?如果是的話,我做錯了什麼。在標題=今天是我顯示我的數據的地方。謝謝。 下面是我的代碼加載Windows Phone 8中ViewModel項目的數據

MainPage.xaml中

<!--Pivot Control--> 
    <phone:Pivot Title="DAILY ROUTINE"> 
     <!--Pivot item one--> 
     <phone:PivotItem Header="activity"> 
      <!--Double line list with text wrapping--> 
      <phone:LongListSelector x:Name="MLongListSelector" Margin="0,0,-12,0" ItemsSource="{Binding Items}" SelectionChanged="LongListSelector_SelectionChanged"> 
       <phone:LongListSelector.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Margin="0,0,0,17"> 
          <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/> 
         </StackPanel> 
        </DataTemplate> 
       </phone:LongListSelector.ItemTemplate> 
      </phone:LongListSelector> 
     </phone:PivotItem> 

     <!--Pivot item two--> 
     <phone:PivotItem Header="today"> 
      <!--Double line list with text wrapping--> 
      <phone:LongListSelector x:Name="MainLongListSelector" Margin="0,0,-12,0" ItemsSource="{Binding Items}" SelectionChanged="LongListSelector_SelectionChanged"> 
       <phone:LongListSelector.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Margin="0,0,0,17"> 
          <TextBlock Text="{Binding Id}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/> 
          <TextBlock Text="{Binding Title}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/> 
         </StackPanel> 
        </DataTemplate> 
       </phone:LongListSelector.ItemTemplate> 
      </phone:LongListSelector> 

     </phone:PivotItem> 
    </phone:Pivot> 

MainPage.xaml.cs中

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Navigation; 
using Microsoft.Phone.Controls; 
using Microsoft.Phone.Shell; 
using MyPhoneApp1.Resources; 

namespace MyPhoneApp1 
{ 
public partial class MainPage : PhoneApplicationPage 
{ 
    // Constructor 
    public MainPage() 
    { 
     InitializeComponent(); 

     // Set the data context of the listbox control to the sample data 
     DataContext = App.ViewModel; 

     // Sample code to localize the ApplicationBar 
     //BuildLocalizedApplicationBar(); 
    } 

    // Load data for the ViewModel Items 
    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     /* 
     if (!App.ViewModel.IsDataLoaded) 
     { 
      App.ViewModel.LoadData(); 
     } 
     * */ 


     using (ToDoListContext c = new ToDoListContext(ToDoListContext.ConnectionString)) 
     { 
      c.CreateIfNotExists(); 
      c.LogDebug = true; 
      MainLongListSelector.ItemsSource = c.ToDoLists.ToList(); 
     } 

    } 
    private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     var si = MLongListSelector.SelectedItem as MyPhoneApp1.ViewModels.ItemViewModel; 

     if (MLongListSelector.SelectedItem == null) 
      return; 

     if (si.LineOne.Equals("+ To Do List")) 
      NavigationService.Navigate(new Uri("/todolistPage.xaml", UriKind.Relative)); 
     else if (si.LineOne.Equals("+ Reminder")) 
      NavigationService.Navigate(new Uri("/reminderPage.xaml", UriKind.Relative)); 

     // Reset selected item to null (no selection) 
     MLongListSelector.SelectedItem = null; 
    } 


} 

} 

我已經調試它,下面是SQL語句

SELECT [t0].[Id], [t0].[Title] 
FROM [ToDoLists] AS [t0] 
+0

確實Ç .DoDoLists包含數據?你有斷點檢查嗎 – SWilko

+0

是的,它有3個數據。通過檢查斷點,你的意思是什麼?我該怎麼做 –

+0

通過點擊左邊距(添加紅點)在代碼中添加一個斷點,然後運行應用程序,代碼將中斷,以便您可以檢查變量,即c.ToDoLists – SWilko

回答

1

我不會建議你已經建立你直接設置ItemsSource屬性a在XAML中的Binding

<phone:LongListSelector x:Name="MLongListSelector" 
    ItemsSource="{Binding Items}" > 

由於BindingPath設置爲Items,更改存儲在Items中的列表的數據將導致UI自動更新。

// get the list ... 
var list = c.ToDoLists.ToList(); 
Debug.Assert(list != null); 
// clear any existing items, which will in turn remove all items from the UI 
App.ViewModel.Items.Clear(); 
// for each item in the list, add it to the existing bound Items list 
foreach(var item in list) { 
    // you may need to transform the data here 
    // The item must be the right type ... 
    App.ViewModel.Items.Add(item); 
} 

因爲它看起來像您使用的WP8模板,該ToDoLists屬性需要返回ItemViewModel秒的枚舉列表或調用Add將失敗。你可以創建一個ItemViewModel的新實例,如果類型不(例如)匹配:

var itemViewModel = new ItemViewModel() { 
    LineOne = item.Text, 
    LineTwo = item.Description 
}; 
App.ViewModel.Items.Add(itemViewModel); 

上面的代碼假定然後是一個待辦事項列表項可能是這樣的:

public class TodoItem { 
    public string Text { get; set; } 
    public string Description { get; set; } 
} 
+0

owh!你是對的。因爲我使用了內置的templatePivot App。我注意到它與Databound App不同。非常感謝!!它現在有效! –

0

我想這可能是您綁定MainLongListSelector兩次的問題 在XAML

ItemsSource="{Binding Items}" 

,並在C#

MainLongListSelector.ItemsSource = c.ToDoLists.ToList(); 

看起來你需要刪除的XAML綁定

+0

嗨@dellywhell,我已經在xaml中刪除它,並保持xaml.cs,但它仍然沒有工作 –

+0

嗨,我已更新我的問題,我調試時添加SQL語句。 –

相關問題