gpt4 book ai didi

c# - xamarin 广播接收器访问 View 模型

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:45:10 24 4
gpt4 key购买 nike

注意事项:Xamarin 4.2.1.64,Visual Studio 2015 教授。

我已经创建了一个跨平台应用程序,可以在扫描条形码的 Android 设备上运行。

目前,当扫描时,软件有一个可选的输出模式(缓冲区、键盘、剪贴板和 Intent )。

目前正在使用键盘模式。

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

我需要改变的流程

用户单击扫描条形码的设备按钮,只有这一次设备设置为 Intent 和广播,我的应用程序接收者接收广播,从 Intent 中读取条形码并调用我的 View 模型以使用条形码更新字段。 View 模型现在将更改以检测字段更改并相应地运行该方法。

到目前为止的代码

可移植 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,但我正在学习很多东西,所以我意识到这实际上可能是一个简单的答案。

还有

我打算做的事情是否正确?接收条形码编号并访问 View 模型的“userinput”属性并更改它?我应该以某种方式访问​​ View 上的字段并更改它,然后允许我的属性更改方法执行业务逻辑吗?

最佳答案

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

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

// in your PCL
public interface IScanReceiver
{
}

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

// in your viewmodel
MessagingCenter.Subscribe<IScanReceiver>();

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

关于c# - xamarin 广播接收器访问 View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40786530/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com