我很难设置一种捕获用户名以显示在其他页面上的方法。此代码在azure简易表上进行身份验证并创建新用户。
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("CIS4910C-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 == textBoxUsername.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(home));
}
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 = textBoxUsername.Text,
Password = textBoxPassword.Text
};
await App.MobileService.GetTable<User_Cred>().InsertAsync(itemReg);
var dialog = new MessageDialog("Thank you for Registering! Now just hit log in");
await dialog.ShowAsync();
}
catch (Exception em)
{
var dialog = new MessageDialog("An Error Occured: " +em.Message);
await dialog.ShowAsync();
}
}
之前曾被问过,我只能对这段无效的代码进行几次修改。任何建议或编辑表示赞赏。这是在主页上,但我的下一页上的下一个代码块无法读取。
public string globalUserLabel;
public void SaveSetting(string userLabel, string userNamelabel)
{
globalUserLabel = userLabel;
ApplicationDataContainer localSettings =
ApplicationData.Current.LocalSettings;
//Saving your setting
localSettings.Values[userLabel] = textBoxUsername.Text;
}
下一页有
public home()
{
this.InitializeComponent();
UserNameLabelBox.Text = ReadSetting(MainPage.globalUserLabel );
}
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;
答案 0 :(得分:0)
您需要确保userLabel
的值在存储时与尝试检索时相同。通常建议为此使用const string
字段。更好的是,将访问设置的所有代码重构为一个单独的类。您可以为单个设置创建属性,并将其名称用作设置的键:
public class AppSettings
{
public string UserName
{
get => ReadSetting(null);
set => SaveSetting(value);
}
private string ReadSetting(string defaultValue,
[CallerMemberName]string settingKey = null)
{
var localSettings = ApplicationData.Current.LocalSettings;
if (localSettings.Values.ContainsKey(settingKey))
{
return (string)settings.Values[settingKey];
}
else
{
return defaultValue;
}
}
private string SaveSetting(string value,
[CallerMemberName]string settingKey = null)
{
ApplicationData.Current.LocalSettings.Values[settingKey] = value;
}
}
现在,您只需在代码中使用AppSettings
:
public void SaveSetting(string userLabel, string userNamelabel)
{
var appSettings = new AppSettings();
appSettings.UserName = textBoxUsername.Text;
}
private string ReadSetting(string userLabel)
{
var appSettings = new AppSettings();
return appSettings.UserName ?? "User not signed in";
}
为进一步解释代码-[CallerMemberName]
是一个特殊属性,在编译过程中会填写该属性,其中将包含方法调用者的名称,在这种情况下,它将是属性名称。
此外,AppSettings
现在是一项简单的服务,可以用作单例,您可以从中提取接口并将其插入需要使用IoC容器的任何地方。