技术,框架和设备我正在使用:
- 框架:Xamarin.Forms
- IDE:Visual Studio 2022
- 物理设备(智能手机):Zebra TC26(Android 10)
- 物理设备(智能手表):Samsung Galaxy Watch4(Android Watch4) 11)
问题定义
目前我有一个测试Xamarin.Forms项目,该项目由两个不同的UI(XAML文件)组成:
- 用户界面1:homepage.xaml-应该在智能手机上显示此屏幕
- 用户界面2:watchscreen.xaml-该屏幕应显示在智能手表的
情况下,在下面的代码下,我确保将homepage.xaml部署到智能手机,然后将手表屏幕部署到智能手表:
Page homePage = new NavigationPage(new HomePage());
// BuildVersionCodes.R is a reference to Android version 11 (mostly now used by Wear OS 3.x)
if (Build.VERSION.SdkInt == BuildVersionCodes.R)
{
// SM-R870 is a reference to the Samsung Galaxy Watch4
// Note: This is needed to ensure the UI is specific to the UI of a smartwatch
if (Build.Model == "SM-R870")
{
Page watchScreen = new NavigationPage(new WatchScreen());
MainPage = watchScreen;
}
}
else
{
MainPage = homePage;
}
现在我想在不同的设备上使这些页面相互通信。 homepage.xaml存在于主Xamarin.Forms项目以及WatchScreen.XAML中。
我希望他们彼此交流的方式是发送消息或其他内容。 Xamarin.Forms项目也附带了一个本机项目。在这个本机Xamarin.android项目中,我尝试在主攻击中检索主项目中存在的按钮(在watchscreen.xaml中,此按钮存在,并且在watchscreen.xaml.cs中,我有一种方法可以放回此按钮)。
watchscreen.xaml.cs中给出按钮的方法:
public Button GetSendButtonFromWearableUI() => btnSendMessage;
在mainActivity.cs中,我会通过以下方法获取此方法:
Button button = (App.Current.MainPage.Navigation.NavigationStack.LastOrDefault() as WatchScreen)
.GetSendButtonFromWearableUI();
每当我通过此操作单击按钮时:
button.Clicked += delegate
{
SendData();
};
某些数据应从mainActivity.cs发送,并由homepage.xaml捕获并显示为在上面。我尝试了几种方法,但我没有成功实现需要发生的事情。
Technologies, frameworks and devices I'm using:
- Framework: Xamarin.Forms
- IDE: Visual Studio 2022
- Physical Device (smartphone): Zebra TC26 (Android 10)
- Physical Device (smartwatch): Samsung Galaxy Watch4 (Android 11)
Problem definition
Currently I have a test Xamarin.Forms project that consists of two different UIs (XAML files):
- User Interface 1: HomePage.XAML - This screen should be displayed on the smartphone
- User Interface 2: WatchScreen.XAML - This screen should be displayed on the smartwatch
With code below I make sure HomePage.XAML is deployed to a smartphone and watchscreen is deployed to a smartwatch:
Page homePage = new NavigationPage(new HomePage());
// BuildVersionCodes.R is a reference to Android version 11 (mostly now used by Wear OS 3.x)
if (Build.VERSION.SdkInt == BuildVersionCodes.R)
{
// SM-R870 is a reference to the Samsung Galaxy Watch4
// Note: This is needed to ensure the UI is specific to the UI of a smartwatch
if (Build.Model == "SM-R870")
{
Page watchScreen = new NavigationPage(new WatchScreen());
MainPage = watchScreen;
}
}
else
{
MainPage = homePage;
}
Now I want to make these pages on different devices communicate with each other. HomePage.xaml exists within the main Xamarin.Forms project as well as WatchScreen.xaml.
The way I want them to communicate with each other is by sending a message or something. A Xamarin.Forms project also comes with a native project. In this native Xamarin.Android project I try to retrieve inside the MainActivity.cs the button that exists within the main project by using (in WatchScreen.xaml this button exists and in WatchScreen.xaml.cs I have a method that gives this button back).
Method in WatchScreen.xaml.cs that gives button back:
public Button GetSendButtonFromWearableUI() => btnSendMessage;
In MainActivity.cs I get this method by using:
Button button = (App.Current.MainPage.Navigation.NavigationStack.LastOrDefault() as WatchScreen)
.GetSendButtonFromWearableUI();
Whenever I click on the button by doing this:
button.Clicked += delegate
{
SendData();
};
Some data should be sent from MainActivity.cs and catched by HomePage.xaml and displayed on it. I tried several approaches but I didn't succeed in achieving what needs to happen.. Therefore, I'm wondering if you guys could help me out with this and would be much appreciated.
发布评论
评论(1)
同时,我一直在调查这个问题,并提出了解决方案。请按照以下步骤获得相同的结果。为了使这项解决方案工作,我将来自Microsoft的Google和Mendingcenter的可穿戴数据层API组合在一起。
另外,下面的示例仅显示从智能手表到智能手机的通信。为了反向进程,您可以将“发送”按钮放在主页上,而不是智能手表屏幕上,并确保订阅正确的消息。
最后一个注:请记住,下面使用的Google使用的代码已弃用,但仍然有效...
用于使这项工作的参考文献:
使用Android中的Xamarin 在Xamarin.android项目中安装依赖性
MessageKeys.cs
此类用于声明用于在设备之间发送和接收消息的消息键。
xamarin.forms(基本项目)-app.xaml.cs
在app.xaml.cs中,如前所述,我要确保可穿戴的UI显示器watchscreen.xaml和任何其他设备显示常规Android UI - > homepage.xaml。
Xamarin.Forms(基本项目)-WatchScreen.xaml.cs
将消息从可穿戴设备发送到Android智能手机。
xamarin.forms(基本项目) - homepage.xaml.cs
xamarin.forms(本机Android Project) - MainActivity.cs
在MainActivity.cs中我实现以下接口:
变量 :变量:
用于接收广播消息的内部类“ MessageReceiver”:
注册接收器(通过可穿戴数据层API接收),创建Google客户端并订阅smartwatch消息(通过MesidaggeCentErter检索消息)
ProcessMessage方法:发送从可耐磨性的消息发送到智能手机
senddata(),onstart(),onstop(),ondatachanged(对此部分没有任何作用,因为这是在项目之外接收消息,我现在不需要它),onconnected(),onconnectionsUspended( ),onConnectionFailed():
请参阅参考以查看使用了哪些代码,因为代码完全相同... ps:senddata的一件事已更改。如果要继续发送数据,请在尝试捕获块后终于从最终删除“ client.disconenct()”。
Xamarin.forms(本机Android项目) - 可穿戴式服务器服务的可穿戴式服务器:
DearableService是一个新类,并在本机项目中创建。同样,有关此部分,请参阅参考,因为它是我项目中使用的完全相同的代码。
为了全面概述正在发生的事情,我在下图中对此进行了可视化:(例如,显示了从智能手表到智能手机的通信工作方式)
如果您想从智能手机通信到智能手表,则可以做像这样的东西:
是的。现在,您将使用可穿戴数据层API和MesdagingCenter在同一应用程序中接收消息。我们只使用单独的UI来实现这一点,而不是进行单独的项目。
In the meantime I've been investigating this issue and came up with a solution. Follow steps below to get the same result. To make this solution work I've combined the Wearable Data Layer API from Google and MessagingCenter from Microsoft.
Also the example below shows only the communication from the smartwatch to the smartphone. In order to reverse processes you can put the send button on the HomePage instead of the smartwatch screen and make sure to subscribe to the correct messages.
One last note: keep in mind that code used below from Google is deprecated but it still works...
References used to make this work:
Syncing Data Between Wearable and Handheld Devices Using Xamarin in Android
Installed dependencies on the Xamarin.Android project within Xamarin.Forms project:
MessageKeys.cs
This class is used to declare message keys that are being used to send and receive messages between devices.
Xamarin.Forms (Base project) - App.xaml.cs
In the App.xaml.cs, as pointed out earlier, I'm making sure the wearable UI displays WatchScreen.xaml and any other devices display regular Android UI -> HomePage.xaml.
Xamarin.Forms (Base project) - WatchScreen.xaml.cs
Send message from Wearable device to Android smartphone.
Xamarin.Forms (Base project) - HomePage.xaml.cs
Xamarin.Forms (Native Android Project) - MainActivity.cs
Within MainActivity.cs I implement the following interfaces:
Variables:
Internal class 'MessageReceiver' for receiving broadcast messages:
Registering receiver (to receive through Wearable Data Layer API), creating Google Client and Subscribing to smartwatch message (to retrieve message through MessagingCenter)
ProcessMessage method: sends received message from wearable to smartphone
SendData(), OnStart(), OnStop(), OnDataChanged (didn't do anything with this part, because this is to receive messages outside the project and I don't need it for now), OnConnected(), OnConnectionSuspended(), OnConnectionFailed():
See the reference to see what code has been used, since code is exactly the same... P.S.: one thing for SendData has been changed. If you want to keep sending data, remove 'client.Disconenct()' from finally after the try and catch block.
Xamarin.Forms (Native Android Project) - WearableService inherits from WearableListenerService:
WearableService is a new class and created within the native project. Also for this part see the reference, because it's the exact same code being used within my project.
To get an overall overview of what's happening, I've visualized this in the diagram below: (example shows how communication works from smartwatch to smartphone)
If you want to communicate from smartphone to smartwatch, you could do something like this:
That's it guys. Now you will receive messages within the same application using the Wearable Data Layer API and MessagingCenter. Instead of having separate projects, we just use separate UIs to make this happen...