【问题标题】:MvvmCross Visibility Binding doesn't workMvvmCross 可见性绑定不起作用
【发布时间】:2018-05-28 09:35:30
【问题描述】:

我希望使用一个按钮和一个具有 mvvmcross 可见性绑定的属性来显示和隐藏确定的布局。

编辑: 实际问题是,如果我部署项目,则此命令不起作用,并且仅在必须显示第一个时才显示布局。

视图模型:

public class HomeViewModel : MvxViewModel
{
    #region Properties
    #region Literals 
    private string _usernameLoginTV;
    public string UsernameLoginTV
    {
        get { return _usernameLoginTV; }
        set
        {
            _usernameLoginTV = value;
            RaisePropertyChanged(() => UsernameLoginTV);
        }
    }
    private string _usernameHint;
    public string UsernameHint
    {
        get { return _usernameHint; }
        set
        {
            _usernameHint = value;
            RaisePropertyChanged(() => UsernameHint);
        }
    }

    private string _passwordLoginTV;
    public string PasswordLoginTV
    {
        get { return _passwordLoginTV; }
        set
        {
            _passwordLoginTV = value;
            RaisePropertyChanged(() => PasswordLoginTV);
        }
    }

    private string _passwordHint;
    public string PasswordHint
    {
        get { return _passwordHint; }
        set
        {
            _passwordHint = value;
            RaisePropertyChanged(() => PasswordHint);
        }
    }

    private string _passwordRegisterTV;
    public string PasswordRegisterTV
    {
        get { return _passwordRegisterTV; }
        set
        {
            _passwordRegisterTV = value;
            RaisePropertyChanged(() => PasswordRegisterTV);
        }
    }
    private string _confirmPasswordHintRegister;
    public string ConfirmPasswordHintRegister
    {
        get { return _confirmPasswordHintRegister; }
        set
        {
            _confirmPasswordHintRegister = value;
            RaisePropertyChanged(() => ConfirmPasswordHintRegister);
        }
    }
    private string _loginButtonText;
    public string LoginButtonText
    {
        get { return _loginButtonText; }
        set
        {
            _loginButtonText = value;
            RaisePropertyChanged(() => LoginButtonText);
        }
    }

    private string _registerButtonText;
    public string RegisterButtonText
    {
        get { return _registerButtonText; }
        set
        {
            _registerButtonText = value;
            RaisePropertyChanged(() => RegisterButtonText);
        }
    }
    //Boolean property Hider Register View
    private bool _hideRegisterView;
    public bool HideRegisterView
    {
        get { return _hideRegisterView; }
        set
        {
            _hideRegisterView = value;
            RaisePropertyChanged(() => HideRegisterView);
        }
    }
    #endregion
    #region Click
    public IMvxCommand ShowRegisterSquare
    {
        get
        {
            return new MvxCommand(() =>
            {
                HideRegisterView = !HideRegisterView;
            });
        }
    }
    #endregion
    #endregion

    public override Task Initialize()
    {
        UsernameLoginTV = Strings.usernameLoginTV;
        UsernameHint = Strings.usernameLoginHint;
        PasswordLoginTV = Strings.passwordLoginTV;
        PasswordHint = Strings.passwordLoginHint;
        LoginButtonText = Strings.loginButton;
        RegisterButtonText = Strings.registerButton;
        ConfirmPasswordHintRegister = Strings.repeatPassword;
        PasswordRegisterTV = Strings.passwordRegisterTV;

        return base.Initialize();
    }

}

AXML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:background="@drawable/background">

<!-- HOME NO LOGGED -->
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal">
<!-- Es imperativo que el ScrollView solo tenga un hijo-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical">
    <!-- Login Layout-->
        <LinearLayout
            android:id="@+id/loggingLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="vertical"
            local:MvxBind="Visibility InvertedVisibility(HideRegisterView)">
<ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/icon" />
        <!-- Square Login fields-->
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="#BFFFFFFF">
                <TextView
                    android:id="@+id/usernameLoginTV"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="10dp"
                    android:text="-_Username:"
                    android:textSize="16dp"
                    android:textColor="@color/text_login"
                    local:MvxBind="Text UsernameLoginTV" />
                <EditText
                    android:id="@+id/usernameLoginET"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:paddingLeft="2dp"
                    android:hint="username"
                    android:textSize="16dp"
                    android:maxLines="1"
                    android:background="@color/text_button"
                    local:MvxBind="Hint UsernameHint" />
                <TextView
                    android:id="@+id/passwordLoginTV"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="10dp"
                    android:text="-_Password:"
                    android:textSize="16dp"
                    android:textColor="@color/text_login"
                    local:MvxBind="Text PasswordLoginTV" />
                <EditText
                    android:id="@+id/passwordET"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:paddingLeft="2dp"
                    android:paddingRight="2dp"
                    android:hint="-_password"
                    android:inputType="textPassword"
                    android:textSize="16dp"
                    android:maxLines="1"
                    android:background="#f9f9f9"
                    local:MvxBind="Hint PasswordHint" />
            <!-- Login Button -->
                <Button
                    android:id="@+id/loginButton"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="10dp"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:background="#00cc66"
                    android:text="-_Iniciar Sesion"
                    android:textColor="@color/text_button"
                    android:textSize="18dp"
                    local:MvxBind="Text LoginButtonText" />
            </LinearLayout>
        <!-- Register Button -->
            <Button
                android:id="@+id/registerButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:background="#4db8ff"
                android:gravity="center"
                android:text="-_Registrar"
                android:textColor="@color/text_button"
                android:textSize="18dp"
                local:MvxBind="Text RegisterButtonText; Click ShowRegisterSquare" />
        </LinearLayout>
    <!-- Register Layout-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="#BFFFFFFF"
            local:MvxBind="Visibility Visibility(HideRegisterView)">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:text="-_Username:"
                android:textColor="@color/text_login"
                android:textSize="16dp"
                local:MvxBind="Text UsernameLoginTV" />
            <EditText
                android:id="@+id/usernameRegisterInput"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:paddingLeft="2dp"
                android:hint="username"
                android:textSize="16dp"
                android:maxLines="1"
                android:background="@color/text_button"
                local:MvxBind="Hint UsernameHint" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:text="-_Password:"
                android:textSize="16dp"
                android:textColor="@color/text_login"
                local:MvxBind="Text PasswordLoginTV" />
            <EditText
                android:id="@+id/passwordRegisterInput"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:paddingLeft="2dp"
                android:paddingRight="2dp"
                android:hint="password"
                android:textSize="16dp"
                android:inputType="textPassword"
                android:maxLines="1"
                android:background="#f9f9f9"
                local:MvxBind="Hint PasswordHint" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:text="-_Password:"
                android:textSize="16dp"
                android:textColor="@color/text_login"
                local:MvxBind="Text PasswordLoginTV" />
            <EditText
                android:id="@+id/passwordConfirmRegister"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:paddingLeft="2dp"
                android:paddingRight="2dp"
                android:hint="password"
                android:textSize="16dp"
                android:inputType="textPassword"
                android:maxLines="1"
                android:background="#f9f9f9"
                local:MvxBind="Hint ConfirmPasswordHintRegister" />
            <Button
                android:id="@+id/loginButton"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:background="#00cc66"
                android:text="-_Iniciar Sesion"
                android:textColor="@color/text_button"
                android:textSize="18dp"
                local:MvxBind="Text RegisterButtonText" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

我认为这是不正确的,但应该有效...... 感谢您的帮助!

【问题讨论】:

  • 目前还不清楚实际问题是什么。你能提供更多细节吗?
  • 实际问题是,如果我部署项目,此命令不起作用,并且布局仅在必须显示第一个时显示。你有理由,很抱歉没有解释这个问题
  • 您能否发布整个 axml,因为我不知道您所说的“布局仅在必须显示第一个时显示”是什么意思,并且在您的代码中只有 1 个布局。其他人在哪里?我可以从该代码中看到的是,如果您单击按钮,loggingLayout 会更改其可见性
  • @fmaccaroni 我发布了你所说的整个 axml

标签: c# xamarin xamarin.android mvvmcross


【解决方案1】:

您是否安装了 MvvmCross.Plugin.Visibility Nuget 包?

我已经能够在代码中使用可见性绑定实现隐藏和显示,如 here 所示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多