MS Access和VB具有表單和控件的標籤屬性,您可以在運行時存儲它們並保持與應用程序一致。 .NET中有什麼等價的東西?在運行時是否可以在Winform或控件中嵌入元數據?
1
A
回答
1
有Windows窗體中一個標籤屬性,但它不堅持。
我使用的模式是創建一個類調用預置(對每一條信息我要堅持屬性),然後用PreferencesManager類進行管理,像這樣的:
public class PreferencesManager
{
static private string dataPath = null;
static public string DataPath
{
get
{
if (dataPath == null)
{
string baseFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
dataPath = Path.Combine(baseFolder, @"MY_APP_NAME");
if (!Directory.Exists(dataPath))
{
Directory.CreateDirectory(dataPath);
}
}
return dataPath;
}
}
/// <summary>
/// Saves the specified preferences.
/// </summary>
/// <param name="pref">The preferences.</param>
static public void Save(Preferences pref)
{
// Create file to save the data to
string fn = "Preferences.xml";
string path = Path.Combine(DataPath, fn);
using (FileStream fs = new FileStream(path, FileMode.Create))
{
// Create an XmlSerializer object to perform the serialization
XmlSerializer xs = new XmlSerializer(typeof(Preferences));
// Use the XmlSerializer object to serialize the data to the file
xs.Serialize(fs, pref);
}
}
static public Preferences Load()
{
Preferences ret = null;
string path = string.Empty;
try
{
// Open file to read the data from
string fn = "Preferences.xml";
path = Path.Combine(DataPath, fn);
using (FileStream fs = new FileStream(path, FileMode.Open))
{
// Create an XmlSerializer object to perform the deserialization
XmlSerializer xs = new XmlSerializer(typeof(Preferences));
// Use the XmlSerializer object to deserialize the data from the file
ret = (Preferences)xs.Deserialize(fs);
}
}
catch (System.IO.DirectoryNotFoundException)
{
throw new Exception("Could not find the data directory '" + DataPath + "'");
}
catch (InvalidOperationException)
{
return new Preferences();
}
catch (System.IO.FileNotFoundException)
{
return new Preferences();
}
return ret;
}
}
3
.NET中的大多數UI控件都有一個.Tag
屬性,可用於同一目的。
但是,如果你需要額外的功能,可以使一類從基本控制(即你可以創建一個名爲SpecialPictureBox類,從繼承的PictureBox,並增加了額外的字段),並使用繼承它在你的Windows窗體中就像你可以使用PictureBox一樣。
+0
除非我m錯誤了,.Tag沒有被持續 – 2009-08-10 20:46:42
+0
。標籤也沒有在VB6中持久。 ;) – Brandon 2009-08-11 03:06:46
0
相關問題
- 1. 是否可以在運行時編輯嵌入資源文件?
- 2. 是否可以在運行時加載EF元數據?
- 3. Windows窗體:是否可以在運行時插入.cs文件?
- 4. 是否可以在運行時重新排列流控面板中的控件?
- 5. 在Web應用程序中是否可以嵌入數據庫?
- 6. JPA可嵌入是否可以包含Embeddable或Embeddables嵌入?
- 7. 是否可以在Google表單中加入或綁定數據?
- 8. JavaScript是否可以在UIWebView中運行?
- 9. 是否可以在iframe中運行setTimeout?
- 10. 如何在嵌入在WinForm中的WebBrowser控件中啓用JavaScript,該控件又嵌入在XBAP頁面中?
- 11. 是否可以在Excel中使用輸入數據運行硒測試?
- 12. Rails - 是否可以(或可能)在控制器內運行耙子?
- 13. 是否可以在C#中運行時創建/執行代碼?
- 14. 是否可以在FXG文件中嵌入圖像?
- 15. 是否可以在電子郵件通訊中嵌入視頻?
- 16. 是否可以在Flash/Flex中嵌入ActiveX組件?
- 17. 是否可以在程序集中嵌入ttinclude文件?
- 18. 是否可以在流浪者中嵌入同步文件夾?
- 19. 是否可以在C++應用程序中嵌入TTF或OTF字體文件?
- 20. 在將基元插入List時,是否可以獲得編譯或運行時錯誤?
- 21. 是否可以在運行時切換jar文件?
- 22. 是否可以在運行時提供新的PropertiesConfiguration文件?
- 23. 是否可以在運行時禁用條件編譯符號?
- 24. 是否可以在運行時有條件地使用XPManifest?
- 25. 是否可以將3D數據嵌入到XSL-FO中?
- 26. 是否可以在javascript執行中寫入mongodb控制檯?
- 27. ASP.NET中是否存在可在運行時渲染HTML的控件?
- 28. 是否可以在OneDrive文件夾中的SQLite數據庫上運行查詢?
- 29. 在winform上找到隱藏的控件,並使它們在運行時可見?
- 30. autohotkey是否可以在自己的腳本中寫入運行?
哇,非常感謝代碼片段:) – programmernovice 2009-08-10 21:12:28