简体   繁体   English

使用棱镜创建基础荷载视图

[英]Creating base loading view with prism

My prism application has a lot of async operations called from my view models. 我的棱镜应用程序有很多从我的视图模型调用的异步操作。 In some cases, I want the view to be disabled and display some kind of busy indicator until the viewmodel gets back the result from the async operation. 在某些情况下,我希望视图被禁用并显示某种繁忙的指示符,直到视图模型从异步操作获取结果为止。

I though of creating a base view which will implement this behavior (ie have a dependency property of IsLoading which will disable the view and display a busy indicator above it). 我虽然创建了将实现此行为的基本视图(即具有IsLoading的依赖项属性,该属性将禁用该视图并在其上方显示一个繁忙指示器)。 The problem is, I'm not sure how to implement this base view. 问题是,我不确定如何实现此基本视图。 Any help would be appreciated, thanks. 任何帮助,将不胜感激,谢谢。

Edit: I wrote a LoadingView which does the job, I think. 编辑:我想我写了一个工作的LoadingView。

public class LoadingView : UserControl { private object content;

  public bool IsLoading { get { return (bool)GetValue(IsLoadingProperty); } set { SetValue(IsLoadingProperty, value); } } private ProgressRing m_RingControl; public LoadingView() { m_RingControl = new ProgressRing(); m_RingControl.IsActive = false; } // Using a DependencyProperty as the backing store for IsLoading. This enables animation, styling, binding, etc... public static readonly DependencyProperty IsLoadingProperty = DependencyProperty.Register("IsLoading", typeof(bool), typeof(LoadingView), new PropertyMetadata(false, IsActivePropertyChanged)); private static void IsActivePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { LoadingView view = d as LoadingView; if (view != null) { // Loading - show ring control if (((bool)e.NewValue) == true) { view.content = view.Content; view.Content = view.m_RingControl; view.m_RingControl.IsActive = true; } else { view.m_RingControl.IsActive = false; view.Content = view.content; } } } } 

and i put binding on LoadingView.IsLoading with some IsLoading (or IsBusy) in the viewmodel 我在视图模型中将绑定与一些IsLoading(或IsBusy)放在LoadingView.IsLoading上

This is a subject that can get really complex really fast. 这是一个可能很快变得非常复杂的主题。

I would advise a small change in approach - instead of putting an IsBusy property in a base viewmodel, instead make it abstract so that each derived viewmodel has to implement its own specific check. 我建议在方法上进行一些小的更改-而不是将IsBusy属性放在基础视图模型中,而应使其抽象化,以便每个派生的视图模型必须实现自己的特定检查。

public class BaseViewModel : INotifyPropertyChanged
{

    public abstract bool IsBusy { get; }

}

public class FancyViewModel : BaseViewModel
{

    public override bool IsBusy
    {
        get { return [check #1] && [check #2]...; }
    }
}

Now it is up to each specific viewmodel to determine whether it is busy. 现在由每个特定的视图模型确定是否繁忙。 A crude mechanism is to have a counter which you increment every time an async function is kicked off, and decrement it when the operation ends - if its value is equal to zero then there are no current async operations. 粗略的机制是拥有一个计数器,您可以在每次启动异步功能时就增加一个计数器,并在操作结束时将其递减-如果其值等于零,则说明当前没有异步操作。 When using flags or counters be aware of various property reading issues that can occur because of compiler optimisations , learn to use the volatile keyword in the right place. 当使用标志或计数器时,请注意由于编译器的优化而可能发生的各种属性读取问题,请学习在正确的位置使用volatile关键字。

Alternatively instead of keeping a counter you could use the thread safe CountdownEvent class . 另外,也可以使用线程安全的CountdownEvent类来代替保持计数器。 If you want to get really advanced then you can check out the various thread signalling mechanisms in the System.Threading namespace , or look at task parallelism and the Task object . 如果您想真正变得更高级,则可以在System.Threading名称空间中查看各种线程信令机制,或者查看任务并行性Task对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM