首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >InitializeComponent()在将WebContext添加到ApplicationLifetimeObjects或等待App.Current.Startup事件之前?

InitializeComponent()在将WebContext添加到ApplicationLifetimeObjects或等待App.Current.Startup事件之前?
EN

Stack Overflow用户
提问于 2011-09-21 14:13:59
回答 1查看 781关注 0票数 2

我有一个使用MVVM方法架构的Silverlight应用程序。在我的中,是时候在用户是LoggedIn之后才加载一些数据了,所以我需要拦截这个事件来触发ViewModel ()。在默认配置中,我的意思是在App.xaml.cs中:

代码语言:javascript
复制
public App()
    {
        InitializeComponent();

        // Create a WebContext and add it to the ApplicationLifetimeObjects
        // collection.  This will then be available as WebContext.Current.
        WebContext webContext = new WebContext();
        webContext.Authentication = new FormsAuthentication();
        //webContext.Authentication = new WindowsAuthentication();
        this.ApplicationLifetimeObjects.Add(webContext);

如果尝试在ViewModel构造函数中订阅LoggedIn或LoggedOut事件,就会遇到一些问题: WebContext还不存在.

所以我想..。我将首先创建我的WebContext,然后在我的应用程序中创建InitializeComponents(),但这使ExpressionBlend很难过.

所以,这里是我的解决方案,我想和大家分享,因为我并不完全相信这是正确的方法:

代码语言:javascript
复制
App.Current.Startup += (sender, eventArgs) => 
        {
            WebContext.Current.Authentication.LoggedIn += WebContext_LoggedIn;
            WebContext.Current.Authentication.LoggedOut += WebContext_LoggedOut;
        };

在我的ViewModel ctor中,我订阅了App.Current.Startup,我的委托将订阅我的ViewModel到登录事件,这样我就没有改变我的App.xaml.cs,并且当WebContext存在时,我肯定会订阅登录事件。所以:

代码语言:javascript
复制
private void WebContext_LoggedIn(object sender, AuthenticationEventArgs e)
    {
        LoadData();
    }

编辑

在本例中,我更感兴趣的是,当我说我不应该更改InitializeComponent()与其他事件之间的顺序,并且需要侦听某个特定事件以触发InitializeComponent().时,我是否正确。

为了完整起见,这里是我的重构,以消除ViewModel中的依赖:

我创建了一条信息:

代码语言:javascript
复制
public class UserLoginStatusChangedMessage : MessageBase
{
    public bool IsLoggedIn { get; set; }
}

寄到这里:

代码语言:javascript
复制
private void Application_Startup(object sender, StartupEventArgs e)
    {
        // This will enable you to bind controls in XAML files to WebContext.Current
        // properties
        this.Resources.Add("WebContext", WebContext.Current);

        // This will automatically authenticate a user when using windows authentication
        // or when the user chose "Keep me signed in" on a previous login attempt
        WebContext.Current.Authentication.LoadUser(this.Application_UserLoaded, null);

        // Show some UI to the user while LoadUser is in progress
        this.InitializeRootVisual();

        WebContext.Current.Authentication.LoggedIn += (s, a) => 
        { 
            Messenger.Default.Send(new UserLoginStatusChangedMessage 
            { IsLoggedIn = true }); 
        };

        WebContext.Current.Authentication.LoggedOut += (s, a) => 
        {
            Messenger.Default.Send(new UserLoginStatusChangedMessage 
            { IsLoggedIn = false });
        };
    }

并在ViewModel ctor中收到了这样的消息:

代码语言:javascript
复制
 Messenger.Default.Register<UserLoginStatusChangedMessage>(this, msg => 
        {
            if (msg.IsLoggedIn)
            {
                LoadData();
            }
        });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-09-21 16:42:25

我建议使用某种信使类,在用户登录时发送消息。MVVMLight有一个易于使用的不错的信使类。然后,您只需在用户登录状态更改时发送消息,并在视图模型中订阅该事件,该事件需要知道用户是否已登录。

您可以检查WebContext是否已创建,以及用户是否在创建视图模型时登录,然后只需使用消息来确定是否/何时会发生更改。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7501319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档