开发者

How to create an object reference to a xaml page from App.xaml.cs codebehind?

I have a Silverlight 4 Business Project where I have enabled the ASP.NET Authentication/Authorization role information. I would like to pass the currently authenticated user's account information from the app.xaml.cs codebehind to a different XAML page, but I have no idea how that is done, or if it's even possible.

My goal is to databind the IsEnabled property of various buttons of my target XAML page, based on whether the current user is in a particular admin related role or not. Th开发者_如何学Goe Application_UserLoaded event handler of app.xaml.cs seems to be the safest event handler to initiate this task because it fires only after the user's account information is loaded from the server.

I had previously attempted to retrieve the current user information directly from my target XAML page, but I was never getting the current user information because Application_UserLoaded hadn't finished loading the current user info yet.

public partial class App : Application
{
      private void Application_UserLoaded(LoadUserOperation operation)
      {
        // How do you create an object reference to a XAML page from your project solution
        // from this event handler?
      }
}

Thanks in advance for any assistance, John


You do not need a reference to your view to accomplish this task. There is a better solution. At first, create a class for the application-wide context with IsAdmin property, and add it to the App's resource dictionary

 private void Application_Startup(object sender, StartupEventArgs e)
 {
     this.Resources.Add("GlobalContext", new GlobalContext());
 }

Set IsAdmin property in your Application_UserLoaded handler, and bind to it from any of your views

<Button IsEnabled="{Binding IsAdmin, Source={StaticResource GlobalContext}}"

Cheers.


After some trial and error, I solved my original issue by raising a custom event handler in the Application_UserLoaded() handler of app.xaml.cs.

I then subscribed to this custom event handler from the codebehind of my specific XAML view page where I needed to databind the IsEnabled property of my various Silverlight controls.

The reason I had to create a custom event handler is because you cannot assume that the security role information will always be ready to reference in the Loaded() event handler of your XAML view page.

I found more information about this specific timing issue at: http://forums.silverlight.net/forums/p/146631/328217.aspx

cheers, John

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