How to assign control to variable in WPF MVVM

hungChun 0 Reputation points
2023-03-15T09:05:22.6333333+00:00

I tried a video SDK and got a example code written in C#,WPF below, it shows video stream on screen.

the point is, it used the context variable and assigned PublisherVideo(a control in xaml) to Renderer

I want to rewite it by MVVM pattern, is there any proper way to do this?

I can just bind a property of control to a variable, but don't know how to get control in viewModel...

MainWindow.xaml.cs

        public MainWindow()
        {
            InitializeComponent();

            context = new Context(new WPFDispatcher());

            Publisher = new Publisher.Builder(context)
            {
                Capturer = capturerDevices[0].CreateVideoCapturer(VideoCapturer.Resolution.High, VideoCapturer.FrameRate.Fps30),
                Renderer = PublisherVideo
            }.Build();
        }

xaml

<Window x:Class="BasicVideoChat.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:BasicVideoChat"
        xmlns:OpenTok="clr-namespace:OpenTok;assembly=WPFVideoRenderer"
        mc:Ignorable="d"
        Title="MainWindow" Height="960" Width="640">
    <Grid>
        <OpenTok:VideoRenderer x:Name="PublisherVideo" HorizontalAlignment="Center" Height="480" VerticalAlignment="Top" Width="640">
        </OpenTok:VideoRenderer>
        <OpenTok:VideoRenderer x:Name="SubscriberVideo" HorizontalAlignment="Center" Height="480" VerticalAlignment="Bottom" Width="640">
        </OpenTok:VideoRenderer>
    </Grid>
</Window>
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,665 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,190 questions
{count} votes