2017-11-04 129 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; 
相關問題