Xamarin设置焦点MVVM

Xamarin设置焦点MVVM,xamarin,Xamarin,在Xamarin中使用可移植项目 可移植项目目录中的ScannerPage.xaml文件 可移植目录中的MainViewModel.cs类 namespace StockCheckApp.ViewModels { 公共类MainViewModel:INotifyPropertyChanged { 私人清单(股票清单),; 私有字符串_userInput; 公开上市股票 { 获取{return\u stockList;} 设置 { _存货清单=价值; OnPropertyChanged();

在Xamarin中使用可移植项目

可移植项目目录中的ScannerPage.xaml文件


可移植目录中的MainViewModel.cs类

namespace StockCheckApp.ViewModels
{
公共类MainViewModel:INotifyPropertyChanged
{
私人清单(股票清单),;
私有字符串_userInput;
公开上市股票
{
获取{return\u stockList;}
设置
{
_存货清单=价值;
OnPropertyChanged();
}
}
公共字符串用户输入
{
获取{return\u userInput;}
设置
{
_用户输入=值;
OnPropertyChanged();
}         
}
公共主视图模型()
{
}
公共命令后命令
{
得到
{
返回新命令(异步()=>
{
var stockService=new stockService();
StockList=await stockService.GetStockAsync(UserInput);
});
}
}
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
我需要做的是将
设置为在应用程序启动时选择,然后在单击按钮后再次选择

在我的示例中,这是如何实现的?

几乎没有变化。 1.在xaml中删除

 <ContentPage.BindingContext>
    <ViewModels:MainViewModel />
  </ContentPage.BindingContext>
  • MainViewModel文件

    public class MainViewModel : INotifyPropertyChanged
    {
        //private List<Stock> _stockList;
        private string _userInput;
        //public List<Stock> StockList
        //{
        //    get { return _stockList; }
        //    set
        //    {
        //        _stockList = value;
        //        OnPropertyChanged();
        //    }
        //}
    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(async () =>
            //{
            //    var stockService = new StockService();
            //    StockList = await stockService.GetStockAsync(UserInput);
            //});
            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));
    }
    
    public类主视图模型:INotifyPropertyChanged
    {
    //私人清单(股票清单),;
    私有字符串_userInput;
    //公开上市股票
    //{
    //获取{return\u stockList;}
    //设置
    //    {
    //_stockList=价值;
    //OnPropertyChanged();
    //    }
    //}
    扫描页面;
    公共主视图模型(扫描页面父级)
    {
    page=父级;
    }
    公共字符串用户输入
    {
    获取{return\u userInput;}
    设置
    {
    _用户输入=值;
    OnPropertyChanged();
    }
    }  
    公共命令后命令
    {
    得到
    {
    //返回新命令(异步()=>
    //{
    //var stockService=new stockService();
    //StockList=await stockService.GetStockAsync(UserInput);
    //});
    返回新命令(()=>
    {
    page.MyBox.Focus();//单击按钮时设置焦点
    });
    }
    }
    公共事件属性更改事件处理程序属性更改;
    受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
    {
    PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
    }
    
    }


  • 我注释掉了一些代码,只是为了能够运行它。您可以保留所有代码。

    目前无法正确测试,但我看到您所做的看起来不错。
    public ScannerPage()    
    {
        InitializeComponent();
        BindingContext = new MainViewModel(this);
    
    }
    
    protected override void OnAppearing()
    {
            base.OnAppearing();
            myBox.Focus(); //select when apprears
    }        
    public Entry MyBox
    {
            get
            {
                return myBox;
            }        
    }
    
    public class MainViewModel : INotifyPropertyChanged
    {
        //private List<Stock> _stockList;
        private string _userInput;
        //public List<Stock> StockList
        //{
        //    get { return _stockList; }
        //    set
        //    {
        //        _stockList = value;
        //        OnPropertyChanged();
        //    }
        //}
    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(async () =>
            //{
            //    var stockService = new StockService();
            //    StockList = await stockService.GetStockAsync(UserInput);
            //});
            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));
    }