簡體   English   中英

Silverlight 隔離存儲和 DevExpress 網格

[英]Silverlight Isolated Storage and DevExpress Grid

DevExpress 產品一直為持久的用戶偏好提供良好的支持。 我的 DevExpress 經驗從早期的 Delphi 版本到 present.Net 版本,我已經看到了將設置持久保存到 Windows 注冊表的選項,到 XML 等。

我現在使用的是 Silverlight DXGrid (2011 vol. 1),將用戶的自定義網格設置存儲在獨立存儲中似乎很自然,因此它保留在會話之間。 實現這一目標的最佳方法是什么? 有內置的方法嗎? 如果我必須自己做,是否至少有一個 object 代表我可以序列化的設置,還是我必須編寫自己的序列化方案?

我查看了GridControlTableView類的文檔,發現沒有內置方法可以實現這一點(如WriteSettingsToIsolatedStorage()方法)。

在 DevExpress 支持人員的幫助下,我發現這篇文章描述了如何做我想做的事。

我使用下面的擴展方法將其提升到了一個新的水平,它使用網格的名稱來保存和恢復其布局。 我可以撥打我想要的電話:

gridControl.SaveLayoutToIsolatedStorage();
gridControl.RestoreLayoutFromIsolatedStorage();

這是代碼:

public static class GridSettingsExtension
{
    private const string layoutFolderName = "dxGridLayout";
    private static readonly Func<GridControl, string> gridLayoutFile = g => g.Name + ".xml";

    public static bool IsLayoutSaved( this GridControl gridControl ) {
        var file = IsolatedStorageFile.GetUserStoreForApplication();
        var fullPath = Path.Combine( layoutFolderName, gridLayoutFile( gridControl ) );
        return file.FileExists( fullPath );
    }

    public static void SaveLayoutToIsolatedStorage( this GridControl gridControl ) {
        var file = IsolatedStorageFile.GetUserStoreForApplication();

        if( !file.DirectoryExists( layoutFolderName ) ) {
            file.CreateDirectory( layoutFolderName );
        }

        string fullPath = Path.Combine( layoutFolderName, gridLayoutFile( gridControl ) );
        using( var fs = file.CreateFile( fullPath ) ) {
            gridControl.SaveLayoutToStream( fs );
        }
    }

    public static void RestoreLayoutFromIsolatedStorage( this GridControl gridControl ) {
        var file = IsolatedStorageFile.GetUserStoreForApplication();
        var fullPath = Path.Combine( layoutFolderName, gridLayoutFile( gridControl ) );
        using( var fs = file.OpenFile( fullPath, FileMode.Open, FileAccess.Read ) ) {
            gridControl.RestoreLayoutFromStream( fs );
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM