简体   繁体   中英

Xamarin.Forms - Variable on multiple pages

I am working on a Xamarin.Forms application with BLE implentation. In the app I want to create an app with a flyout structure. For this, however, some of the variables/data are needed in several pages of the app. The data is only needed for the C# implementation and not for the XAML implementation. Within the class/page, the transfer of variables and data works great. However, not when accessing another page.

I have already tried global functions, getter & setter as well as delegates, but have not found out a solution to the problem.

Do any of you have an idea about this or even a solution? Thanks

Delegates global functions (not private) getter & setter functions

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM