加载Windows Phone 8中的ViewModel项目的数据



我正在尝试显示来自我的SQL Server Compact 3.5的数据。在我的Onnavigateto函数上,我已经说了代码,但我不确定为什么它无法加载它。我正在使用Pivot应用程序,是否可以使用它来显示我的数据?如果是,我做错了什么。在标题中=今天是我显示数据的地方。谢谢。以下是我的代码

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]

我不建议您直接设置ItemsSource属性,因为您已经在XAML中建立了Binding

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

由于Binding Path设置为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 s或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; }
}

我认为这可能是您两次绑定的问题在xaml

ItemsSource="{Binding Items}" 

和c#

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

看起来您需要删除XAML绑定

最新更新