【问题标题】:Xamarin binding EventHandlerXamarin 绑定 EventHandler
【发布时间】:2019-02-09 13:22:26
【问题描述】:

如何将事件处理程序从 xaml 绑定到 ViewModel 在我的 xamal 中:

<Entry Text="{Binding SecondName , Mode=TwoWay}"  Focused="{Binding FocusedEventhandler}" Completed="{Binding Compleated}">

如何在我的 viewModel 中获取该事件处理程序操作?

【问题讨论】:

  • 您在寻找FocusedCompleted 的哪个活动?
  • 你需要做的是把你的事件变成一个视图模型可以连接的命令。有几个例子。以here 为例。
  • @Arvindraja 我正在寻找两者

标签: xamarin mvvm xamarin.forms data-binding


【解决方案1】:

试试这个方法

public class TestViewModel 
{    
    public ICommand FocusedEventhandler { get; private set; }   
    public TestViewModel()
    {     
        FocusedEventhandler = new Command(async () => await ShowContactList());   
    }  

    async Task ShowContactList()
    {   
        //your code here
    }   
}

从您的页面调用您的 ViewModel

public partial class TestPage: ContentPage {  
public AddContact() {  
    InitializeComponent();  
    BindingContext = new TestViewModel();  
}  

这只是概述,让您了解我们如何做到这一点。

【讨论】:

  • 感谢您的回复 Arvindraja,我尝试了相同的方法,但我得到了类似“未处理的异常:Xamarin.Forms.Xaml.XamlParseException:位置 16:95。未找到或没有聚焦属性一个可访问的吸气剂”
  • 这是我的方式&lt;Entry Text="{Binding SecondName , Mode=TwoWay}" Placeholder="Enter your second name" Focused="{Binding FocusedEventhandler}"&gt; &lt;/Entry&gt;
  • 这里是viewmodel public ICommand FocusedEventhandler { get; private set; } async Task ShowContactList() { //your code here } public MainPageViewModel() { InitializeCommands(); } private void InitializeCommands() { TextChanging = new Command(() =&gt; { EntryTextChanging(); }); FocusedEventhandler = new Command(async () =&gt; await ShowContactList()); }
  • 文件很大,无法发布
  • 只是做一些研发。有很多关于相同的文章。除了指导您之外,没有人猜测您的确切问题。 :)
猜你喜欢
  • 2012-07-24
  • 1970-01-01
  • 1970-01-01
  • 2014-10-30
  • 2021-04-05
  • 1970-01-01
  • 1970-01-01
  • 2019-03-19
  • 2017-02-09
相关资源
最近更新 更多