using System.Linq; using System.Windows.Media.Imaging; using ytLive.Models; using ytLive.Services; using ytLive.ViewModels; namespace ytLive.ViewModels; public partial class MainViewModel { private void RegisterLoadedWebSources() { if (_webView2Manager == null) return; foreach (var source in Scenes.SelectMany(s => s.Elements).OfType() .Where(s => s.Type == SourceType.WebSource)) { _webView2Manager.Register(source); source.PropertyChanged += (_, e) => { if (e.PropertyName == nameof(Source.WebUri)) _webView2Manager.OnWebUriChanged(source); }; } } // Runs on the main window's Loaded handler (MainWindow.xaml.cs): the // composition controller needs the window HWND as its parent, which does not // exist in the ctor. Web sources register then — no frames are expected before // the window is shown anyway. public void InitWebView2() { _webView2Manager = new WebView2Manager(System.Windows.Application.Current.Dispatcher); _webView2Manager.PreviewBitmapChanged += OnWebView2PreviewBitmapChanged; RegisterLoadedWebSources(); } private void OnWebView2PreviewBitmapChanged(string sourceId, WriteableBitmap bitmap) { foreach (var scene in Scenes) foreach (var el in scene.Elements.OfType().Where(s => s.Id == sourceId)) el.VideoImageSource = bitmap; } }