[英]Xamarin.Forms - Variable on multiple pages
I am working on a Xamarin.Forms application with BLE implentation.我正在开发一个带有 BLE 实现的 Xamarin.Forms 应用程序。 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.
仅 C# 实施需要数据,XAML 实施不需要数据。 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.我已经尝试过全局函数、getter 和 setter 以及委托,但还没有找到解决问题的方法。
Do any of you have an idea about this or even a solution?你们中有人对此有想法甚至解决方案吗? Thanks
谢谢
Delegates global functions (not private) getter & setter functions委托全局函数(非私有)getter 和 setter 函数
Here are THREE "static" techniques that are good to know about.这里有三种值得了解的“静态”技术。
1. XAML x:Static
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
2.
StaticResource
静态资源
<Label BackgroundColor="{StaticResource MyDarkColor}" />
In App.xaml:在 App.xaml 中:
<Application ...
<Application.Resources>
<Color x:Key="MyDarkColor">#112233</Color>
...
3. Instance property "proxy" of a static value. 3. 值为 static 的实例属性“proxy”。
This "trick" accesses a "static" via an "instance property".这个“技巧”通过“实例属性”访问“静态”。 Thus, it looks like any other property, to other code (and XAML).
因此,对于其他代码(和 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:如果 static 在不同的 class 中:
// In the class that `BindingContext` is set to:
public SomeType MyDarkColor => Class1.MyGlobalValue;
...
public class Class1
{
public static SomeType MyGlobalValue;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.