简体   繁体   中英

How can I share the value of a field between back-end C# and a renderer?

My C# looks like this:

public App()
{
   InitializeComponent();
   MainPage = new Japanese.MainPage();
}

public partial class MainPage : TabbedPage
{

    public MainPage()
    {
        InitializeComponent();

        var phrasesPage = new NavigationPage(new PhrasesPage())
        {
            Title = "Play",
            Icon = "ionicons-2-0-1-ios-play-outline-25.png"

        };

public partial class PhrasesPage : ContentPage
{
    public PhrasesFrame phrasesFrame;

    public PhrasesPage()
    {
        InitializeComponent();
        NavigationPage.SetHasNavigationBar(this, false);
        App.phrasesPage = this;
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        App.dataChange = true;
        phrasesFrame = new PhrasesFrame(this);
        phrasesStackLayout.Children.Add(phrasesFrame);
    }

public partial class PhrasesFrame : Frame
{

    private async Task ShowCard()
    {
        if (pauseCard == false) 
        ..

and I have an iOS renderer for a tab page

public class TabbedPageRenderer : TabbedRenderer
{
    private MainPage _page;

    private void OnTabBarReselected(object sender, UITabBarSelectionEventArgs e)
    {

        ...
            pauseCard = false;
        ...

My problem is there is no connection between the two and I would like to know how I can make it so that pauseCard could be set in one place and read in another.

Here is a simple custom Entry example using a bindable bool property that gets changed from the renderer every time the text changes in the entry.

Entry subclass w/ a bindable property called OnOff ( bool )

public class CustomPropertyEntry : Entry
{
    public static readonly BindableProperty OnOffProperty = BindableProperty.Create(
        propertyName: "OnOff",
        returnType: typeof(bool),
        declaringType: typeof(CustomPropertyEntry),
        defaultValue: false);

    public bool OnOff
    {
        get { return (bool)GetValue(OnOffProperty); }
        set { SetValue(OnOffProperty, value); }
    }
}

iOS Renderer

Note: I keep a reference to the instance of the CustomPropertyEntry passed into OnElementChanged so later I can set its custom property when needed.

public class CustomPropertyEntryRenderer : ViewRenderer<CustomPropertyEntry, UITextField>
{
    UITextField textField;
    CustomPropertyEntry entry;

    protected override void OnElementChanged(ElementChangedEventArgs<CustomPropertyEntry> e)
    {
        base.OnElementChanged(e);

        if (Control == null)
        {
            textField = new UITextField();
            SetNativeControl(textField);
        }
        if (e.OldElement != null)
        {
            textField.RemoveTarget(EditChangedHandler, UIControlEvent.EditingChanged);
            entry = null;
        }
        if (e.NewElement != null)
        {
            textField.AddTarget(EditChangedHandler, UIControlEvent.EditingChanged);
            entry = e.NewElement;
        }
    }

    void EditChangedHandler(object sender, EventArgs e)
    {
        entry.OnOff = !entry.OnOff;
    }
}

XAML Example:

<local:CustomPropertyEntry x:Name="customEntry" Text="" />
<Switch BindingContext="{x:Reference customEntry}" IsToggled="{Binding OnOff}" />

在此处输入图片说明

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