我在Xamarin上做了一个跨平台项目。 想出了这个问题。我有一个TabActivity(包含4个Framgents(Request,Info,Fav,Settings)。还有一个TcpController,它只不过是一个带有一堆异步任务的类。如果你看下面的方法:
public async Task Read( ) {
await socketListener.StartListeningAsync( port );
socketListener.ConnectionReceived += async ( sender, args ) => {
var client = args.SocketClient;
await Task.Delay( 2000 );
while (true) {
System.Console.WriteLine( "Testing.." );
//Read from the ReadStream property of the socket client to receive data
if (client.ReadStream.CanRead) {
await Task.Run( ( ) => client.ReadStream.ReadAsync( dataSet, 0, dataSet.Length ) );
netObj = networkObjectAdapter.ByteArrayToObject( dataSet );
if (netObj.command == NetworkCommand.RECEIVE_LIST) {
Message msg = new Message( );
msg.Obj = dataSet;
requestFragmentHandler.SendMessage( msg );
} else if (netObj.command == NetworkCommand.RECEIVE_VOTE_SONG) {
Message msg = new Message( );
msg.Obj = dataSet;
requestFragmentHandler.SendMessage( msg );
}
//other business logic
}
await args.SocketClient.DisconnectAsync( );
break;
}
};
}
正如你在if语句中看到的那样,当收到一些东西时,我使用RequestFragmentHandler向它发送一条消息,虽然我不确定是否有f.e.每个片段的3个处理程序对Android架构有意义。如果让一堆处理程序处理片段和Tcp Controller之间的通信是一个糟糕的系统决策,那么什么是不阻止UI的最佳方法?