输入用户名和密码时,UWP崩溃

时间:2017-08-17 01:33:27

标签: c# azure uwp

这是我的第一篇文章。我会尝试获取所有相关信息。我正在为我的BS Capstone课程开发一个UWP。我有一个主页,让用户可以选择输入用户名和密码并登录,或输入用户名和密码并注册,以便他们能够登录。我在那里有一些例外情况用户未注册,对话框将通知他们需要注册。 问题是当我输入我知道未注册的用户名和密码时,应用程序崩溃了。 我正在使用VS 2015和Azure SQL数据库。我必须知道的唯一线索是输出显示以下消息。 抛出异常:mscorlib.ni.dll中的“System.Net.Http.HttpRequestException”

另外,我在App.Xaml.cs选项卡中有连接字符串

 public static MobileServiceClient MobileService = new 
 MobileServiceClient("http://tale-server1.database.windows.net");

最后,在尝试注册新用户时,Catch异常会触发:

catch (Exception em)
        {
            var dialog = new MessageDialog("An Error Occured: " + 
em.Message);
            await dialog.ShowAsync();
        }

我的主页代码如下。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Microsoft.WindowsAzure.MobileServices;
using System.Threading.Tasks;
using Windows.UI.Popups;
using Windows.Storage;
using System.Net.Http;
using Newtonsoft.Json;
using SQLite;
using SQLite.Net;
using SQLite.Net.Async;
using Microsoft.WindowsAzure.MobileServices.Sync;
using Microsoft.WindowsAzure.MobileServices.SQLiteStore;
using SQLitePCL;


// The Blank Page item template is documented at 
http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace TALE_Capstone
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a 
Frame.
/// </summary>
public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    public int IsAuth { get; set; }

    //[DataTable("User_Cred")]
    public class User_Cred
    {
        public string id { get; set; }
        public string userName { get; set; }
        public string Password { get; set; }

    }

    private IMobileServiceSyncTable<User_Cred> todoGetTable = 
App.MobileService.GetSyncTable<User_Cred>();



    private async Task InitLocalStoreAsync()
    {
        if (!App.MobileService.SyncContext.IsInitialized)
        {
            var store = new MobileServiceSQLiteStore("Tale-DB");
            store.DefineTable<User_Cred>();
            await App.MobileService.SyncContext.InitializeAsync(store);
        }
        await SyncAsync();
    }

    private async Task SyncAsync()
    {
        await App.MobileService.SyncContext.PushAsync();
        await todoGetTable.PullAsync("User_Cred", 
todoGetTable.CreateQuery());
    }



    async public void submitAuthBtn_Click(object sender, RoutedEventArgs e)
    {
        await InitLocalStoreAsync();

        GetAuthentication();

    }

    async public void GetAuthentication()
    {
        try
        {

            //IMobileServiceTable<User_Cred> todoTable = App.MobileService.GetTable<User_Cred>();

            List<User_Cred> items = await todoGetTable
                .Where(User_Cred => User_Cred.userName == UserNameEnter.Text) 
                .ToListAsync();

            IsAuth = items.Count();

            // Return a List UI control value back to the form

            foreach (var value in items)
            {
                var dialog = new MessageDialog("Welcome Back  " + value.userName);
                await dialog.ShowAsync();
            }


            if (IsAuth > 0)
            {
                var dialog = new MessageDialog("You are Authenticated");
                await dialog.ShowAsync();

            }
            else
            {
                var dialog = new MessageDialog(" Account Does Not Exist, please Register to get Started.");
                await dialog.ShowAsync();
            }
        }
        catch (Exception em)
        {
            var dialog = new MessageDialog("An Error Occured: " + em.Message);
            await dialog.ShowAsync();
        }
    }

    async private void submitAuthBtn_Copy_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            User_Cred itemReg = new User_Cred
            {
                userName = UserNameEnter.Text,
                Password = PWEnter.Text
            };
            await App.MobileService.GetTable<User_Cred>().InsertAsync(itemReg);
            var dialog = new MessageDialog("Thank you for Registering! Lets begin");
            await dialog.ShowAsync();
        }
        catch (Exception em)
        {
            var dialog = new MessageDialog("An Error Occured: " + em.Message);
            await dialog.ShowAsync();
        }
    }



}
}

任何帮助将不胜感激!

康拉德

1 个答案:

答案 0 :(得分:0)

根据您的描述,我假设您将UWP应用程序与Azure移动应用程序后端和Azure SQL数据库连接以存储数据。 AFAIK,DropDownListFor的初始化如下所示:

MobileServiceClient

我建议您按照tutorial进行操作,然后下载sample client project来检查此问题。此外,当您将uwp应用程序与azure移动应用程序连接以获取详细的错误消息时,您可以利用fiddler并收集网络跟踪,然后您可以缩小此问题,或者您可以使用我们的详细错误更新您的问题找到这个问题。