gpt4 book ai didi

xamarin - 如何使 ViewCell 的 Tapped 事件将参数发送到通用函数,然后为该 ViewCell 元素打开一个选择器?

转载 作者:行者123 更新时间:2023-12-04 04:24:11 25 4
gpt4 key购买 nike

更新:只是提醒一下,如果有人可以告诉我如何在不使用手势的情况下实现此功能,则有 500 分奖励>

我正在使用 ViewCell 和手势识别器使用以下代码打开选择器。 ViewCell 在左侧有一个标签,在右侧有一个标签区域,最初在应用程序启动时填充,然后在单击 ViewCell 时使用选取器填充。

XAML

<ViewCell x:Name="ati" Tapped="OpenPickerCommand">
<Grid VerticalOptions="CenterAndExpand" Padding="20, 0">
<Grid.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding OpenPickerCommand}"
CommandParameter="{x:Reference atiPicker}" NumberOfTapsRequired="1" />
</Grid.GestureRecognizers>
<local:LabelBodyRendererClass Text="Answer Time Interval" HorizontalOptions="StartAndExpand" />
<Picker x:Name="atiPicker" IsVisible="false" HorizontalOptions="End" SelectedIndexChanged="atiPickerSelectedIndexChanged" ItemsSource="{Binding Times}"></Picker>
<local:LabelBodyRendererClass x:Name="atiLabel" HorizontalOptions="End"/>
</Grid>
</ViewCell>

<ViewCell x:Name="pti" Tapped="OpenPickerCommand">
<Grid VerticalOptions="CenterAndExpand" Padding="20, 0">
<Grid.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding OpenPickerCommand}"
CommandParameter="{x:Reference ptiPicker}" NumberOfTapsRequired="1" />
</Grid.GestureRecognizers>
<local:LabelBodyRendererClass Text="Phrase Time Interval" HorizontalOptions="StartAndExpand" />
<Picker x:Name="ptiPicker" IsVisible="false" HorizontalOptions="End" SelectedIndexChanged="ptiPickerSelectedIndexChanged" ItemsSource="{Binding Times}"></Picker>
<local:LabelBodyRendererClass x:Name="ptiLabel" HorizontalOptions="End"/>
</Grid>
</ViewCell>

C# 这适用于具有 CommandParameter 的不同选择器(ati、bti、pti 等)

public SettingsPage()
{
InitializeComponent();
BindingContext = new CommandViewModel();
}

void atiPickerSelectedIndexChanged(object sender, EventArgs e)
{
var picker = (Picker)sender;
int selectedIndex = picker.SelectedIndex;
if (selectedIndex != -1)
{
App.DB.UpdateIntSetting(Settings.Ati, selectedIndex);
atiLabel.Text = AS.ati.Text();
}
}

void ptiPickerSelectedIndexChanged(object sender, EventArgs e)
{
var picker = (Picker)sender;
int selectedIndex = picker.SelectedIndex;
if (selectedIndex != -1)
{
App.DB.UpdateIntSetting(Settings.Pti, selectedIndex);
ptiLabel.Text = AS.pti.Text();
}
}


public class CommandViewModel: ObservableProperty
{
public ICommand openPickerCommand;

public CommandViewModel()
{
openPickerCommand = new Command<Picker>(PickerFocus);
//openPickerCommand = new Command(tapped);
}

public ICommand OpenPickerCommand
{
get { return openPickerCommand; }
}

void PickerFocus(Picker param)
{
param.Focus();
}
}

我想删除 TapGestureRecognizers 的使用,但我仍然想保留功能和布局。

有人向我建议,如果我像这样使用 ViewCell 的 Tapped 事件会更好:
 Tapped="OnTapped"

有人可以详细解释一下我如何在 C# 中连接它。我最好在 CommandViewModel 和 C# 支持代码中编写一些代码。 View 模型也可以有一种方法来接受参数,以便它可以用来打开不同的选择器吗?

非常感谢我如何做到这一点的一个例子。请注意,如果有一种方法可以通过仅在 .cs 支持代码中进行编码来做到这一点,我就不需要特别使用 CommandViewModel。

最佳答案

您可以使用附加属性解决此问题。只需为添加命令/参数属性的 ViewCell 定义一个“行为”类。

public static class TappedCommandViewCell
{
private const string TappedCommand = "TappedCommand";
private const string TappedCommandParameter = "TappedCommandParameter";

public static readonly BindableProperty TappedCommandProperty =
BindableProperty.CreateAttached(
TappedCommand,
typeof(ICommand),
typeof(TappedCommandViewCell),
default(ICommand),
BindingMode.OneWay,
null,
PropertyChanged);

public static readonly BindableProperty TappedCommandParameterProperty =
BindableProperty.CreateAttached(
TappedCommandParameter,
typeof(object),
typeof(TappedCommandViewCell),
default(object),
BindingMode.OneWay,
null);

private static void PropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
if (bindable is ViewCell cell)
{
cell.Tapped -= ViewCellOnTapped;
cell.Tapped += ViewCellOnTapped;
}
}

private static void ViewCellOnTapped(object sender, EventArgs e)
{
if (sender is ViewCell cell && cell.IsEnabled)
{
var command = GetTappedCommand(cell);
var parameter = GetTappedCommandParameter(cell);

if (command != null && command.CanExecute(parameter))
{
command.Execute(parameter);
}
}
}

public static ICommand GetTappedCommand(BindableObject bindableObject) =>
(ICommand)bindableObject.GetValue(TappedCommandProperty);

public static void SetTappedCommand(BindableObject bindableObject, object value) =>
bindableObject.SetValue(TappedCommandProperty, value);

public static object GetTappedCommandParameter(BindableObject bindableObject) =>
bindableObject.GetValue(TappedCommandParameterProperty);

public static void SetTappedCommandParameter(BindableObject bindableObject, object value) =>
bindableObject.SetValue(TappedCommandParameterProperty, value);
}

之后在 XAML 中引用您的行为命名空间并使用完全限定名称指定属性值:

<ViewCell StyleId="disclosure-indicator"
behaviors:TappedCommandViewCell.TappedCommand="{Binding BrowseCommand}"
behaviors:TappedCommandViewCell.TappedCommandParameter="https://www.google.com">
<StackLayout Orientation="Horizontal">
<Label Text="Recipient"
VerticalOptions="Center"
Margin="20,0"/>
<Label Text="{Binding LedgerRecord.Recipient}"
HorizontalOptions="EndAndExpand"
VerticalOptions="Center"
Margin="0,0,20,0"/>
</Label>
</StackLayout>
</ViewCell>

以上将允许您使用 MVVM 并且没有点击手势识别器。

关于xamarin - 如何使 ViewCell 的 Tapped 事件将参数发送到通用函数,然后为该 ViewCell 元素打开一个选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45372174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com