55using System . Reflection ;
66using System . Runtime . CompilerServices ;
77using Avalonia . Controls ;
8+ using Avalonia . Controls . Embedding ;
89using Avalonia . Input ;
910using Avalonia . Interactivity ;
1011using Avalonia . Remote . Protocol ;
@@ -28,10 +29,12 @@ RemoteInteractionRouter remoteInteractionRouter
2829
2930 private BrowserRemoteTransport ? transport ;
3031 private IDisposable ? remoteServer ;
32+ private EmbeddableControlRoot ? remoteRoot ;
3133 private Control ? remoteContent ;
3234 private MainWindow ? remoteWindow ;
3335 private RemoteWindowService ? remoteWindowService ;
3436 private Uri ? endpoint ;
37+ private bool remoteRenderingStarted ;
3538
3639 public Uri ? Endpoint
3740 {
@@ -109,7 +112,6 @@ await Dispatcher.UIThread.InvokeAsync
109112 ) ;
110113 }
111114
112- currentTransport . Start ( ) ;
113115 transport = currentTransport ;
114116 Endpoint = new Uri ( $ "http://{ address } :{ port } /") ;
115117
@@ -173,6 +175,8 @@ private void DisposeRemoteVisual()
173175 {
174176 remoteServer ? . Dispose ( ) ;
175177 remoteServer = null ;
178+ remoteRoot = null ;
179+ remoteRenderingStarted = false ;
176180 remoteContent ? . RemoveHandler ( InputElement . PointerReleasedEvent , OnRemoteInteraction ) ;
177181 remoteContent ? . RemoveHandler ( InputElement . KeyDownEvent , OnRemoteInteraction ) ;
178182 remoteContent ? . DataContext = null ;
@@ -215,10 +219,15 @@ internal MainWindow CreateRemoteVisual(BrowserRemoteTransport currentTransport)
215219 ) ??
216220 throw new InvalidOperationException ( "Avalonia 远程控制服务不可用" ) ;
217221
222+ remoteRoot = serverType . GetField ( "_topLevel" , BindingFlags . Instance | BindingFlags . NonPublic ) ? . GetValue ( remoteServer ) as EmbeddableControlRoot ??
223+ throw new InvalidOperationException ( "Avalonia 远程控制根不可用" ) ;
224+ remoteRoot . StopRendering ( ) ;
225+
218226 serverType . GetProperty ( "Content" ) ! . SetValue ( remoteServer , content ) ;
219227 currentWindowService . Attach ( remoteOverlay , remotePopupLayer ) ;
220228 remoteInteractionRouter . Attach ( currentWindowService ) ;
221229 currentTransport . ViewportChanged += OnRemoteViewportChanged ;
230+ currentTransport . Start ( ) ;
222231 remoteContent = content ;
223232 remoteWindowService = currentWindowService ;
224233 return remoteWindow ;
@@ -237,19 +246,40 @@ private void OnRemoteInteraction(object? sender, RoutedEventArgs e)
237246
238247 private void OnRemoteViewportChanged ( double width , double height )
239248 {
249+ if ( width <= 0 || height <= 0 )
250+ return ;
251+
240252 if ( Dispatcher . UIThread . CheckAccess ( ) )
241253 {
242254 remoteWindow ? . SetRemoteViewportWidth ( width ) ;
255+ ScheduleRemoteRendering ( ) ;
243256 return ;
244257 }
245258
246259 Dispatcher . UIThread . Post
247260 (
248- ( ) => remoteWindow ? . SetRemoteViewportWidth ( width ) ,
261+ ( ) =>
262+ {
263+ remoteWindow ? . SetRemoteViewportWidth ( width ) ;
264+ ScheduleRemoteRendering ( ) ;
265+ } ,
249266 DispatcherPriority . Send
250267 ) ;
251268 }
252269
270+ private void ScheduleRemoteRendering ( )
271+ {
272+ if ( remoteRenderingStarted )
273+ return ;
274+
275+ remoteRenderingStarted = true ;
276+ Dispatcher . UIThread . Post
277+ (
278+ ( ) => remoteRoot ? . StartRendering ( ) ,
279+ DispatcherPriority . Render
280+ ) ;
281+ }
282+
253283 private static IPAddress GetLanAddress ( )
254284 {
255285 var address = NetworkInterface . GetAllNetworkInterfaces ( )
0 commit comments