2016-06-27 2 views
2

editText 및 MvvmCross에 문제가 있습니다. editText (숫자 포함)을 void로 설정하면 (삭제시) ViewModel 속성에 영향을 미치지 않습니다.MvvmCross - EditText를 void로 설정해도 ViewModelProperty가 업데이트되지 않습니다.

그것은 내 코드에 의존하지 않는

, 그것은 MVVMCross의 API를 예를 재현 할 수 있습니다

AXML :

<TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Enter a number..." /> 
<EditText 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:inputType="numberDecimal|numberSigned" 
    android:textSize="30dp" 
    local:MvxBind="Text DoubleProperty" /> 

뷰 모델을 :

private double _doubleProperty = 42.12; 

public double DoubleProperty 
{ 
    get { return _doubleProperty; } 
    set { _doubleProperty = value; RaisePropertyChanged(() => DoubleProperty); } 
} 

screenshot from API Example

+0

보여줄 수 있습니까? – xleon

답변

2

문제는 DoubleProperty가 아닌 nullable double 유형이므로 값의 '무효'가 될 수 없습니다. double 유형을 nullable double로 변경하는 것만으로는 작동하지 않습니다. 빈 문자열을 nullable double로 설정하면 실패합니다.

MvxBind:Error: 44.49 SetValue failed with exception - ArgumentException: Object of type 'System.String' cannot be converted to type 'System.Nullable`1[System.Double]'.

그래서 어떻게 작동시킬 수 있습니까?

접근 한 - 허용 null 값가 null 값으로 빈 문자열을 변환하는 MvxValueConverter을 사용하기 전에 사용한

한 가지 방법. 에서

public class NullableDoubleToStringConverter : MvxValueConverter<double?, string> 
{ 
    protected override string Convert(double? value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return value.ToString(); 
    } 

    protected override double? ConvertBack(string value, Type targetType, object parameter, CultureInfo culture) 
    { 
     double outValue; 
     return double.TryParse(value, out outValue) ? (double?)outValue : null; 
    } 
} 

당신의 ViewModel Null 허용하는 유형을 변경 :에서

private double? _doubleProperty = 42.12; 
public double? DoubleProperty 
{ 
    get { return _doubleProperty; } 
    set { _doubleProperty = value; RaisePropertyChanged(() => DoubleProperty); } 
} 

당신의 AXML/XML 컨버터를 사용

<EditText 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:inputType="numberDecimal|numberSigned" 
    android:textSize="30dp" 
    local:MvxBind="Text NullableDoubleToString(DoubleProperty)" /> 

접근 2 - 제로 (null이 아닌)

에 기본

이 접근 방식은 editText에서 마지막 현재 값을 제거 할 때 값을 0으로 재설정하십시오.

변환기 :

public class DoubleToStringConverter : MvxValueConverter<double, string> 
{ 
    protected override string Convert(double value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return value.ToString(); 
    } 

    protected override double ConvertBack(string value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return string.IsNullOrWhiteSpace(value) ? default(double) : double.Parse(value); 
    } 
} 

뷰 모델 속성 :

private double _doubleProperty = 42.12; 
public double DoubleProperty 
{ 
    get { return _doubleProperty; } 
    set { _doubleProperty = value; RaisePropertyChanged(() => DoubleProperty); } 
} 

레이아웃 AXML/XML :이 EDITTEXT/속성에 대한 당신의 axml과의 ViewModel 코드가

<EditText 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:inputType="numberDecimal|numberSigned" 
    android:textSize="30dp" 
    local:MvxBind="Text DoubleToString(DoubleProperty)" /> 
+0

답변 해 주셔서 감사합니다. 그것은 작동합니다! –

관련 문제