MvvmCross - MvxBind:警告无法为-empty绑定Hello创建目标绑定 -

时间:2015-05-13 13:17:26

标签: c# android xamarin xamarin.android mvvmcross

我刚刚开始使用我的第一个Xamarin应用程序,并将其设置为新解决方案的默认配置,只需极少的更改。

应用程序编译正常并且也可以很好地部署到虚拟设备。

MvxBindingAttributes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="MvxBinding">
    <attr name="MvxBind" format="string"/>
    <attr name="MvxLang" format="string"/>
  </declare-styleable>
  <declare-styleable name="MvxControl">
    <attr name="MvxTemplate" format="string"/>
  </declare-styleable>
  <declare-styleable name="MvxListView">
    <attr name="MvxItemTemplate" format="string"/>
    <attr name="MvxDropDownItemTemplate" format="string"/>
  </declare-styleable>
  <item type="id" name="MvxBindingTagUnique"/>
  <declare-styleable name="MvxImageView">
    <attr name="MvxSource" format="string"/>
  </declare-styleable>
</resources>

HomeViewModel.cs

public class HomeViewModel : MvxViewModel
{
    /// <summary>
    /// The _hello.
    /// </summary>
    private string _hello = "Hello MvvmCross";

    /// <summary>
    /// Gets or sets the hello.
    /// </summary>
    public string Hello
    {
        get
        {
            return _hello;
        }

        set
        {
            _hello = value;
            RaisePropertyChanged(() => Hello);
        }
    }
}

App.cs

public class App : Cirrious.MvvmCross.ViewModels.MvxApplication
{
    /// <summary>
    /// The initialize.
    /// </summary>
    public override void Initialize()
    {
        CreatableTypes().EndingWith("Service").AsInterfaces().RegisterAsLazySingleton();
        RegisterAppStart<HomeViewModel>();
    }
}

HomeView.cs

[Activity(Label = "View for HomeViewModel")]
public class HomeView : MvxActivity
{
    /// <summary> The on create. </summary>
    /// <param name="bundle"> The bundle. </param>
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.HomeView);
    }
}

HomeView.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="fill_parent"
    android:layout_height="fill_parent">
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="40dp"
        local:MvxBind="Text Hello" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="40dp"
        local:MvxBind="Hello" />
</LinearLayout>

当我在设备上运行时,Hello MvvmCross文本按预期显示,但在输出窗口中我看到此错误:

MvxBind:Warning:  8.57 Failed to create target binding for binding Hello for -empty-
[0:] MvxBind:Warning:  8.57 Failed to create target binding for binding Hello for -empty-
05-13 12:56:37.654 I/mono-stdout(  683): MvxBind:Warning:  8.57 Failed to create target binding for binding Hello for -empty-

如果我关闭虚拟设备上的应用程序,然后再次启动它,我会得到:

MvxBind:Warning: 97.69 Failed to create target binding for binding Hello for -empty-
[0:] MvxBind:Warning: 97.69 Failed to create target binding for binding Hello for -empty-
05-13 12:58:06.802 I/mono-stdout(  683): MvxBind:Warning: 97.69 Failed to create target binding for binding Hello for -empty-

我似乎无法比去年更多地发现这个问题。

为什么报告绑定失败并且按预期绑定?另外,为什么警告号码(8.57 / 9.69)会发生变化但却有相同的错误信息?

我也有解决方案中包含的默认LinkerPleaseInclude.cs文件,所以我现在对此感到困惑。

2 个答案:

答案 0 :(得分:2)

原来我的一个绑定声明不正确:

<强> HomeView.axml

<TextView
    ...
    local:MvxBind="Hello" />

应该是:

<TextView
    ...
    local:MvxBind="Text Hello" />

缺少Text类型。

答案 1 :(得分:0)

您需要设置要在Activity中使用的viewmodel。

更改此行: public class HomeView : MvxActivity 致:public class HomeView : MvxActivity<HomeViewModel>