【发布时间】:2017-04-08 18:21:51
【问题描述】:
注意事项: Xamarin 4.2.1.64,Visual Studio 2015 proff.
我创建了一个跨平台应用程序,可以在扫描条形码的 Android 设备上运行。
当前扫描软件时,有一个可选的输出模式(缓冲区、键盘、剪贴板和意图)。
当前使用键盘模式。
流程
用户单击扫描条形码的设备按钮,软件尝试转储到屏幕上的输入,如果不是,则改为选项卡(应用程序在启动时将焦点设置到输入字段)。当点击应用程序上的按钮时,它会调用我的服务来查询一组数据并返回结果,然后更新结果列表以供用户查看。
我需要改变流程
用户单击扫描条形码的设备按钮,只有这一次设备设置为意图并广播,我的应用接收器接收广播,从意图中读取条形码并调用我的视图模型以使用条形码更新字段。 viewmodel 现在将更改以检测字段更改并相应地运行该方法。
到目前为止的代码
便携式 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">
<Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" />
<StackLayout Orientation="Vertical">
<Entry x:Name="myBox" 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>
可移植的 Xaml 代码隐藏
public partial class ScannerPage : ContentPage
{
public ScannerPage()
{
InitializeComponent();
BindingContext = new MainViewModel(this);
}
protected override void OnAppearing()
{
base.OnAppearing();
myBox.Focus();
}
public Entry MyBox
{
get
{
return myBox;
}
}
}
便携式主视图模型
public class MainViewModel : INotifyPropertyChanged
{
ScannerPage page;
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(ScannerPage parent)
{
page = parent;
page.MyBox.Focus();
}
public Command PostCommand
{
get
{
return new Command(async () =>
{
var stockService = new StockService();
StockList = await stockService.GetStockAsync(UserInput);
page.MyBox.Focus();
});
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
//use this to clear? then reset focus?
}
}
Android 接收器类
[BroadcastReceiver(Enabled = true)]
[IntentFilter(new[] { "android.intent.action.bcr.newdata" })]
public class Receiver1 : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();
Services.StockService meh = new Services.StockService();
//MainViewModel md = new MainViewModel();
Dowork(meh, intent);
}
//this isnt correct i need to update viewmodel not call service direct!
private async void Dowork (Services.StockService meh, Intent intent)
{
string action = intent.Action.ToString();
string decodedBarcode = intent.GetStringExtra(BCRIntents.ExtraBcrString);
//now i just need to update my field in the xaml....somehow
}
}
我的困惑
我单步执行,我的代码遇到了断点,但在这个阶段我需要我的接收器以某种方式更新输入字段。
我还不熟悉 Xamarin,我正在学习很多,所以我意识到这实际上可能是一个简单的答案。
还有
我打算做什么是正确的吗?接收条形码编号并访问视图模型的“用户输入”属性并更改它? os 我应该以某种方式访问视图上的字段并更改它,然后允许我的属性更改方法执行业务逻辑吗?
【问题讨论】: