0

我已经实现了一个类,它负责异步加载对象并相应地更改光标,即在UpdateCursor方法中:

    static Cursor cursor;

    public AsyncLoader(Func<CbT> request, Callback callback, Cursor cursor)
    {
        this.request = request;
        this.callback = callback;
        AsyncLoader<CbT>.cursor = cursor;

        LatestRequestId = Guid.NewGuid();
        UpdateCursor();
        ...
    }

    void UpdateCursor()
    {
        if (LatestRequestId == Guid.Empty)
        {
            cursor = Cursors.Arrow;
        }
        else
        {
            cursor = Cursors.AppStarting;
        }
    }

在我要使用这个类的类中,我有 Cursor 属性,它实现了 INotifyProperty 并且它绑定到窗口的光标:

    private Cursor _CurrentCursor;
    public Cursor CurrentCursor
    {
        get { return _CurrentCursor; }
        set
        {
            if (value != _CurrentCursor)
            {
                _CurrentCursor = value;
                OnPropertyChanged("CurrentCursor");
            }
        }
    }

在视图中:

    Cursor="{Binding CurrentCursor}"

问题是,如何将 CurrentCursor 传递给 AsyncLoader,以便在 UpdateCursor 运行时,更改将反映回 CurrentCursor 并触发 PopertyChange 事件?

4

1 回答 1

0

解决方案之一是创建一个界面

interface ICursorHolder{
    Cursor CurrentCursor{get;set;}
}

将 ICursorHolder 类型的实例传递给 AsyncLoader 并使用 Cursor 属性在您的类中实现此接口

public AsyncLoader(Func<CbT> request, Callback callback, ICursorHolder cursorHolder)
{
    this.request = request;
    this.callback = callback;
    AsyncLoader<CbT>.cursorHolder = cursorHolder;//change according to your logic

    LatestRequestId = Guid.NewGuid();
    UpdateCursor();
    ...
}

void UpdateCursor()
{
    if (LatestRequestId == Guid.Empty)
    {
        cursorHolder.Cursor = Cursors.Arrow;
    }
    else
    {
        cursorHolder.Cursor = Cursors.AppStarting;
    }
}

和你的班级

public class ShellPresenter: PresenterBase, ICursorHolder{   
    private Cursor _CurrentCursor;
    public Cursor CurrentCursor
    {
        get { return _CurrentCursor; }
        set {
            if (value != _CurrentCursor){
                _CurrentCursor = value;
                OnPropertyChanged("CurrentCursor");
            }
        }
    }    
}

更新1

更新了示例代码

更新2

private void CallAsync(){
   ShellPresenter shell=GetInstance();
   Loader.AsyncLoader(request,callback, shell);

}
于 2012-08-15T12:00:53.683 回答