Xamarin.Forms -多个页面上的变量

q9rjltbz  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(166)

我正在开发一个带有BLE实现的Xamarin.Forms应用程序。在这个应用程序中,我想创建一个带有飞出结构的应用程序。然而,为此,应用程序的几个页面中需要一些变量/数据。这些数据只在C#实现中需要,而在XAML实现中不需要。在类/页面中,变量和数据的传输工作得很好。但是,在访问另一个页面时就不行了。
我已经尝试了全局函数、getter & setter以及委托,但还没有找到解决问题的方法。
你们中有没有人对此有什么想法,甚至是解决办法?谢谢
委托全局函数(非私有)getter和setter函数

v64noz0r

v64noz0r1#

Here are THREE "static" techniques that are good to know about.

1. XAML x:Static

...
xmlns:local="clr-namespace:MyNameSpace"
...

<Label Text="{x:Static local:MyClass.MyGlobalValue}" />
public class MyClass
{
  public static string MyGlobalValue;
}

2. XAML StaticResource

<Label BackgroundColor="{StaticResource MyDarkColor}" />

In App.xaml:

<Application ...
  <Application.Resources>
    <Color x:Key="MyDarkColor">#112233</Color>
...

3. Instance property "proxy" of a static value.

This "trick" accesses a "static" via an "instance property". Thus, it looks like any other property, to other code (and XAML).

<Label BackgroundColor="{Binding MyDarkColor}" />
// In the class that `BindingContext` is set to:
public static Color MyGlobalValue;

public Color MyDarkColor => MyGlobalValue;

IF the static is in a different class:

// In the class that `BindingContext` is set to:
public SomeType MyDarkColor => Class1.MyGlobalValue;

...
public class Class1
{
  public static SomeType MyGlobalValue;
}

相关问题