在Xamarin中在UI和代码模块之间传递值的最佳方法是什么?

huangapple go评论163阅读模式
英文:

Best way of passing values between UI and Code modules in Xamarin

问题

我正在创建一个跨平台的Xamarin应用程序,适用于Android和iOS。
该应用程序需要返回设备的位置信息(经度、纬度、地址的第一行)。
它还需要从Mifare Ultralight Fobs读取特定数据。
当提供一个“管理员Fob”时,该应用程序将切换到写入模式,允许用户为其他用户编程Fob。
我已经解决了基本问题,可以获取地址详细信息并读取/写入Fob的数据。

由于Xamarin不断发展,我想知道在UI和底层代码模块之间传递信息的最佳方法是什么。

似乎有以下几种方法:

  1. 使用静态变量
  2. 依赖注入
  3. 消息中心

我对Xamarin也非常陌生。

英文:

I am in the process of creating a cross platform app in Xamarin for Android and IOS.
The app needs to return the location of the device (Longitutue, Latitude, first line of address)
It also needs to read specific data from Mifare Ultralight Fobs.
When presented with an 'Admin Fob' the app will switch to Write Mode allowing the user to program Fobs for other users.
I have solved the basic issues in that I can get the address details and read / write data to a Fob.

As Xamarin is constantly evolving I was wanting to know what the best approach is to passing information back and forth between the UI and the underling code modules.

There seems to be

  1. use static variables
  2. Dependency Injection
  3. Messaging Centre

I am also very new to Xamarin.

答案1

得分: 1

静态变量不是 UI 值的好选择,除非你知道你在做什么。

最简单的方法就像 @MindSwipe 所说的 MVVM 模式一样。创建一个简单的 ViewModel,将其设置为 BindingContext,然后在 XAML 中使用绑定。它基本上与 WPF 相同,因此你可以使用许多适用于 Xamarin 的 WPF 指南。

这是一个简单的示例:

public class MyViewPage : ContentPage
{
  MyViewModel ViewModel = null;
  public MyViewPage(){
    this.BindingContext = ViewModel = new MyViewModel();
  }
}

ViewModel:

public class MyViewModel : INotifyPropertyChanged
 {
   private int _Counter = 0;
   public int Counter
   {
     get { return _Counter; }
     set
     {
       _Counter = value;
       OnPropertyChanged(nameof(Counter));
     }
   }
 }

在 XAML 中只需简单的绑定:

<Label Text="{Binding Counter}" />

默认情况下,绑定支持双向绑定,因此当你设置 Counter 时,UI 会立即更新。

英文:

Static Variables are not a good choice for UI values unless you know what you're doing.

The simplest approach is like @MindSwipe said the MVVM pattern. Create a simple ViewModel, set it as BindingContext and use Binding in XAML. Its mostly the same as WPF, so you can use many guides for WPF which works also for Xamarin.

Here is a simple example:

public class MyViewPage : ContentPage
{
  MyVieModel ViewModel = null;
  public MyViewPage(){
    this.BindingContext = ViewModel = new MyViewModel();
  }
}

The ViewModel:

public class MyViewModel : INotifyPropertyChanged
 {
   private int _Counter = 0;
   public int Counter
   {
     get { return _Counter; }
     set
     {
       _Counter = value;
       OnPropertyChanged(nameof(Counter));
     }
   }
 }

and on XAML just simple Binding:

&lt;Label Text=&quot;{Binding Counter}&quot; /&gt;

Binding works with Two-Way Binding by default, so when you set the Counter, the UI is updated imidiately.

huangapple
  • 本文由 发表于 2020年1月6日 20:21:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定