![](/img/trans.png)
[英]Object reference not set wpf issues with App.Current.Properties
[英]Xamarin/XAML/C# - How do I set up my App.Current.Properties dictionary?
问题:我正在编写一个计算假期休假天数的应用程序。 在应用程序启动时,系统会提示用户输入他们的总休假天数,并将其存储在名为 LeaveDays 的静态字符串中。 重新安装我的应用程序后,我意识到 LeaveDays 变量被重置。 好的,所以我需要正确保存这些数据。 我发现人们经常使用 Application.Current.Properties 选项,因此我没有将 LeaveDays 变量分配给提示条目的值,而是将其分配给 Application.Current.Properties["LeaveDays"] 键。
现在我的问题是我在任何地方都没有设置密钥,而且我收到了 KeyNotFoundException。 我认为它应该在主 App 文件中设置,但我不想用任何值实例化它。 该值需要来自提示。 我确定我只是误读了关于这个的文档,但我不确定应该如何设置属性字典以及它如何与手动输入一起工作。
下面是我的弹出窗口的代码。 任何帮助深表感谢:
async void SetLeaveDaysPopup()
{
VersionTracking.Track();
var firstLaunch = VersionTracking.IsFirstLaunchEver;
if (firstLaunch == true || ChangeLeaveDaysButton.IsEnabled == true)
{
Application.Current.Properties["LeaveDays"] = await DisplayPromptAsync("Set Leave Days", "Please set the amount of total leave days you have");
EntitlementDaysText.Text = Application.Current.Properties["LeaveDays"] as string;
calculateLeaveHours(EntitlementDaysText.Text);
}
}
在 Jason 的帮助下修复了代码:
async void SetLeaveDaysPopup()
{
VersionTracking.Track();
var firstLaunch = VersionTracking.IsFirstLaunchEver;
if (firstLaunch == true || ChangeLeaveDaysButton.IsEnabled == true)
{
Application.Current.Properties["LeaveDays"] = await DisplayPromptAsync("Set Leave Days", "Please set the amount of total leave days you have");
await Application.Current.SavePropertiesAsync();
if (Application.Current.Properties.ContainsKey("LeaveDays"))
{
EntitlementDaysText.Text = Application.Current.Properties["LeaveDays"] as string;
}
calculateLeaveHours(EntitlementDaysText.Text);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.