1

Flex Mobile 应用程序是基于视图的。而且我正在使用 Adob​​e Cirrus(我假设它对于任何 FMS netConnection 都相同) 有谁知道如何在 flex 移动应用程序中的视图之间维护持久的 netConnection?

编辑:尝试更清楚地解释我需要什么...

非常简单,我正在连接到 cirrus

netConnection = new NetConnection();
netConnection.addEventListener(NetStatusEvent.NET_STATUS, cirrusStatusHandler);
netConnection.connect(CirrusAddress + "/" + DeveloperKey);

然后我有这个根据连接状态触发某些功能,

protected function cirrusStatusHandler(event:NetStatusEvent):void

                switch (event.info.code)
                {
                    case "NetConnection.Connect.Closed" :
                        trace(event.info.code);
                        break;

                    case "NetConnection.Connect.Rejected" :
                        trace(event.info.code);
                        break;

                    case "NetConnection.Connect.Failed" :
                        trace(event.info.code);
                        break;

                    case "NetConnection.Connect.Success" :
                        trace(event.info.code);
                        break;

                    case "NetGroup.Connect.Success" :
                        trace(event.info.code);
                        break;

                    case "NetGroup.Posting.Notify" :
                        trace(event.info.code);
                        break;

                    case "NetGroup.Neighbor.Connect" :
                        trace(event.info.code);
                        break;

                    case "NetGroup.Neighbor.Disconnect" :
                        trace(event.info.code);
                        break;

                    case "NetGroup.SendTo.Notify" :
                        trace(event.info.code);
                        break;

                    case "NetStream.Connect.Success" :
                        trace(event.info.code);
                        break;

                    case "NetStream.Publish.Start" :
                        trace(event.info.code);

                        break;

                    case "NetStream.Play.Start" :
                        trace(event.info.code);
                        break;

                    case "NetStream.Play.Failed" :
                        trace(event.info.code);
                        break;


                    case "NetStream.Play.Stop" :
                        trace(event.info.code);
                        break;

                    case "NetStream.Connect.Closed" :
                        trace(event.info.code);
                        break;  

                    case "NetStream.Play.UnpublishNotify" :
                        trace(event.info.code);
                        break;  

                    case "NetStream.Unpublish.Success" :
                        trace(event.info.code);
                        break;

                }
            }

我希望能够trace(netConnection.nearID)在这个视图上,转到另一个视图trace(netConnection.nearID)并获得相同的结果。并且能够拥有cirrusStatusHandler()像我上面那样的功能来监听卷云事件。netConnection.close()如果我想能够关闭连接,那么可以在另一个视图上做。

头脑风暴的想法我认为我可以做什么:

我在想我也许可以在主 ViewNavigatorApplication mxml 文件上设置连接,但是我有很多事情要做,并且基于 netConnection 状态事件被触发,似乎处理每个 mxml 文件中的所有内容可能真的很复杂意见。

也许我可以在 ViewNavigatorApplication mxml 文件中声明 netCon 变量,然后将事件侦听器添加到每个视图上的这些变量?

但我不熟悉访问 mainViewNavigatorApplication mxml 文件中存在的变量

我只需要能够建立一次连接,然后它会一直保持到我打电话netConnection.close()

有任何想法吗?这可能吗?简单的?真的很复杂吗?

我想我可以在不需要 netConnection 的地方使用单独的视图,而只是让这个特定的视图在需要保持 netCon 的视图中使用“状态”。使用状态似乎很愚蠢,因为这是一个基于视图的应用程序。

编辑:@Flextras 答案:

更新:

这是我没有任何错误的编译(直到我调试然后我得到一个崩溃病态解释)

这是我把它放在那里的主要 ViewNavigatorApplication 文件 MYAPP.mxml:

public static var netConnection:NetConnection;
public static var groupspec:GroupSpecifier; 
public static var netGroup:NetGroup;

