一个ListView,每行有一个Switch,如何将Switch与行中的项绑定?

时间:2016-09-21 08:06:10

标签: c# xamarin xamarin.forms

Xamarin.Forms 中: 我有一个 ListView ,其中填充了某些对象。在ListView的每行中,我还有开关,用户可以使用选择或取消选择该项。

我的问题是我无法弄清楚如何将ListView中的特定项目传递给Switch的切换事件,以便用户的偏好然后可以保存。

即。每个Switch如何知道它在ListView中与哪个对象共享?(在下面的代码中,我试图传递的对象是 Survey 对象。)

到目前为止我发现了什么: 我发现这个问题正是我所追求的,但我不太明白答案: https://forums.xamarin.com/discussion/64197/switch-inside-listview-get-listview-item-on-toggle

在上述帖子之后,我尝试了以下内容:

扩展Switch类:

public class ListSwitch : Switch
    {
        public static BindableProperty SurveyItemProperty = BindableProperty.Create(
            propertyName: "SurveyItem",
            returnType: typeof(Survey),
            declaringType: typeof(ListSwitch),
            defaultValue: null);

        public Survey SurveyItem
        {
            get { return (Survey)GetValue(SurveyItemProperty); }
            set { SetValue(SurveyItemProperty, value); }
        }
    }

将此添加到我的View codebehind:

        mySwitch.SetBinding(ListSwitch.SurveyItemProperty, "SurveyItem");

        mySwitch.Toggled += (sender, e) =>
        {
            var item = (Survey)((ListSwitch)sender).SurveyItem;
            _vm.WatchSurveyCommand.Execute(item);
        };

当我尝试这个时,我得到以下错误

System.NullReferenceException: Object reference not set to an instance of an object
  at ConsensusCloud.MainViewModel+<ExecuteWatchSurveyCommand>c__async1.MoveNext () [0x00033] in /Users/Lambros/Dropbox/XamarinProjects/ConsensusCloud/ConsensusCloud/ViewModels/MainViewModel.cs:65
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128
  at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00000] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:113
  at ConsensusCloud.MainViewModel+<>c__async6.MoveNext () [0x00021] in /Users/Lambros/Dropbox/XamarinProjects/ConsensusCloud/ConsensusCloud/ViewModels/MainViewModel.cs:57
  at at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/src/UIKit/UIApplication.cs:79
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/3426/6c3fee4d/source/xamarin-macios/src/UIKit/UIApplication.cs:63
  at ConsensusCloud.iOS.Application.Main (System.String[] args) [0x00008] in /Users/Lambros/Dropbox/XamarinProjects/ConsensusCloud/iOS/Main.cs:17

1 个答案:

答案 0 :(得分:0)

什么是&#34; SurveyItem&#34;?它必须是列表ItemsSource中包含的个别项上的公共属性。如果要访问当前项目,只需绑定到&#34;。&#34;,这是当前项目。

ListView中,用于项目的模板的绑定上下文是当前处理的项目。有关此主题的更多详细信息,请参阅documentation