Xamarin 和 Raspberry Pi

Xamarin and Raspberry Pi

我第一次开始学习 Xamarin,希望能得到正确的指导。目前我可以编写 ASP.Net MVC 5 应用程序。我有兴趣与 Raspberry Pi 用于 IOS 和 Android 设备进行通信,根据我在网上阅读的内容,Mono 框架是实现此目的的最佳方法。如果我错了,请纠正我。

我的问题是,我是否可以使用 Xamarin Forms 而不是 Xamarin Native UI,如果 Mono Framework 是我可以用 Xamarin Forms 实现的东西,还是我需要做的完全独立的事情而不是 Xamarin Forms .我希望这不会太混乱,但我只是想了解什么与什么一起工作,以便我可以为自己创建一个路线图。

Raspberry Pi (RPi) 可以 运行 Android 只有这样你才能利用 Xamarin 提供的功能。在这种情况下,您将能够制作一个 Xamarin.Android 应用程序并在 RPi 上 运行 它。

但是,您想 运行 RPi 上的某种服务器并与 iOS 或 Android 设备上的应用程序通信,这将 运行 Xamarin 应用程序。 在这里,这个应用程序是否使用 Xamarin.Forms 并不重要。

服务器端

您可以在 Raspberry Pi 服务器端做什么,这对您来说可能是最简单的。就是下载安装Windows10 IoT Core。然后你可以 运行 一个 ASP.NET WebAPI 或 MVC 应用程序。

或者,您可以在 RPi 上 Raspbian 或任何其他基于 Linux 的发行版 运行 上执行此操作,只需使用 .NET Core 即可。

这两种解决方案都可以让您在服务器和客户端之间共享序列化契约。

Phone 应用端

在 phone 上,您只会有一个客户端与 RPi 上的服务器通信。有几篇关于使用 Xamarin 编写弹性 API 客户端的非常好的文章(无论您是否使用 Forms 都无所谓)。

这是个人喜好,但我会使用 Refit to define the API for the server. Along with Polly 来重试或熔断失败的请求。我不隶属于任何一个。

最后,应用程序或服务器上的 运行 并不重要,它们是两个独立的实体,您最有可能共享的唯一内容是您的数据合同正在交换。

我使用 Raspberry Pi 作为我的主计算机。我使用 MonoDevelop 编写 C# 程序,并使用 mono 编写 运行 它们。我使用 System.Windows.Forms 作为我的 GUI。完成后,我有一个 .exe 可以在 Raspberry Pi 或 Windows 上 运行。我有一个 class 可以帮助我在 运行 时向表单添加控件。

 public static class ControlCreator
 {
    public static void Add(this Control.ControlCollection collection
    ,out GroupBox box,string id, string text, int left, int top
    , int width, int height)
    {
        box = new GroupBox();
        box.Text = text;
        AddControl (collection,box,id,left,top,width,height);
        return;
    }
    public static void Add(this Control.ControlCollection collection
    ,out Button box,string id, string text, int left, int top
    , int width, int height)
    {
        box = new Button();
        box.Text = text;
        AddControl (collection,box,id,left,top,width,height);
        return;
    }
    public static void Add(this Control.ControlCollection collection
    ,out Label box,string id, string text, int left, int top
    , int width, int height)
    {
        box = new Label();
        box.Text = text;
        AddControl (collection,box,id,left,top,width,height);
        return;
    }
    private static void AddControl(
    Control.ControlCollection collection,Control box,string id, int left
    , int top, int width, int height)
    {
        box.Name = id;
        box.Left = left;
        box.Top = top;
        box.Width = width;
        box.Height = height;
        collection.Add(box);
        return;
    }
}