【问题标题】:xamarin broadcast receiver access viewmodelxamarin 广播接收器访问视图模型
【发布时间】: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 我应该以某种方式访问​​视图上的字段并更改它,然后允许我的属性更改方法执行业务逻辑吗?

【问题讨论】:

    标签: c# android mvvm xamarin


    【解决方案1】:

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

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

    // in your PCL
    public interface IScanReceiver
    {
    }
    
    // in your android project
    public class Receiver1 : BroadcastReceiver, IScanReceiver
    
    // in your viewmodel
    MessagingCenter.Subscribe<IScanReceiver>();
    

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

    【讨论】:

    • 愚蠢的问题1,当你说PCL时,你指的是什么?
    • 您的可移植类库。假设您的 XAML 所在的位置。如果您使用的是共享库,那么这与我所说的完全一样。无论您的 ViewModel 和 View 在哪里。
    • 我将在 Xamarin 论坛上从 Craig Dunn 那里窃取一个示例,但它应该有助于解释一下。在此示例中,它使用 Activity 将消息发送到 Xamarin.Forms 页面,但您可以使用 Receiver 将消息发送到您的 ViewModel 或页面:forums.xamarin.com/discussion/26681/…
    • 好的,所以我创建了接口,添加了一个名为 Result {get;set:} 的属性字符串。我的接收者暗示了这个属性,然后我说 Result = decodedBarcode;很好,但我又如何以及在哪里使用我的视图模型中的 messagecenter 方法?
    • 克雷格邓恩的例子,所以基本上在我的接收器中创建一个发送字符串的消息中心,然后让我的 ScannerPage 类订阅消息?据我了解,如果我将字段设置为 = 结果,我的 ViewModel 中的 OnProperty Chnaged 方法应该被击中吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多