xamarin广播接收器访问视图模型

lem*_*unk 4 c# android mvvm xamarin

注释: Xamarin 4.2.1.64,Visual Studio 2015 proff.

我创建了一个跨平台应用程序,可以在扫描条形码的Android设备上工作.

目前扫描时软件具有可选的输出模式,(缓冲区,键盘,剪贴板和意图).

目前使用键盘模式.

用户单击设备按钮扫描条形码,软件尝试转储到屏幕上的输入,如果不是它而不是选项卡(应用程序在启动时将焦点设置为输入字段).单击应用程序上的按钮时,它会调用我的服务查询一组数据并返回结果,然后更新结果列表供用户查看.

流程我需要改变

用户点击设备按钮扫描条形码,只有这次设备被设置为意图和广播,我的应用程序接收器接收广播,从意图中读取条形码并调用我的viewmodel以条形码更新字段.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>
Run Code Online (Sandbox Code Playgroud)

便携式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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

便携式主视图模型

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?
    }
}
Run Code Online (Sandbox Code Playgroud)

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
    }
}
Run Code Online (Sandbox Code Playgroud)

我坚持的是什么

我单步执行,我的代码点击我的断点,但在这个阶段我需要我的reciver以某种方式更新输入字段.

我还不熟悉Xamarin而且我正在学习很多,所以我意识到这可能是一个简单的答案.

我的意图是正确的吗?收到条形码编号并访问viewmodels"userinput"属性并更改它?我应该以某种方式访问​​视图上的字段并更改它然后允许我的属性更改方法来执行业务逻辑?

小智 6

您可以使用Xamarin.Forms附带的消息中心 https://developer.xamarin.com/guides/xamarin-forms/messaging-center/

将ViewModel Subscribe用于MessagingCenter以获取来自您的服务的事件,然后使用该事件更新绑定到该字段的属性.如果您需要PCL中的类型来映射订阅,请在PCL中为您的服务创建一个不需要任何实际方法合同的接口,然后让您的服务实现它,这样您就可以设置强大的订阅类型:

// in your PCL
public interface IScanReceiver
{
}

// in your android project
public class Receiver1 : BroadcastReceiver, IScanReceiver

// in your viewmodel
MessagingCenter.Subscribe<IScanReceiver>();
Run Code Online (Sandbox Code Playgroud)

或者,您可以在依赖项服务中设置ViewModel,然后使用Service Locator(反)模式查找ViewModel实例并更新绑定到该字段的属性.MVVM Light:http://www.mvvmlight.net/ 在为您提供工具方面做得非常出色.