我敢肯定,我在这里缺少一些深层或明显的概念:)
所以我现在有一个页面可以设置各种蓝牙传感器,并从心率监视器,速度表和节奏传感器中获取数据。 (使用plugin.ble)
因此,我在ViewModel中为一个称为bluetoothpage的内容page做所有这些。
我想在称为displaypage的其他内容page中显示我收到的数据。
我创建了一个可以保存我想要的数据的简单类(模型):
namespace TSDZ2Monitor.Models;
public partial class BluetoothData : ObservableObject
{
//Heart rate raw data
public int HRM { get; set; }
public double HRR { get; set; }
//SPD raw data
public int SPDWheelRevolutions { get; set; }
public double SPDWheelEventTime { get; set; }
//CAD raw data
public int CADCrankRevolutions { get; set; }
public double CADCrankEventTime { get; set; }
}
那么,如何将数据从蓝牙页面获取到显示页面?
我怀疑我需要根据我的模型使用对象,并在蓝牙ViewModel(Easy ... ish)中使用数据填充它?
但是,我的显示页面如何看到此数据发生?
当我尝试使用反应性的事情时,这种事情是一场噩梦(状态!)
,或者我在这里有点简单:大声笑
解决方法:我可以按照 https://learn.microsoft.com/en-us/learn/learn/dotnet-maui/store-local-data/2-compare-storage-options - 是一种进入这样做还是可以使用对象完成?
G.
编辑:我认为我也可以使用MessagingService https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/messagingcenterals/messagingcenter and https://codemilltech.com/messing-with-xamarin-forms-messaging-center/ 如果我能弄清楚如何在MVVM上下文中使用它们。
另外使用MessagingCenter有什么区别和标准.NET活动处理程序,以告知感兴趣的各方更改吗?
I'm sure that I'm missing some deep or obvious concept here :)
So I have a page now that can setup various Bluetooth sensors and get data from a heart rate monitor, speedometer and cadence sensor. (Using Plugin.BLE)
So I do all of that in a ViewModel for a ContentPage called BluetoothPage.
I want to display the data I get in a different ContentPage called DisplayPage.
I have created a simple class (model) that can hold the data I want:
namespace TSDZ2Monitor.Models;
public partial class BluetoothData : ObservableObject
{
//Heart rate raw data
public int HRM { get; set; }
public double HRR { get; set; }
//SPD raw data
public int SPDWheelRevolutions { get; set; }
public double SPDWheelEventTime { get; set; }
//CAD raw data
public int CADCrankRevolutions { get; set; }
public double CADCrankEventTime { get; set; }
}
So, how do I get the data from my Bluetooth page to my Display page?
I suspect I need to use an object based on my model and populate it with data in my Bluetooth viewmodel (easy...ish)?
But how can my Display page see this data as it happens?
When I tried working with ReactNative this sort of thing was a nightmare (State!)
Or am I being a bit simple in the head here :lol
Workaround: I could save the data to some local storage or sqlite as per https://learn.microsoft.com/en-us/learn/dotnet-maui/store-local-data/2-compare-storage-options - is that the way to do it, or can it be done with the object?
G.
Edit: I think I could also use the MessagingService https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/messagingcenter and https://codemilltech.com/messing-with-xamarin-forms-messaging-center/ if I can figure out how to use them in MVVM context.
Also What is the difference between using MessagingCenter and standard .NET event handlers for informing interested parties of changes?
发布评论
评论(2)
因此,使用消息中心似乎是一种方法。
遵循
我创建了一个消息标记类别:
仅此而已。
在我想从中发送对象的ViewModel中,我做了
btd是我创建的用于保存数据的类的实例:这是一个简化的模型:
在发送页面的ViewModel的构造函数中?)
的XAML中不使用此视图模型,
这在我的接收视图模型中
我还创建了蓝牙类别的实例,但这用于XAML绑定
和构造函数中在ViewModel中,我
效果很好,不知道对性能的影响可能是什么,但似乎反应迅速。
虽然我的思考方式是一个更具想法的解决方案,就是创建任何视图模型都可以访问的任何类的全局实例。
So it seems that using the MessagingCenter was a way to go.
Following the guidance in https://codemilltech.com/messing-with-xamarin-forms-messaging-center/
I created a MessagingMarker class:
That's all.
In the ViewModel where I wanted to send an object from, I did
where btd was an instance of a class I created to hold my data: Here is a simplified model:
In the constructor of the ViewModel for the sending page (probably best somewhere else?)
This is not used in the XAML for this ViewModel
In my receiving ViewModel
I also created an instance of the BluetoothData class, but this is used in the XAML bindings
and in the constructor of the ViewModel I had
Well it works, don't know what the impact on performance might be, but it seems pretty responsive.
To my way of thinking though a more idea solution is to create a global instance of any class that any ViewModel can access.
我还需要这样做 - 就我而言,参考
observableCollection< state>状态
来自多个页面。将其作为另一个可能的解决方案共享:我创建了一个使用
observableCollection
作为static> static
成员的类,该成员首次使用:对于使用集合的每个页面,其VM具有对类的实例的引用:
该页面对显示静态集合的引用:
I had need to also do this -- in my case, reference an
ObservableCollection<State> States
from multiple pages. Sharing this as another possible solution:I created a class with the
ObservableCollection
as astatic
member which is populated once upon first use:For each page that uses the collection, its VM has a reference to an instance of the class:
And the page has a reference to the static collection for display: