c# - 在 MvvmCross 中使用 MvxGridView

标签 c# xamarin.android mvvmcross

我想要做的是在一个网格中显示多个项目的简单屏幕。这可以在 Android 上使用 GridView 轻松存档。

在 MvvmCross 中,我想我必须使用 MvxGridView 来归档它。

这是我的 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">
  <Mvx.MvxGridView
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     local:MvxBind="ItemsSource Items"
     local:MvxItemTemplate="@layout/itemview" />
</LinearLayout>

项目 View :

<?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"
    android:minWidth="25px"
    android:minHeight="25px">
    <TextView
        android:text="Text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
        local:MvxBind="Text Name" />
</LinearLayout>

我的 View 模型:

using System.Collections.ObjectModel;
using Cirrious.MvvmCross.ViewModels;

namespace GridViewBug.Core.ViewModels
{
    public class FirstViewModel
        : MvxViewModel
    {
        public FirstViewModel()
        {
            Items = new ObservableCollection<TestClass>();
            Items.Add(new TestClass { Name = "Test1" });
            Items.Add(new TestClass { Name = "Test2" });
        }

        private string _hello = "Hello MvvmCross";
        public string Hello
        {
            get { return _hello; }
            set { _hello = value; RaisePropertyChanged(() => Hello); }
        }

        public ObservableCollection<TestClass> Items { get; set; }
    }

    public class TestClass
    {
        public string Name { get; set; }
    }
}

下面是我运行此示例时发生的情况:

05-03 16:18:32.799 I/MvxBind ( 7960):   0.84 Problem seen during binding execution for from Items to ItemsSource - problem TargetInvocationException: Exception has been thrown by the target of an invocation.
05-03 16:18:32.799 I/MvxBind ( 7960):     at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
05-03 16:18:32.799 I/MvxBind ( 7960):   at System.Reflection.MonoProperty.SetValue (System.Object obj, System.Object value, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] index, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
05-03 16:18:32.799 I/MvxBind ( 7960):   at System.Reflection.PropertyInfo.SetValue (System.Object obj, System.Object value, System.Object[] index) [0x00000] in <filename unknown>:0 
05-03 16:18:32.799 I/MvxBind ( 7960):   at Cirrious.MvvmCross.Binding.Bindings.Target.MvxPropertyInfoTargetBinding.SetValue (System.Object value) [0x00000] in <filename unknown>:0 
05-03 16:18:32.799 I/MvxBind ( 7960):   at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource (Boolean isAvailable, System.Object value) [0x00000] in <filename unknown>:0 
05-03 16:18:32.799 I/MvxBind ( 7960): InnerException was UnsupportedOperationException: Exception of type 'Java.Lang.UnsupportedOperationException' was thrown.
05-03 16:18:32.799 I/MvxBind ( 7960):     at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (IntPtr jobject, IntPtr jclass, IntPtr jmethod) [0x00023] in /Users/builder/data/lanes/monodroid-lion-bs1/03814ac5/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:604 
05-03 16:18:32.799 I/MvxBind ( 7960):   at Android.Views.ViewGroup.RemoveAllViews () [0x00053] in /Users/builder/data/lanes/monodroid-lion-bs1/03814ac5/source/monodroid/src/Mono.Android/platforms/android-8/src/generated/Android.Views.ViewGroup.cs:2582 
05-03 16:18:32.799 I/MvxBind ( 7960):   at Cirrious.MvvmCross.Binding.Droid.Views.MvxViewGroupExtensions.Refill (Android.Views.ViewGroup viewGroup, IAdapter adapter) [0x00000] in <filename unknown>:0 
05-03 16:18:32.799 I/MvxBind ( 7960):   at Cirrious.MvvmCross.Binding.Droid.Views.MvxViewGroupExtensions.UpdateDataSetFromChange[MvxGridView] (Cirrious.MvvmCross.Binding.Droid.Views.MvxGridView viewGroup, System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs eventArgs) [0x00000] in <filename unknown>:0 
05-03 16:18:32.799 I/MvxBind ( 7960):   at Cirrious.MvvmCross.Binding.Droid.Views.MvxGridView.AdapterOnDataSetChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs eventArgs) [0x00000] in <filename unknown>:0 
05-03 16:18:32.799 I/MvxBind ( 7960):   at (wrapper delegate-invoke) <Module>:invoke_void__this___object_NotifyCollectionChangedEventArgs (object,System.Collections.Specialized.NotifyCollectionChangedEventArgs)
05-03 16:18:32.799 I/MvxBind ( 7960):   at Cirrious.MvvmCross.Binding.Droid.Views.MvxAdapterWithChangedEvent.NotifyDataSetChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00000] in <filename unknown>:0 
05-03 16:18:32.799 I/MvxBind ( 7960):   at Cirrious.MvvmCross.Binding.Droid.Views.MvxAdapterWithChangedEvent.NotifyDataSetChanged () [0x00000] in <filename unknown>:0 
05-03 16:18:32.799 I/MvxBind ( 7960):   at Cirrious.MvvmCross.Binding.Droid.Views.MvxAdapter.SetItemsSource (IEnumerable value) [0x00000] in <filename unknown>:0 
05-03 16:18:32.799 I/MvxBind ( 7960):   at Cirrious.MvvmCross.Binding.Droid.Views.MvxAdapter.set_ItemsSource (IEnumerable value) [0x00000] in <filename unknown>:0 
05-03 16:18:32.799 I/MvxBind ( 7960):   at Cirrious.MvvmCross.Binding.Droid.Views.MvxGridView.set_ItemsSource (IEnumerable value) [0x00000] in <filename unknown>:0 
05-03 16:18:32.799 I/MvxBind ( 7960):   at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
05-03 16:18:32.799 I/MvxBind ( 7960):   at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
05-03 16:18:32.799 I/MvxBind ( 7960):   --- End of managed exception stack trace ---
05-03 16:18:32.799 I/MvxBind ( 7960): java.lang.UnsupportedOperationException: removeAllViews() is not supported in AdapterView
05-03 16:18:32.799 I/MvxBind ( 7960):   at android.widget.AdapterView.removeAllViews(AdapterView.java:521)
05-03 16:18:32.799 I/MvxBind ( 7960):   at cirrious.mvvmcross.binding.droid.binders.MvxBindingLayoutInflatorFactory.n_onCreateView(Native Method)
05-03 16:18:32.799 I/MvxBind ( 7960):   at cirrious.mvvmcross.binding.droid.binders.MvxBindingLayoutInflatorFactory.onCreateView(MvxBindingLayoutInflatorFactory.java:29)
05-03 16:18:32.799 I/MvxBind ( 7960):   at android.view.LayoutInflater.createViewFromTag(Layo
05-03 16:18:32.799 I/mono-stdout( 7960): MvxBind:Error:  0.84 Problem seen during binding execution for from Items to ItemsSource - problem TargetInvocationException: Exception has been thrown by the target of an invocation.
05-03 16:18:32.799 I/mono-stdout( 7960):      at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
05-03 16:18:32.799 I/mono-stdout( 7960):   at System.Reflection.MonoProperty.SetValue (System.Object obj, System.Object value, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] index, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
05-03 16:18:32.799 I/mono-stdout( 7960):   at System.Reflection.PropertyInfo.SetValue (System.Object obj, System.Object value, System.Object[] index) [0x00000] in <filename unknown>:0 
05-03 16:18:32.799 I/mono-stdout( 7960):   at Cirrious.MvvmCross.Binding.Bindings.Target.MvxPropertyInfoTargetBinding.SetValue (System.Object value) [0x00000] in <filename unknown>:0 
05-03 16:18:32.809 I/mono-stdout( 7960):   at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource (Boolean isAvailable, System.Object value) [0x00000] in <filename unknown>:0 
05-03 16:18:32.809 I/mono-stdout( 7960): InnerException was UnsupportedOperationException: Exception of type 'Java.Lang.UnsupportedOperationException' was thrown.
05-03 16:18:32.809 I/mono-stdout( 7960):      at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (IntPtr jobject, IntPtr jclass, IntPtr jmethod) [0x00023] in /Users/builder/data/lanes/monodroid-lion-bs1/03814ac5/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:604 
05-03 16:18:32.809 I/mono-stdout( 7960):   at Android.Views.ViewGroup.RemoveAllViews () [0x00053] in /Users/builder/data/lanes/monodroid-lion-bs1/03814ac5/source/monodroid/src/Mono.Android/platforms/android-8/src/generated/Android.Views.ViewGroup.cs:2582 
05-03 16:18:32.809 I/mono-stdout( 7960):   at Cirrious.MvvmCross.Binding.Droid.Views.MvxViewGroupExtensions.Refill (Android.Views.ViewGroup viewGroup, IAdapter adapter) [0x00000] in <filename unknown>:0 
05-03 16:18:32.809 I/mono-stdout( 7960):   at Cirrious.MvvmCross.Binding.Droid.Views.MvxViewGroupExtensions.UpdateDataSetFromChange[MvxGridView] (Cirrious.MvvmCross.Binding.Droid.Views.MvxGridView viewGroup, System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs eventArgs) [0x00000] in <filename unknown>:0 
05-03 16:18:32.809 I/mono-stdout( 7960):   at Cirrious.MvvmCross.Binding.Droid.Views.MvxGridView.AdapterOnDataSetChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs eventArgs) [0x00000] in <filename unknown>:0 
05-03 16:18:32.809 I/mono-stdout( 7960):   at (wrapper delegate-invoke) <Module>:invoke_void__this___object_NotifyCollectionChangedEventArgs (object,System.Collections.Specialized.NotifyCollectionChangedEventArgs)
05-03 16:18:32.809 I/mono-stdout( 7960):   at Cirrious.MvvmCross.Binding.Droid.Views.MvxAdapterWithChangedEvent.NotifyDataSetChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00000] in <filename unknown>:0 
05-03 16:18:32.809 I/mono-stdout( 7960):   at Cirrious.MvvmCross.Binding.Droid.Views.MvxAdapterWithChangedEvent.NotifyDataSetChanged () [0x00000] in <filename unknown>:0 
05-03 16:18:32.809 I/mono-stdout( 7960):   at Cirrious.MvvmCross.Binding.Droid.Views.MvxAdapter.SetItemsSource (IEnumerable value) [0x00000] in <filename unknown>:0 
05-03 16:18:32.809 I/mono-stdout( 7960):   at Cirrious.MvvmCross.Binding.Droid.Views.MvxAdapter.set_ItemsSource (IEnumerable value) [0x00000] in <filename unknown>:0 
05-03 16:18:32.809 I/mono-stdout( 7960):   at Cirrious.MvvmCross.Binding.Droid.Views.MvxGridView.set_ItemsSource (IEnumerable value) [0x00000] in <filename unknown>:0 
05-03 16:18:32.809 I/mono-stdout( 7960):   at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
05-03 16:18:32.809 I/mono-stdout( 7960):   at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
05-03 16:18:32.809 I/mono-stdout( 7960):   --- End of managed exception stack trace ---
05-03 16:18:32.809 I/mono-stdout( 7960): java.lang.UnsupportedOperationException: removeAllViews() is not supported in AdapterView
05-03 16:18:32.809 I/mono-stdout( 7960):    at android.widget.AdapterView.removeAllViews(AdapterView.java:521)
05-03 16:18:32.809 I/mono-stdout( 7960):    at cirrious.mvvmcross.binding.droid.binders.MvxBindingLayoutInflatorFactory.n_onCreateView(Native Method)
05-03 16:18:32.809 I/mono-stdout( 7960):    at cirrious.mvvmcross.binding.droid.binders.MvxBindingLayoutInflatorFactory.onCreateView(MvxBindingLayoutInflatorFactory.java:29)
05-03 16:18:32.809 I/mono-stdout( 7960):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
05-03 16:18:32.809 I/mono-stdout( 7960):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
05-03 16:18:32.809 I/mono-stdout( 7960):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-03 16:18:32.809 I/mono-stdout( 7960):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-03 16:18:32.809 I/mono-stdout( 7960):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-03 16:18:32.809 I/mono-stdout( 7960):    at gridviewbug.droid.views.FirstView.n_onCreate(Native Method)
05-03 16:18:32.809 I/mono-stdout( 7960):    at gridviewbug.droid.views.FirstView.onCreate(FirstView.java:28)
05-03 16:18:32.809 I/mono-stdout( 7960):    at android.app.Activity.performCreate(Activity.java:4465)
05-03 16:18:32.809 I/mono-stdout( 7960):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-03 16:18:32.809 I/mono-stdout( 7960):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-03 16:18:32.809 I/mono-stdout( 7960):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-03 16:18:32.809 I/mono-stdout( 7960):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-03 16:18:32.809 I/mono-stdout( 7960):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-03 16:18:32.809 I/mono-stdout( 7960):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 16:18:32.809 I/mono-stdout( 7960):    at android.os.Looper.loop(Looper.java:137)
05-03 16:18:32.809 I/mono-stdout( 7960):    at android.app.ActivityThread.main(ActivityThread.java:4424)
05-03 16:18:32.809 I/mono-stdout( 7960):    at java.lang.reflect.Method.invokeNative(Native Method)
05-03 16:18:32.809 I/mono-stdout( 7960):    at java.lang.reflect.Method.invoke(Method.java:511)
05-03 16:18:32.809 I/mono-stdout( 7960):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-03 16:18:32.809 I/mono-stdout( 7960):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-03 16:18:32.809 I/mono-stdout( 7960):    at dalvik.system.NativeStart.main(Native Method)

