MvvmCross绑定到MvxDialogViewController中的进度指示器

时间:2014-05-29 01:07:36

标签: xamarin.ios mvvmcross

我有一个MvxDialogViewController,我试图通过添加可绑定属性来使用Xamarin示例中显示的进度指示器。

当我以编程方式将Visble设置为true时,我可以显示指示符,但是当我绑定到vm属性时不会显示。

以下是视图代码:

var bindings = this.CreateInlineBindingTarget<LoginViewModel>();

Root = new RootElement("Login")
{
    new Section("Login Credentials")
    {
        new EntryElement("Username", "Enter user name").Bind(bindings, vm => vm.UserName),
        new EntryElement("Password", "Enter password", "", true).Bind(bindings, vm => vm.Password)
    }
};

_bindableProgress = new BindableProgress(UIScreen.MainScreen.Bounds).Bind(bindings, b => b.Visible, vm => vm.IsBusy);
_bindableProgress.Title = "Logging in...";
View.Add(_bindableProgress);

我也尝试像这样绑定:

var set = this.CreateBindingSet<LoginView, LoginViewModel>();
set.Bind(_bindableProgress).For(b => b.Title).To(vm => vm.ProgressTitle);
set.Bind(_bindableProgress).For(b => b.Visible).To(vm => vm.IsBusy);
set.Apply();

但两种方式都没有效果。

这是BindableProgress类:

public class BindableProgress : UIView
{
    private UIActivityIndicatorView _activitySpinner;
    private UILabel _loadingLabel;

    public string Title { get; set; }

    private bool _visible;
    public bool Visible
    {
        get { return _visible; }
        set
        {
            _visible = value;
            if (_visible)
            {
                Show();
            }
            else
            {
                Hide();
            }
        }
    }

    public BindableProgress(RectangleF frame) : base(frame)
    {
        // configurable bits
        BackgroundColor = UIColor.Black;
        Alpha = 0;
        AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;

        float labelHeight = 22;
        float labelWidth = Frame.Width - 20;

        // derive the center x and y
        float centerX = Frame.Width/2;
        float centerY = Frame.Height/2;

        // create the activity spinner, center it horizontally and put it 5 points above center x
        _activitySpinner = new UIActivityIndicatorView(UIActivityIndicatorViewStyle.WhiteLarge);
        _activitySpinner.Frame = new RectangleF(
            centerX - (_activitySpinner.Frame.Width / 2),
            centerY - _activitySpinner.Frame.Height - 20,
            _activitySpinner.Frame.Width,
            _activitySpinner.Frame.Height);
        _activitySpinner.AutoresizingMask = UIViewAutoresizing.FlexibleMargins;
        AddSubview(_activitySpinner);


        // create and configure the label
        _loadingLabel = new UILabel(new RectangleF(
            centerX - (labelWidth/2),
            centerY + 20,
            labelWidth,
            labelHeight
            ));
        _loadingLabel.BackgroundColor = UIColor.Clear;
        _loadingLabel.TextColor = UIColor.White;
        _loadingLabel.TextAlignment = UITextAlignment.Center;
        _loadingLabel.AutoresizingMask = UIViewAutoresizing.FlexibleMargins;
        AddSubview(_loadingLabel);
    }

    private void Show()
    {
        _loadingLabel.Text = Title;
        Alpha = 0.75f;
        _activitySpinner.StartAnimating();
    }


    /// <summary>
    /// Fades out the control and then removes it from the super view
    /// </summary>
    private void Hide()
    {
        _activitySpinner.StopAnimating();

        Animate(
            0.5, // duration
            () => { Alpha = 0; },
            () => { RemoveFromSuperview(); }
            );
    }
}

有什么想法吗?

更新

我的vm属性看起来像这样

private bool _isBusy;
public bool IsBusy
{
    get { return _isBusy; }
    set { _isBusy = value; RaisePropertyChanged(() => IsBusy); }
}

它在Android中工作正常,所以我猜不到问题。

1 个答案:

答案 0 :(得分:0)

IsBusy在绑定时可能是假的。因此_visible设置为false并调用Hide()。现在,视图已从超级视图中删除,您无法再显示它,因为Show()不会再次将其添加到超级视图中。尝试省略RemoveFromSuperview();。或者像这样修改Visible属性:

public bool Visible
{
    get { return _visible; }
    set
    {
        if(_visible == value) 
            return;

        _visible = value;
        if (_visible)
        {
            Show();
        }
        else
        {
            Hide();
        }
    }
}