微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有没有办法将一个 Xamarin Forms 控件与另一个“关联”?

如何解决有没有办法将一个 Xamarin Forms 控件与另一个“关联”?

我有一个表单,其中包含在 Xaml 中定义的三个“对”{Entry,Button}。每个按钮都旨在对其相应的条目进行操作。 Xaml 中有没有办法将每个条目与其按钮“关联”?我试图避免必须使用三种几乎相同的方法

OnButtonAClicked()
{
    act on Entry A
}
OnButtonBClicked()
{
   act on Entry B
}
OnButtoncclicked()
{
    act on Entry C
}

我想要一种方法

OnButtonClicked()
{
    act on my associated Entry
}

有什么好的办法吗?

谢谢。

解决方法

使用绑定(还没有测试过,认为它会起作用)

<Entry x:Name="EntryA" ... .>

<Button BindingContext="{x:Reference Name=EntryA}" Clicked="OnButtonClicked" ... />

<Entry x:Name="EntryB" ... .>

<Button BindingContext="{x:Reference Name=EntryB}" Clicked="OnButtonClicked" ... />

然后

OnButtonCClicked(object sendrer,EventArgs args)
{
    Button btn = (Button)sender;

    Entry entry = (Entry)btn.BindingContext;

    // entry should be a reference to the appropriate entry
}
,

您可以使用 #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct node { char word[20]; struct node *next; } node; void print (node n); node *table[1]; int main(void) { // TODO char word[20]; FILE *file = fopen("list","r"); node *first = malloc(sizeof(node)); table[0] = first; if (file != NULL) { while (fscanf(file,"%s",word) != EOF) { node *entry = malloc(sizeof(node)); if (entry != NULL) { strcpy (entry->word,word); entry->next = first->next; first->next = entry; } } } print(*first); } void print (node n) { while(n != NULL) { printf("%s\n",n.word); print(*n.next); } } Command 代替 CommandParameter 事件,这样您就可以将相应的 Entry 控件作为参数发送:

Clicked

在您的 ViewModel 或代码隐藏中:

        <Entry x:Name="entry1"/>
        <Entry x:Name="entry2"/>
        <Button Command="{Binding ClickCommand}"
                CommandParameter="{Binding Source={x:Reference entry1}}"/>
        <Button Command="{Binding ClickCommand}"
                CommandParameter="{Binding Source={x:Reference entry2}}"/>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。