2016-04-30 3 views
0

私はXamarinとMvvmCrossでAndroidアプリケーションを作成しました。 いくつかのビュー(テキスト、編集テキスト、ボタン)をViewModelにバインドしたいとします。これまで何も変わったことはありません。しかし、私のバインディングは適用されません...私は型付きのFindViewByIdを使用すると、私はトレースエラーを取得しませんが、バインディングは適用されません。私は、アプリケーションを実行するとMvvmCross:空のバインディングターゲットがMvxTargetBindingFactoryRegistryに渡されました

、私は次のトレースがあります

MvxBind:Error: Empty binding target passed to MvxTargetBindingFactoryRegistry 
MvxBind:Warning: Failed to create target binding for binding for TextProperty 

OnCreate(Bundle bundle)ボイドの私のオーバーライドは次のとおりです。

:私はAXMLで行うことを試みた

SetContentView(Resource.Layout.Reference); 
var referenceTextView = FindViewById(Resource.Id.referenceEditView); // untyped FindViewById 
var siteTextView = FindViewById<TextView>(Resource.Id.siteTextView); // typed FindViewById<T> 
//var goButton = FindViewById<Button>(Resource.Id.goButton); 
var bindingsSet = this.CreateBindingSet<ReferenceView, ReferenceViewModel>(); 
bindingsSet.Bind(referenceTextView).To(vm => vm.Reference).Mode(MvxBindingMode.TwoWay); 
bindingsSet.Bind(siteTextView).To(vm => vm.Site); 
//bindingsSet.Bind(goButton).To(vm => vm.GoCommand); 
bindingsSet.Apply(); 
base.OnCreate(bundle); 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/siteTextView" 
    android:text="####" 
    local:MvxBind="Text Site" 
    android:gravity="center" /> 
<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/referenceTextView" 
    android:hint="Numéro de dossier" 
    local:MvxBind="Text Reference" /> 
<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Accéder" 
    android:id="@+id/goButton" 
    local:MvxBind="Click GoCommand" /> 

私のプロパティのgetterとsetterは、RaiseAndSetIfChangedメソッドを使用します。

private string _reference; 
public string Reference 
{ 
    get { return _reference; } 
    set { this.RaiseAndSetIfChanged(ref _reference, value,() => Reference); } 
} 

私はLinkerPleaseInclude元のクラスと同じLinkerPleaseIncludeクラスをしました。 私の設定はMvxAndroidSetupクラス から継承し、他のViewModelsではバインディングが正しく適用されます。私は、あなたが二回バインドする必要があると思うこれらの行を削除いけない

答えて

1

SetContentViewの前にbase.OnCreate(bundle);に電話する必要があります。これは、ViewModelがその呼び出しの内側に配置されているためです。そうしないと、明らかに正確なエラーが表示されます。 Sourceはnullになり、ターゲットにバインドされません。

だから、どちらかを行うことができます:

base.OnCreate(bundle); 
SetContentView(Resource.Layout.Reference); 

そして、あなたのAXMLにすべてのバインディングを持っています。ただ、そもそもbase.OnCreateを呼び出していることを確認してください

base.OnCreate(bundle); 
SetContentView(Resource.Layout.Reference); 

var referenceTextView = FindViewById<TextView>(Resource.Id.referenceEditView); 
var siteTextView = FindViewById<TextView>(Resource.Id.siteTextView); 

var bset = this.CreateBindingSet<ReferenceView, ReferenceViewModel>(); 
bset.Bind(referenceTextView).To(vm => vm.Reference); 
bset.Bind(siteTextView).To(vm => vm.Site); 
bset.Apply(); 

:それとも、舞台裏でバインディングを設定し、他のアプローチを行うことができます。

0

var referenceTextView = FindViewById(Resource.Id.referenceEditView); // untyped FindViewById 
var siteTextView = FindViewById<TextView>(Resource.Id.siteTextView); // typed FindViewById<T> 
//var goButton = FindViewById<Button>(Resource.Id.goButton); 
var bindingsSet = this.CreateBindingSet<ReferenceView, ReferenceViewModel>(); 
bindingsSet.Bind(referenceTextView).To(vm => vm.Reference).Mode(MvxBindingMode.TwoWay); 
bindingsSet.Bind(siteTextView).To(vm => vm.Site); 
//bindingsSet.Bind(goButton).To(vm => vm.GoCommand); 
bindingsSet.Apply(); 

だからあなたの作成しただけでこれです:

SetContentView(Resource.Layout.Reference); 
base.OnCreate(bundle); 

そしてAXMLファイルにバインディングを保持します。

はあなたのXAMLファイルの先頭にこれを持っていることを確認してください:

xmlns:local="http://schemas.android.com/apk/res-auto"

を使用すると、CSファイルにバインディングを行っている場合にも、MvvmCross結合モードはデフォルトで双方向です。ですから、.Mode(MvxBindingMode.TwoWay);

+0

確かに、私はミスをしているかもしれません:私は知っているさまざまなアプローチを使用しました。 AXMLファイルで指定した行があります。 –

+0

Androidの一般的な心配はありません。珍しいことがない限り、私はxamlのバインディングを行う傾向があります。 –

1

警告

MvxBind:Error: 2,20 Empty binding target passed to MvxTargetBindingFactoryRegistry MvxBind:Warning: 2,20 Failed to create target binding for binding for Text

を必要といけないタイプViewであるようにreferenceTextViewその結果var referenceTextView = FindViewById(Resource.Id.referenceEditView);によって引き起こされます。For(targetProperty)なしBind<TTArget>を呼び出すときに

MvvmCrossはタイプTTargetデフォルト標的結合プロパティを探しています。これは、同じようにテーブルにルックアップしている:あなたはbindingsSet.Bind(referenceTextView)ウィッヒにそれを渡すためTTargetは、View代わりのTextViewであるあなたのケースでは

TTarget  Property 
---------------------- 
TextView  Text 
Button  Click 
...   ... 

bindings.Bind<View>(btnNumber)の暗黙の呼び出しです。 Viewには、デフォルトのバインディングターゲットプロパティはありません。

bindings.Bind(btnNumber).For("Text") 

のように明示的に設定するか、タイプFindViewById<TextView>を使用する必要があります。

+0

私はミスをしているかもしれません。私は知っているさまざまなアプローチを使用しました:私は型付き型と型なしのFindViewByIdメソッドを使用しました。 –

+0

Okを次にチーズバロンが言ったようにして、 'base.OnCreate(bundle)'を関数の先頭に移動します(他のものの前に呼び出します)。 –

関連する問題