views.HomeView.netConnection = netConnection;
views.ProfileView.netConnection = netConnection;
views.HomeView.groupspec = groupspec;
views.ProfileView.groupspec = groupspec;
views.HomeView.netGroup = netGroup;
views.ProfileView.netGroup = netGroup;

然后在我的视图包中.. ProfileView.mxml:

public static var netConnection:NetConnection;
public static var groupspec:GroupSpecifier;
public static var netGroup:NetGroup;

在我的主页视图中也是如此

但是当我尝试打电话时出现空错误

trace(netConnection.nearID)在 profileView 的 crationComplete 函数中(在 homeView 之后),看看它是否仍然具有相同的 netConnection,然后应该能够获得相同的 nearID

4

4 回答 4

2

听起来您正在混合 View & Services 的责任。

看看一些框架来帮助您使用 MVC 方法 - 我建议使用 Parsley(因为这是我所知道的),但我听说 RobotLegs 也很棒,并且可能更适合移动应用程序,

一般来说 - 视图不应该有明确的服务知识 - 这些通常在其他地方声明,(比如在使用 Parsley 或 RobotLegs 的上下文中。)

视图的逻辑被封装在表示模型 (PM) 中。这在构建移动应用程序时尤其重要,其中相同的逻辑可能适用于您的视图的许多不同版本(即,Android、iOS、平板电脑等)。

然后,您可以使用消息传递(事件)来发送状态更新,并使用 PM 来在您的应用程序中保持持久状态。

于 2011-06-11T22:10:06.227 回答
1

如果您要问“如何在多个视图之间共享相同的 NetConnection”。

只需给每个视图一个对同一个 NetConnection 对象的引用。每个视图都可以拥有自己的、不同的侦听器来监听从 NetConnection 对象分派的事件。

它不必比在 ProfileView 和 HomeView 类中创建属性更复杂,如下所示:

public var netConnectionInstance : NetConnection;

从字面上看,您可以在两个类中放置同一行。

在包含两个视图的类中,执行以下操作:

var netConnectionInstance = new NetConnection()
profileView.netConnectionInstance  = netConnectionInstance;
homeView.netConnectionInstance  = netConnectionInstance;

在这种情况下,我假设 profileView 和 homeView 都是同一类的孩子。如果你有一个更复杂的显示对象结构,事情会变得更复杂一些;但概念是一样的。

于 2011-06-11T23:34:06.797 回答
0

您可以使用像 Singleton 这样的设计模式来保持视图之间的持久连接

于 2011-06-11T22:11:00.097 回答
0

这就是答案:stackoverflow 不允许我在 2 天内将此标记为答案。我会这样标记它,然后很抱歉它被打开了。

好吧,我一直都知道你可以在推送视图时传递一个数据对象,我一直在用它来获取基本信息和东西,但我不知道你可以用 netConnection 做这样的事情,我只是没想到它会这样工作。和性能方面,我想这是移动应用程序的方式,因为我不是强迫某些东西一直运行,而是通过视图传递它,每个先前的视图都会被破坏。

这就是一切。这是我在 firstView 上已有的变量:

public var netConnection:NetConnection;
public var groupspec:GroupSpecifier;
public var netGroup:NetGroup;

然后我只是将这些变量放在一个对象中,并且在推送视图时就像您将任何其他数据传递到视图中一样:

    var goToNextView:Object=new Object();
    goToNextView.netConnection = netConnection;
    goToNextView.groupspec = groupspec;
    goToNextView.netGroup = netGroup;

    navigator.pushView(views.ProfileView,goToProfileObject);

然后在下一个视图中,从对象中获取数据,如下所示:

netConnection = data.netConnection;
groupspec= data.groupspec;
netGroup = data.netGroup;

然后我在新视图上做了跟踪(netConnection.nearID),它和之前的视图一样,所以它起作用了!

感谢您让我找到正确的方向来解决这个 flexcoders,您发布的解决方案更多的是关于设置全局变量,这个解决方案是使用 flex 移动应用导航器以我猜它们缩进的方式传递对象。

于 2011-06-12T03:38:14.020 回答