2011-08-01 77 views
1

我已經能夠找到許多關於在WPF中使用ASP .NET成員資格提供程序的內容。我甚至有我的WPF應用程序中工作的ASP .NET角色提供程序。這些在WPF中非常容易使用。我找不到任何有關如何在WPF應用程序中使用ASP .NET PROFILE提供程序的信息。在WPF應用程序中使用ASP .NET PROFILE提供程序

我的應用程序將運行在位於移動車輛的筆記本電腦上;它不能使用Web服務來連接到服務器並對用戶進行身份驗證。我的代碼需要能夠從本地數據庫中檢索用戶的配置文件信息。

我已經嘗試了幾次來建立一個派生自ProfileBase的類,但沒有任何我嘗試過的。爲了詳細說明,下面的代碼是從我的最新嘗試失敗的摘錄:

public class UserProfile : ProfileBase { 

    [CustomProviderData("AlarmsToDisplay;int")] 
    public int AlarmsToDisplay { 
     get { return (int) GetPropertyValue("AlarmsToDisplay"); } 
     set { SetPropertyValue("AlarmsToDisplay", value); } 
    } 

    // Other properties following the same pattern follow. 

    private UserProfile() { } 

    public static UserProfile GetUserProfile(string username) { 
     return Create(username) as UserProfile; 
    } 

    public static UserProfile GetUserProfile(string username, bool isAuthenticated) { 
     return Create(username, isAuthenticated) as UserProfile; 
    } 
} 

在這段代碼,方法GetUserProfile(用戶名字符串,布爾isAuthenticated)返回null。我相信這是因爲編譯器無法將ProfileBase轉換爲UserProfile,即使UserProfile從ProfileBase中下載。如果我錯了,請糾正我。

有人能指向我現有的文章或描述正確的方式來建立一個從ProfileBase擴展的用戶配置文件類嗎?

回答

1

我設法自己解決這個問題。這是什麼工作:

  1. 從所有的CLR屬性中刪除屬性。這些不適用於默認的aspnet_Profiles表。
  2. 在app.Config中,您需要爲標籤添加一個「繼承」屬性。這必須設置爲您的類的完全限定名稱,用逗號將其與程序集文件名稱(不包含擴展名)分開。

完成這些事情後,GetUserProfile方法和ProfileBase類的Create方法返回UserProfile對象。