从ViewModel设置条目的焦点

问题描述 投票:2回答:2

我已经阅读过关于消息,触发器,行为等等......对于我想要完成的事情,所有这些似乎都有些过分。

我在xaml中有一个重复的数据输入屏幕,它有1个选择器,2个条目和1个按钮。选择一个值后,选择器将保留该选择。第一个条目与选择器相同。第二个条目是总是获得新值的条目。

我想在点击按钮时收集填充的值,然后清除其数据的最后一个输入字段并将焦点放回该条目,以便用户输入新值并点击保存。重复重复等

我理解MVVM模型和理论 - 但我只是想把重点放在xaml视图中的一个输入字段,我完全被难倒了。

编辑以添加代码示例

view.xaml:

<StackLayout Spacing="5" 
             Padding="10,10,10,0">
    <Picker x:Name="Direction" 
             Title="Select Direction" 
             ItemsSource="{Binding Directions}"
             ItemDisplayBinding="{Binding Name}"
             SelectedItem="{Binding SelectedDirection}"/>
    <Label Text="Order"/>
    <Entry Text="{Binding Order}" 
           x:Name="Order" />
    <Label Text="Rack"/>
    <Entry Text="{Binding Rack}" 
           x:Name="Rack" />
    <Button Text="Save" 
            Style="{StaticResource Button_Primary}" 
            Command="{Binding SaveCommand}"
            CommandParameter="x:Reference Rack" />
    <Label Text="{Binding Summary}"/>
</StackLayout>

viewmodel.cs

    public ICommand SaveCommand => new DelegateCommand<View>(PerformSave);

    private async void PerformSave(View view)
    {
        var scan = new Scan()
        {
            ScanType = "Rack",
            Direction = SelectedDirection.Name,
            AreaId = 0,
            InsertDateTime = DateTime.Now,
            ReasonId = 0,
            ScanItem = Rack,
            OrderNumber = Order,
            ScanQty = SelectedDirection.Value,
            IsUploaded = false
        };

        var retVal = _scanService.Insert(scan);

        if (!retVal)
        {
            await _pageDialogService.DisplayAlertAsync("Error", "Something went wrong.", "OK");
        }
        else
        {
            view?.Focus();
            Rack = string.Empty;
            Summary = "last scan was great";
        }
    }

错误显示在此部分中:

private void InitializeComponent() {
        global::Xamarin.Forms.Xaml.Extensions.LoadFromXaml(this, typeof(RackPage));
        Direction = global::Xamarin.Forms.NameScopeExtensions.FindByName<global::Xamarin.Forms.Picker>(this, "Direction");
        Order = global::Xamarin.Forms.NameScopeExtensions.FindByName<global::Xamarin.Forms.Entry>(this, "Order");
        Rack = global::Xamarin.Forms.NameScopeExtensions.FindByName<global::Xamarin.Forms.Entry>(this, "Rack");
    }
xamarin xamarin.forms prism
2个回答
1
投票

您可以将Entry作为View参数发送到视图模型的命令。像那样:

public YourViewModel() 
{
    ButtonCommand = new Command<View>((view) => 
    {
        // ... Your button clicked stuff ...
        view?.Focus();
    });
}

从XAML你这样称呼:

<Entry x:Name="SecondEntry" 
       ... Entry properties ...
       />
<Button Text="Click Me"
        Command="{Binding ButtonCommand}"
        CommandParameter="{Reference SecondEntry}"/>

我希望它有所帮助。


0
投票

不确定您是否可以从XAML设置条目的焦点,但是从页面中,您可以在Clicked事件上使用FocusEntry函数:

saveButton.Clicked += async (s, args) =>
{
    //save the data you need here
    //clear the entries/picker
    yourEntry.Focus();
};
© www.soinside.com 2019 - 2024. All rights reserved.