我做错了什么? 我正在使用我可以从 Nuget 获得的最新内容。在 VS2012 中运行在 Mono Android 4.6.4

这是包含完整代码的 repo 协议(protocol): https://github.com/sschoeb/MvvmCrossGridViewProblem

最佳答案

从 tracem 来看,它看起来像是一个直接的错误,可能是在修复另一个问题时某处发生的回归。

最好的办法是在 GitHub 上为此开一个问题 - https://github.com/slodge/MvvmCross/issues/new ?或者您可以 fork 源代码并在您的分支中修复它。


与此同时,也许尝试使用 MvxListView?或者创建一个新的可绑定(bind) ViewGroup?

关于c# - 在 MvvmCross 中使用 MvxGridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16364197/

相关文章:

android - 如何使用 MVVMCross 将 Fragments 集成到 ViewModels 中

java - 解决许多proguard警告

android - 如何获取 Android_Accelerated_x86 模拟器?

java - Java 版本的 ApkSignerTool 问题

c# - MvvmCross 可移植类库 - System.Net.ServicePointManager

xamarin - MvxAdapter 程序化绑定(bind)

c# - Antlr 生成的类对内部的访问修饰符

c# - 如何避免包含文件的完整路径

c# - 管理 CheckedListBox ItemCheck 事件以在检查项目之后而不是之前运行

c# - 如何一劳永逸地从 VS Team Services 构建到 Azure Web 应用程序?