Xamarin集焦点MVVM

uemypmqf  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(132)

在Xamarin中使用Portable项目。
Portable项目目录中的ScannerPage.xaml文件

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="StockCheckApp.Views.ScannerPage"
         xmlns:ViewModels="clr-namespace:StockCheckApp.ViewModels;assembly=StockCheckApp"
         FocusManager.FocusedElement="{Binding ElementName=}">
    <Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" />
    <ContentPage.BindingContext>
        <ViewModels:MainViewModel />
    </ContentPage.BindingContext>

    <StackLayout Orientation="Vertical">
        <Entry Text="{Binding UserInput, Mode=TwoWay}" />
        <Button Text="Check Stock" Command="{Binding PostCommand}"/>

        <ListView ItemsSource="{Binding StockList}" HasUnevenRows="True">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Vertical" Padding="12,6">
                            <Label Text="{Binding St_Code}" FontSize="24" />
                            <Label Text="{Binding St_Desc}" />
                            <Label Text="{Binding Sl_Loc}" />
                            <Label Text="{Binding Sa_Datetime}" />
                            <Label Text="{Binding Sa_Qty}" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout> 
</ContentPage>

Portable目录中的MainViewModel.cs类。

namespace StockCheckApp.ViewModels
{
    public class MainViewModel : INotifyPropertyChanged
    {
        private List<Stock> _stockList;
        private string _userInput;
        public List<Stock> StockList
        {
            get { return _stockList; }
            set
            {
                _stockList = value;
                OnPropertyChanged();
            }
        }

        public string UserInput
        {
            get { return _userInput; }
            set
            {
                _userInput = value;
                OnPropertyChanged();
            }         
        }

        public MainViewModel()
        {
        }

        public Command PostCommand
        {
            get
            {
                return new Command(async () =>
                {
                    var stockService = new StockService();
                    StockList = await stockService.GetStockAsync(UserInput);
                });
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

我需要做的是将<Entry Text="{Binding UserInput, Mode=TwoWay}" />设置为在应用程序启动时选中,并在单击按钮后再次选中。
在我的例子中,这是如何实现的?

sirbozc5

sirbozc51#

没什么变化。
1.在XAML中删除

<ContentPage.BindingContext>
    <ViewModels:MainViewModel />
</ContentPage.BindingContext>

稍后您将需要一个参数到MainViewModel的构造函数。
1.给你的盒子给予个名字:

<Entry x:Name="myBox" Text="{Binding UserInput, Mode=TwoWay}" />
  1. ScannerPage文件背后的代码
public ScannerPage()    
{
    InitializeComponent();
    BindingContext = new MainViewModel(this);
}
        
protected override void OnAppearing()
{
    base.OnAppearing();
    myBox.Focus(); //select when apprears
}        

public Entry MyBox
{
    get
    {
        return myBox;
    }        
}
  1. MainViewModel文件
public class MainViewModel : INotifyPropertyChanged
{
    private string _userInput;

    ScannerPage page;
    
    public MainViewModel(ScannerPage parent)
    {
        page = parent;
    }
    
    public string UserInput
    {
        get { return _userInput; }
        set
        {
            _userInput = value;
            OnPropertyChanged();
        }
    }  
    
    public Command PostCommand
    {
        get
        {
            return new Command( () =>
            {
                page.MyBox.Focus(); //set focus when button clicked
            });
        }
    }
     
    public event PropertyChangedEventHandler PropertyChanged;
    
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

相关问题