xamarin 我无法检查复选框是否已更改

omhiaaxx  于 2023-04-18  发布在  其他
关注(0)|答案(1)|浏览(132)

我使用以下代码创建了一个复选框

<CheckBox Color="Red" CheckedChanged="{Binding OnCheckBoxCheckChanged}" >
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal">
                            <VisualState.Setters>
                                <Setter Property="Color"
                            Value="Red" />
                            </VisualState.Setters>
                        </VisualState>

                        <VisualState x:Name="IsChecked">
                            <VisualState.Setters>
                                <Setter Property="Color"
                            Value="Green" />
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
            </CheckBox>

然后,我创建了以下OnCheckBoxCheckChanged函数,该函数将绑定到复选框上(当前位于视图model.cs文件中):

void OnCheckBoxCheckedChanged(object sender, CheckedChangedEventArgs e)
        {

            if (e.Value.Equals(true))
            {
                VanData.PPECheck = true;
                _PPECheck = true;            }
            else
            {
                VanData.PPECheck = false;
                _PPECheck = false;
            }
        }

这里的错误是,在我的Visual Studio编辑器中,当创建复选框时尝试绑定时,它说“No DataContext”,当查看我的函数时,它说没有引用。我使用的是标准的Xamarin.Forms模板(选项卡视图),这是目前在“AddNewItem”页面。.xaml代码位于.xaml文件中显然。我在两个AddNewItem视图模型中都尝试过这个函数,将.cs文件附加到AddNewItem.xaml文件。我对Xaml相当陌生,所以不确定这个问题,但我相信绑定找不到我的函数?

感谢任何帮助,如果需要任何更多的信息,请让我知道。提前感谢!

ht4b089n

ht4b089n1#

如果要以MVVM方式编写代码,可以尝试将EventToCommandBehavior作为
安德鲁在评论中提到。
首先,我们应该安装Xamarin Community Toolkit Nuget
然后在xaml文件中,我们将Behaviors添加到CheckBox中。EventToCommandBehavior将CheckedChanged事件更改为CheckValueCommand(我们稍后将在viewmodel中定义)。它还将IsChecked属性值作为参数传递给CheckValueCommand

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         ......
         xmlns:xct="http://xamarin.com/schemas/2020/toolkit">

<CheckBox Color="Red" x:Name="myCheck">
       <VisualStateManager.VisualStateGroups>
           ......
       </VisualStateManager.VisualStateGroups>
   <CheckBox.Behaviors>
       <xct:EventToCommandBehavior
                   EventName="CheckedChanged"
                   CommandParameter="{Binding Source={x:Reference myCheck},Path=IsChecked}"
                   Command="{Binding CheckValueCommand}"/>
   </CheckBox.Behaviors>

在viewModel文件中,我们定义CheckValueCommand:

public Command CheckValueCommand
    {
        get
        {
            return new Command<bool>((e) =>
            {
                if (e)
                {
                    Console.WriteLine("true");
                }
                else
                {
                    Console.WriteLine("false");
                }
            });
        }
    }

有关详细信息,请参阅Xamarin Community Toolkit EventToCommandBehavior
希望能成功。

相关问题