首页 > 解决方案 > 如何在 ViewModel 中获取标签视图以在 xamarin 表单中设置可访问性焦点

问题描述

我有Label观点,我需要Label在我的ViewModel. 我正在使用 Dependency Service 将重点放在 Controls for Accessibility 服务上,DS 需要view作为参数。

这是我的标签

<Label
    AutomationProperties.IsInAccessibleTree="{Binding ShowNoResults}"
    IsVisible="{Binding ShowNoResults}"
    Text="{Binding ResultsHeader}"/>

我试过Command了,但 Label 不支持命令。下面的代码也不起作用

var view = GetView() as HomeworkView;

view总是越来越null。我怎样才能解决这个问题?

标签: xamarinxamarin.formsaccessibility

解决方案


我不太确定你想要实现什么,但你不能从你的视图模型中访问视图元素。

如果你想用控件做点什么,你可以使用消息中心来做,这里是一个例子

在您的视图模型中

MessagingCenter.Send(this, "your message here");

然后在您的页面中,您需要从该视图模型订阅此消息并执行所需的操作

MessagingCenter.Instance.Unsubscribe<ViewModelClassNamedel>(this, "your message here");    
MessagingCenter.Instance.Subscribe<ViewModelClassName>(this, "your message here", (data) =>
    {
        this.youControlName.Focus();
    });

推荐阅读