How to programmatically click a button in WinRT?

一笑奈何 提交于 2019-12-12 06:07:42

问题


How is this possible for WinRT?

I have read the answer for WPF How to programmatically click a button in WPF?, but does not really help me.

Anyone solved such an issue?


回答1:


First, I'd like to say if you want the button to "appear to have been pressed (in terms of animation/highlight effects) this won't help you but otherwise it should.

My Advice to you would be to follow the Model-View-ViewModel (MVVM) design pattern when designing your application if you haven't already. That way instead of calling the "button" click you can simply execute the method in your viewmodel that would normally be bound to that click.

Example:

You create a model class representing data in your database. You create a view (page/window) with buttons and other UI elements on it. You create a ViewModel class that has a series of public methods and collections.

Now in the XAML for the View, you bind the ViewModel as your DataContext and bind the public properties of the ViewModel to your collections (ItemSource for a ListBox being bound to an ObservableCollection is on example). You can create public methods that are "commands" and bind them your buttons so that when the button click event is fired, the command in the view model is executed. Now for all your unit tests and for any other reason you might want to programmatically "click" the button, you can simply call the associated methods in the ViewModel and never worry about what the actual View is doing.



来源:https://stackoverflow.com/questions/27605662/how-to-programmatically-click-a-button-in-winrt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!