2017-11-04 157 views
0

在我的Xamarin.Forms应用程序中,我添加了一个.NET标准库,其中我想使用属性字典。什么是避免Application.Current.Properties的硬编码键的最佳方法?

但是,我想避免关键“id”的硬编码。

Application.Current.Properties ["id"] = someClass.ID; 

什么是最好的方式去呢?

+0

就是我的回答针对你的问题?我不确定,如果你问我提供了什么。 – Nikolaus

+0

是的,@尼古拉斯谢谢。 – John

+0

接受的答案将针对我的需求。 ;-) – Nikolaus

回答

1

有可能是没有最好的方式,但一个方法是使用nameof语法,因为如果重命名的属性,这将被改变,也:

// I added ToLowerInvariant, because you wrote "id". 
Application.Current.Properties[nameof(someClass.ID).ToLowerInvariant()] = someClass.ID; 
2

我想创建一个静态类此,沿

public static class Constants 
    { 
     public const string Id = "id"; 
     // etc... 
    } 

然后东西线更新代码才能

Application.Current.Properties [Constants.Id] = someClass.ID; 
相关问题