xamarin - Xamarin 自定义控制命令/CommandParameter 问题
问题描述
一段时间以来,我一直致力于创建自定义控件。经过几次迭代后,我得出的结论是我遇到了绑定问题......事实上,当我将控件放入一个简单的 XAML 页面并执行该功能时,它工作得很好。但是,当我需要在单个页面上实例化多个控件时,即到集合、flexlayout、carouselview 中,Command 和 CommandParameter 绑定会丢失......并且不再发生 ViewModel 调用。
我的控制很简单......想一个复选框替换。我放置一个 1x1 网格,带有一个框架(用于轮廓)和一个标签来放置单个字符......“A”,“B”,“C”......“1”。“2”。“3”...无论您需要什么...我都有可绑定的属性...文本、TextColor、BorderColor、BackgroundColor 和“Selected”。
所以,现在我需要一个页面来问这个问题……“你觉得……随便……选择所有适用的。” 然后我提供了一个列表......带有数字或字母项目......用户可以选择无,任何或全部......所以我创建了一个包含一系列问题的视图,其中包含“可检查”项目的列表。 .. 正如我上面所说,如果控件在独立页面中,则可以完美运行...如果我动态生成这些控件的列表,Command 和 CommandParameter 突然不再起作用。
我的测试实现如下所示......虽然在这种情况下认为一些更简单的东西,比如“彩票”号码选择器。在这种情况下,ViewModel 将有一个简单的ObservableCollection<string> PrimaryControlList;
And,CommandParamter 将调用一个 VM 函数以及控件的文本,以跟踪用户选择的项目。
<Frame x:Name="primaryFrame">
<FlexLayout x:Name="flexPrimary" BindableLayout.ItemsSource="{Binding PrimaryControlList}" Wrap="Wrap" Direction="Row" JustifyContent="SpaceAround" AlignItems="Start" AlignContent="Start" >
<BindableLayout.ItemTemplate>
<DataTemplate>
<local:NumberSelect Text="{Binding .}" Command="Binding DoSomethingWithThis" CommandParameter="{Binding .}"/>
</DataTemplate>
</BindableLayout.ItemTemplate>
</FlexLayout>
</Frame>
任何人都可以提供指导吗?
解决方案
命令在你的ViewModel
while 当前BindingContext
的FlexLayout
is 中PrimaryControlList
。
解决方案:
首先,为您的 ContentPage 命名,让我们说MyPage
:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Name="MyPage"
x:Class="App266.MainPage">
假设您的页面绑定到 ViewModel 并且您的命令绑定应该是:
<local:NumberSelect Text="{Binding .}" Command="{Binding BindingContext.ButtonCommand, Source={x:Reference MyPage}}" CommandParameter="{Binding .}"/>
推荐阅读
- swiftui - SwiftUI 中的 Divider 可能存在的错误
- java - 使用 Cucumber 运行测试场景并生成 JVM 报告作为 Java 应用程序
- reactjs - 使用“npm start”和“npm run deploy”时反应页面不同
- javascript - 我怎样才能使它成为异步函数或承诺?
- python - ImportError:libcudart.so.10.1:无法打开共享对象文件:没有这样的文件或目录
- javascript - 循环使笔记应用程序中的笔记数量翻倍
- python - 通过函数调用在 numpy 中计算矩阵
- html - 如何使包含图像的 div 具有固定大小?
- pandas - 错误:删除索引值列表时“未在轴中找到”
- php - 如何在 Mac 上使用 php-fpm 配置 NGINX