简体   繁体   English

如何在后端C#和渲染器之间共享字段的值?

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

My C# looks like this: 我的C#看起来像这样:

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 我有一个用于选项卡页面的iOS渲染器

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. 我的问题是两者之间没有连接,我想知道如何做到这一点,以便可以在一个位置设置pauseCard并在另一个位置读取。

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示例,它使用可绑定的bool属性,每次条目中的文本更改时,都会从渲染器更改它。

Entry subclass w/ a bindable property called OnOff ( bool ) 带有可绑定属性OnEnt的入口子类( 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 iOS渲染器

Note: I keep a reference to the instance of the CustomPropertyEntry passed into OnElementChanged so later I can set its custom property when needed. 注意:我保留对传递给OnElementChangedCustomPropertyEntry实例的引用,因此以后可以在需要时设置其custom属性。

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: XAML示例:

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

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么不能在ViewModel中声明的后端C#中填充数组? - Why can I not populate an array in my back-end C# that I declare in my ViewModel? 如何将DynamicResource的值发送到自定义控件C#后端? - How can I send the value of a DynamicResource to my custom controls C# back end? 在C#后端中将资源声明为Class时,如何访问资源? - How can I access a resource when it's declared as a Class in C# back end? 在C#后端,如何在分段控件中选择元素? - In the C# back end, how can I select an element in a Segmented Control? 如何在具有C#后端而不是XAML的模板上绑定XAML元素? - How can I bind a XAML element on a template with the C# back end instead of in the XAML? 如何在C#后端的模板中更改标签的颜色? - How can I change the color of a Label in a template in the C# back end? 我可以将XAML网格的高度绑定回我的C#后端代码吗? - Can I bind the Height of a XAML Grid back to my C# back end code? 如何通过直接调用后端C#的VM修改我的应用程序,以改用Xamarin MessagingCenter? - How can I modify my application from VM making direct calls to the back end C# to instead use Xamarin MessagingCenter? 如何在Visual Studio中放置标签的后端代码 - How to put back-end code for a label in Visual Studio 有什么办法可以在使用C#后端时加快向页面添加新元素的速度吗? - Is there any way that I can speed up the adding of new elements to a page when using the C# back end?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM