1

我有与Windows Azure/有关的问题Windows Phone 8 services

在我点击按钮后,使用 refresh_button_Tap 事件我收到了带有 MessageBox 的消息:

“System.NullReferenceException:对象引用未设置为对象的实例。在 pic.MainPage.d__0.MoveNext()”

然后我点击确定,新记录出现在Azure Database.

public class UsersTableItem
    {
        public int Id { get; set; }

        [DataMember(Name = "Login")]
        public string Login { get; set; }

        [DataMember(Name = "Password")]
        public string Password { get; set; }

    }

    public partial class MainPage : PhoneApplicationPage
    {
        // MobileServiceCollectionView implements ICollectionView (useful for databinding to lists) and 
        // is integrated with your Mobile Service to make it easy to bind your data to the ListView
        //private MobileServiceCollectionView<PicturesTableItem> items;

        private IMobileServiceTable<PicturesTableItem> picturesTable = App.MobileService.GetTable<PicturesTableItem>();

        private MobileServiceCollectionView<UsersTableItem> items;

        private IMobileServiceTable<UsersTableItem> usersTable = App.MobileService.GetTable<UsersTableItem>();

        // Constructor
        public MainPage()
        {
            InitializeComponent();

        }

        private async void InsertUsersTableItem(UsersTableItem UsersTableItem)
        {
            // This code inserts a new PicturesTableItem into the database. When the operation completes
            // and Mobile Services has assigned an Id, the item is added to the CollectionView
            try
            {

                await usersTable.InsertAsync(UsersTableItem);
                items.Add(UsersTableItem);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        private void refresh_button_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            var usersTableItem = new UsersTableItem {Login = "LOL", Password = "9E32581C5C0D680FFC7D95C370D3260B" };
            InsertUsersTableItem(usersTableItem);
        }
    }

Azure SQL

USE [pic_db]
GO

/**** Object:  Table [pic].[UsersTableItem]    Script Date: 2013-02-19 22:02:47 ****/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [pic].[UsersTableItem](
       [Id] [int] IDENTITY(1,1) NOT NULL,
       [Login] [nvarchar](max) NOT NULL,
       [Password] [nvarchar](max) NOT NULL,
CONSTRAINT [PK_UsersTableItem] PRIMARY KEY CLUSTERED
(
       [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)

GO

如何解决这个问题?

4

2 回答 2

1

从您的代码中,我看不到您正在初始化items集合,因此items.Add(UsersTableItem);似乎是罪魁祸首,并且它发生在 之后InsertAsync,因此您的数据确实可以很好地到达数据库。

于 2013-02-20T06:05:25.350 回答
0

首先,感谢吉姆·奥尼尔。

但还有更多。这是 Hyper-V 和我的 Wifi 的网络连接问题。我的 Hyper-V 模拟器延迟 1 分钟连接到互联网。首先应用程序刚刚运行,一段时间后同步时钟。但仍然没有任何联系。一分钟后连接真的可用。

由于:相关主题

今天我再次从我的 Azure 下载示例 TodoItems 项目。他们使用类似的东西来初始化项目集合。

private void RefreshTodoItems()
        {
            // This code refreshes the entries in the list view be querying the TodoItems table.
            // REMOVED the filter on completed items, so they're all dislpayed
            items = todoTable
                //.Where(todoItem => todoItem.Complete == false)
                .ToCollectionView();
            ListItems.ItemsSource = items;
        }

protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            RefreshTodoItems();
        }

如果模拟器无法连接,RefreshTodoItems 将无法正常工作。所以我的问题也出现在 Azure 的测试示例项目中。我不仅是遇到 Wifi / Hyper-V 模拟器问题的人。我应该购买 Windows Phone 8 设备,或者微软应该解决这个问题。

于 2013-02-20T11:22:11.387 回答