MVVM - 视图加载和事件处理

发布于 2024-12-09 06:42:35 字数 320 浏览 9 评论 0原文

在我的 Windows Phone 应用程序中,我需要跟踪一些事件以获得良好的流程。但我不确定如何按顺序处理它们。

应用程序启动时需要完成的操作:

  • 加载主视图并实例化相应的视图模型
  • 在视图模型的构造函数中,我启动一个登录序列,该序列在使用事件处理程序完成时发出信号

现在,当登录序列完成并且视图是完全加载我需要启动另一个序列。 但问题是,这两个事件“完成”的顺序并不总是相同...... 我使用 MVVMLight 中的 EventToCommand 向视图模型发出视图已“加载”的信号。

关于如何同步这个的任何想法。

In my windows phone app, I need to track some events to get a good flow. But I'm not sure how to handle them in good sequence.

What needs to be done at startup of the app:

  • Main view is loaded and corresponding view model instantiated
  • In the constructor of the view model I initiate a login sequence that signals when completed with an eventhandler

Now when the login sequence has finished AND the view is completely loaded I need to startup another sequence.
But here is the problem, the order of these 2 events 'completing' is not always the same...
I've use the EventToCommand from MVVMLight to signal the view model that the view has 'loaded'.

Any thoughts on how to synchronize this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人事已非 2024-12-16 06:42:35

因为您不应该在 UI 线程上使用等待句柄或类似的东西。您必须使用视图模型中的标志来同步这两种方法,并在继续之前检查它们。

因此,在视图模型中实现两个布尔属性。现在,当登录对话框完成时,将其中一个属性(我们称之为 IsLoggedIn)设置为 true,当初始化序列完成时,将另一个属性(IsInitialized)设置为 true。现在的技巧在于这两个属性的 setter 的实现:

#region [IsInitialized]

public const string IsInitializedPropertyName = "IsInitialized";

private bool _isInitialized = false;

public bool IsInitialized {
    get {
        return _isInitialized;
    }
    set {
        if (_isInitialized == value)
            return;

        var oldValue = _isInitialized;
        _isInitialized = value;

        RaisePropertyChanged(IsInitializedPropertyName);

        InitializationComplete();
    }
}

#endregion

#region [IsLoggedIn]

public const string IsLoggedInPropertyName = "IsLoggedIn";

private bool _isLoggedIn = false;

public bool IsLoggedIn {
    get {
        return _isLoggedIn;
    }
    set {
        if (_isLoggedIn == value) 
            return;

        var oldValue = _isLoggedIn;
        _isLoggedIn = value;

        RaisePropertyChanged(IsLoggedInPropertyName);

        InitializationComplete();
    }
}

#endregion

public void InitializationComplete() {
    if (!(this.IsInitialized && this.IsLoggedIn))
        return;

    // put your code here
}

或者,您可以从 setter 中删除 InitializationComplete 并将 InitializationComplete 更改为:

public void InitializationComplete() {
    // put your code here
}

然后订阅 'PropertyChanged'事件使用以下实现:

private void Class1_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
    if (e.PropertyName == IsInitializedPropertyName || e.PropertyName == IsLoggedInPropertyName) {
        if (this.IsInitialized && this.IsLoggedIn)
            InitializationComplete();
    }
}

As you should not use wait handles or something similar on the UI thread. You will have to sync the two method using flags in your view model and check them before progressing.

So, implement two boolean properties in your view model. Now when the login dialog is finished set one of the properties (lets call it IsLoggedIn) to true, and when the initialization sequence is finished you set the other property (how about IsInitialized) to true. The trick now lies in the implementation of the setter of these two properties:

#region [IsInitialized]

public const string IsInitializedPropertyName = "IsInitialized";

private bool _isInitialized = false;

public bool IsInitialized {
    get {
        return _isInitialized;
    }
    set {
        if (_isInitialized == value)
            return;

        var oldValue = _isInitialized;
        _isInitialized = value;

        RaisePropertyChanged(IsInitializedPropertyName);

        InitializationComplete();
    }
}

#endregion

#region [IsLoggedIn]

public const string IsLoggedInPropertyName = "IsLoggedIn";

private bool _isLoggedIn = false;

public bool IsLoggedIn {
    get {
        return _isLoggedIn;
    }
    set {
        if (_isLoggedIn == value) 
            return;

        var oldValue = _isLoggedIn;
        _isLoggedIn = value;

        RaisePropertyChanged(IsLoggedInPropertyName);

        InitializationComplete();
    }
}

#endregion

public void InitializationComplete() {
    if (!(this.IsInitialized && this.IsLoggedIn))
        return;

    // put your code here
}

Alternatively you can remove the InitializationComplete from the setters and change InitializationComplete to:

public void InitializationComplete() {
    // put your code here
}

Then subscribe to the 'PropertyChanged' event use the following implementation:

private void Class1_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
    if (e.PropertyName == IsInitializedPropertyName || e.PropertyName == IsLoggedInPropertyName) {
        if (this.IsInitialized && this.IsLoggedIn)
            InitializationComplete();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文