从另一个视图调用视图控件以获取应用程序中的数据

时间:2019-09-21 13:32:38

标签: sapui5 sap-fiori

我试图从详细视图控制器调用主视图的控件。如果这样,我就可以获取列表数据

using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine;
using UnityEngine.Networking;


public class SocketController : MonoBehaviour
{


    [SerializeField]
    PlayerController player;
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(GetRequest("http://plantgo.ru:5000"));


    }

    private void Awake()
    {
        //socket.url = "ws://127.0.0.1:5000/socket.io/?EIO=4&transport=websocket";

    }
    // Update is called once per frame
    void Update()
    {
    }
    //private IEnumerator Connector()
    //{
    //   while (true)
    // {
    //   socket.Emit("movement");

    // yield return new WaitForSeconds(0.10f);

    //       }
    // wait 1 seconds and continue




    //    }
    class PlayerInfo
    {
        public string id;
        public string x;
        public string z;
    }

    IEnumerator GetRequest(string uri)
    {
        while (true)
        {
            using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
            {
                // Request and wait for the desired page.
                yield return webRequest.SendWebRequest();

                string[] pages = uri.Split('/');
                int page = pages.Length - 1;

                if (webRequest.isNetworkError)
                {
                    Debug.Log(pages[page] + ": Error: " + webRequest.error);
                }
                else
                {
                    Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
                    if (webRequest.downloadHandler.text == "no")
                    {
                        continue;
                    }
                    else
                    {
                        PlayerInfo playerInfo = (PlayerInfo)JsonUtility.FromJson<PlayerInfo>(webRequest.downloadHandler.text);
                        Debug.Log(playerInfo);
                        player.PlayerMove(float.Parse(playerInfo.x, CultureInfo.InvariantCulture), float.Parse(playerInfo.z, CultureInfo.InvariantCulture));
                    }
                }

            }
            //yield return new WaitForSeconds(0.1f);
        }
    }


}

如果我这样做,我将无法获取列表

sap.ui.getCore().byId("application-Test-url-component---master--list")

如果版本发生更改,这是正确的呼叫方式

sap.ui.getCore().byId("list").

请提供建议或任何链接。

1 个答案:

答案 0 :(得分:0)

首先,不要在byId(...)上使用Core方法,这是一种不好的做法,也不是一个养成的好习惯。

出于您的兴趣,您的第一行代码确实找到了相关的UI控件,而第二行却没有找到相关的原因,是因为Core已在Fiori Launchpad中的所有应用程序之间共享。这意味着所有控件ID均以应用程序名称空间为前缀,并且由于第二行代码不包含应用程序名称空间,因此找不到控件。

关于如何从其他位置正确地从该控件中获取列表数据,只需从与该控件绑定到的模型路径中获取绑定数据即可。例如,如果主列表绑定到{/Products},则this.getView().getModel().getProperty("/Products")会得到相同的数据。但是,这假定您的数据模型在整个应用程序中共享。我建议您阅读UI5文档并了解有关MVC架构的更多信息,因为UI控件应该只显示存储在基础模型中的数据,也就是应该存储数据的地方。

让我知道是否有任何不清楚的地方或是否有疑问。如果我有帮助,请标记为正确。