因此,我正在为我的 Capstone 类(class)开发 UWP 移动应用。截至目前,我已编写代码来对 Azure SQL 数据库中保存的表中的用户进行身份验证。我需要的是将输入的用户名保存到可用于在应用程序的其他部分显示用户名的内容中,或者用于搜索数据库并仅提取与该用户名关联的内容。
我看过一些关于 session 类的博客等,但我似乎无法在这里使用它。任何其他帮助将不胜感激。请注意,我没有使用 SQL Server,我的 App.Xaml.cs 中有一个连接字符串。
public static MobileServiceClient MobileService = new
MobileServiceClient("http://tale-webapp.azurewebsites.net");
我当前的登录页面如下:
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();
this.Frame.Navigate(typeof(UserHome));
}
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: User Nname
Already exists.");
await dialog.ShowAsync();
}
}
}
}
尝试之后,我仍然不走运,代码似乎在某种程度上有效。
这是我在主页(即登录页面)中放置的内容
private void saveSetting(string userLabel, string userNamelabel)
{
Windows.Storage.ApplicationDataContainer localSettings =
Windows.Storage.ApplicationData.Current.LocalSettings;
//Saving your setting
localSettings.Values[userLabel] = UserNameEnter.Text;
}
当转到下一页时,这是我放置的代码。
private string readSetting(string userLabel)
{
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
//Reading and returning your setting value
var value = localSettings.Values[userLabel];
if (value != null)
return value.ToString();
else
return userLabel;
UserNameLabelBox.Text = readSetting(userLabel);
}
问题在于 UserNameLabelBox(文本框)有一条绿色波浪线,VS 表示检测到无法访问的代码。运行应用程序时,该框中没有任何显示。
现在我已经应用了给出的答案,但遇到了错误。在登录页面后加载的第二个页面中,当我添加 UserNameLabelBox.Text = readSetting(userNameLabel); 时,出现红线错误。红线出现在 userNameLabel 处,表明它在当前上下文中不存在。
回顾一下:在登录页面中,我有;
private void saveSetting(string userLabel, string userNameLabel)
{
Windows.Storage.ApplicationDataContainer localSettings =
Windows.Storage.ApplicationData.Current.LocalSettings;
//Saving your setting
localSettings.Values[userLabel] = UserNameEnter.Text;
}
登录成功后出现以下页面;
public sealed partial class UserHome : Page
{
public UserHome()
{
this.InitializeComponent();
UserNameLabelBox.Text = readSetting(userLabel);
}
这里就是发生错误的地方(上图)
private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(UserJournalPage));
}
private void aboutUs_Click(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(AboutUs));
}
private string readSetting(string userLabel )
{
Windows.Storage.ApplicationDataContainer localSettings =
Windows.Storage.ApplicationData.Current.LocalSettings;
//Reading and returning your setting value
var value = localSettings.Values[userLabel];
if (value != null)
return value.ToString();
else
return userLabel;
最佳答案
我确信您可以通过多种方式执行此操作,但最简单的方法之一是将用户名存储在应用程序设置 (Windows.Storage.ApplicationData.Current.LocalSettings) 中。
您可以使用这个例如:
Windows.Storage.ApplicationDataContainer localSettings =
Windows.Storage.ApplicationData.Current.LocalSettings;
//setting value
localSettings.Values["userName"] = "Username Value";
//Getting the setting value
string value = localSettings.Values["userName"].toString();
我还建议先加密用户名,然后再将其存储在应用程序设置中。
编辑
示例代码示例:
//Call this from your code with the setting name and the value that you want to store in it
private void saveSetting(string settingName,string settingValue){
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
//Saving your setting
localSettings.Values[settingName] = settingValue;
}
//Retrieve your setting value
private string readSetting(string settingName){
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
//Reading and returning your setting value
var value=localSettings.Values[settingName];
if(value!=null)
return value.toString();
else
return "";
}
因此,在特定用户登录成功后,您可以通过调用 saveSetting()
方法保存用户名,稍后在下一页中,您可以通过调用 readSetting( )
方法。
为了将值设置为 TextBlock,您需要编写如下内容:
TextBlockName.Text=readSetting("YourSettingName");
希望这有帮助。
编辑2: 看到你的代码后..我已经确定你出了什么问题..
UserNameLabelBox.Text
已分配,但从未更新。这是因为该函数返回存储在设置中的值,并且控件返回到调用方法。所以UserNameLabelBox .Text = readSetting(userLabel)
永远不会被调用。
The return statement terminates execution of the method in which it appears and returns control to the calling method
请仔细阅读此Return Statement Documentation了解更多。
2.您正在从方法内部调用方法本身..即 recursion并且您的代码将无限地调用方法 readSetting()
解决方案:
只需从 readSetting() 方法中删除 UserNameLabelBox.Text = readSetting(userLabel);
并将其放置在您的类构造函数中 -(例如 MainPage() 是 MainPage 的构造函数。 xaml.cs) .. 因此,一旦创建该类的新实例,您的文本 block 就会获得该值。
示例:
public YourPageThatComesAfterLogin()
{
this.InitializeComponent();
UserNameLabelBox.Text = readSetting(userLabel);
}
关于C# - UWP - 输入登录屏幕后保存用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45828707/