应用程序主线程尚未在react native中注册

时间:2016-10-27 13:05:31

标签: android react-native

嗨,我是新的反应本机编程,我正在尝试制作第一个程序。我面临以下问题: -

import React, { Component } from 'react';
import { AppRegistry, Text } from 'react-native';

class HelloWorldApp extends Component {
  render() {
    return (
      <Text>Hello world!</Text>
    );
  }
}

AppRegistry.registerComponent('HelloWorldApp', () => HelloWorldApp);

enter image description here

enter image description here

修改

我正在使用此工具https://rnplay.org

1 个答案:

答案 0 :(得分:0)

当您使用rnplay时,您需要致电

AppRegistry.registerComponent('HelloWorldApp', () => HelloWorldApp);

而不是

import React from 'react';
import {
  registerComponent,
} from 'react-native-playground';
import {
  StyleSheet,
  Text,
  View,
} from 'react-native';

class HelloWorldApp extends React.Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.instructions}>
         Hello world
        </Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#fff',
  },
  instructions: {
    fontSize: 16,
    textAlign: 'center',
    margin: 15,
  },
});

registerComponent(HelloWorldApp);

示例Hello world示例

public class MyTemplateSwitcher : DataTemplateSelector
    {
        private static DataTemplate _senderMessageTemplate  = new DataTemplate(() => new SenderMessageViewCell());
        private static DataTemplate _agentMessageTemplate  = new DataTemplate(() => new AgentMessageViewCell());

        protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
        {
            var messageObject = item as Message;

            switch (messageObject.messageType)
            {
                case MessageType.Sender:
                    return _senderMessageTemplate;
                case MessageType.Agent:
                    return _agentMessageTemplate;
                case MessageType.Bot:
                    return BotDataTemplate(item, container);
            }
        }

        private static DataTemplate BotDataTemplate(object item, BindableObject container)
        {
            var message = item as Message;

            var stackLayout = new StackLayout();
            stackLayout.Children.Add(new Label() { Text = message.SenderId, TextColor = Xamarin.Forms.Color.Navy });

            string[] messageWords = message.MessageText.Split(' ');
            List<string> wordsList = messageWords.ToList();

            foreach (String word in wordsList) // just an example of dynamically creating buttons inside a viewcell
            {
                var button = new Button() { Text = word };
                button.BindingContext = container.BindingContext;
                button.SetBinding(Button.CommandProperty, "TestCommand");
                button.CommandParameter = word;
                stackLayout.Children.Add(button);
            }

            return new DataTemplate( () => new ViewCell { View = stackLayout, BindingContext = container.BindingContext });
        }
    }
相关问题