使用隐藏代码创建和显示控件

发布于 2024-12-05 03:32:32 字数 1414 浏览 0 评论 0原文

对于我的一个 WPF 项目,我需要集成 VLC 播放器,并且我一直在使用此 Codeplex 项目

但是,我需要在运行时创建 1 个以上的播放器控件实例,并设置其各种属性来显示它。播放器实例的数量基于用户选择。

我正在尝试将示例应用程序中给出的 XAML 转换为其等效的隐藏代码。

<Wpf:VlcControl x:Name="myVlcControl" />

<Grid Grid.Row="0">
     <Grid.Background>
         <VisualBrush Stretch="Uniform">
             <VisualBrush.Visual>
                 <Image Source="{Binding ElementName=myVlcControl, Path=VideoSource}" />
             </VisualBrush.Visual>
         </VisualBrush >
    </Grid.Background>
</Grid> 

这是我到目前为止所得到的

编辑:在 Chen Kinnrot 的回答后更新代码,但问题仍然存在。

    Grid g = new Grid();
    VlcControl p = new VlcControl();

    p.Media = new PathMedia(@"C:\movie.mkv");

    VisualBrush vbr = new VisualBrush();
    g.Background = vbr;

    vbr.Stretch = Stretch.Uniform;

    Binding binding = new Binding("VideoSource");
    binding.ElementName = "p";
    Image img = new Image();
    img.SetBinding(Image.SourceProperty, binding);

    vbr.Visual = img;
    g.Children.Add(p);                       
    this.grid1.Children.Add(g);
    p.Play();

使用上面的代码,当我运行应用程序时,我可以听到电影声音但看不到它。我在后面的代码中缺少图像绑定(元素名称和路径),但不知道该怎么做。

有人可以向我指出一些文档(谷歌搜索不会产生任何好的结果)或提供将 XAMl 中的 Binding 转换为其等效代码的指针吗?

非常感谢

For one of my WPF projects I need to integrate VLC player and I have been playing around with the sample app from this Codeplex Project

However I need to create more than 1 instance of the the player control at runtime and display it setting its various properties. The number of player instances are based on user selection.

I am trying to convert the XAML given in the sample app to its equivalent code behind.

<Wpf:VlcControl x:Name="myVlcControl" />

<Grid Grid.Row="0">
     <Grid.Background>
         <VisualBrush Stretch="Uniform">
             <VisualBrush.Visual>
                 <Image Source="{Binding ElementName=myVlcControl, Path=VideoSource}" />
             </VisualBrush.Visual>
         </VisualBrush >
    </Grid.Background>
</Grid> 

This is what I have gotten to so far

EDIT: Code update after Chen Kinnrot's answer, but the problem persists.

    Grid g = new Grid();
    VlcControl p = new VlcControl();

    p.Media = new PathMedia(@"C:\movie.mkv");

    VisualBrush vbr = new VisualBrush();
    g.Background = vbr;

    vbr.Stretch = Stretch.Uniform;

    Binding binding = new Binding("VideoSource");
    binding.ElementName = "p";
    Image img = new Image();
    img.SetBinding(Image.SourceProperty, binding);

    vbr.Visual = img;
    g.Children.Add(p);                       
    this.grid1.Children.Add(g);
    p.Play();

With the above code behind, when I run the app I can hear the movie sound but not see it. I am missing the Image Binding (elementname and path) in my code behind, but don't know how to do it.

Can someone point me to some docs (Googling doesn't produce any good results) or provide pointers to convert the Binding in XAMl to its equivalent code behind?

Many Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染柒℉ 2024-12-12 03:32:33
        Binding binding = new Binding("VideoSource");
        binding.ElementName = "myVlcControl";
        Image img = new Image();
        img.SetBinding(Image.SourceProperty, binding);
        Binding binding = new Binding("VideoSource");
        binding.ElementName = "myVlcControl";
        Image img = new Image();
        img.SetBinding(Image.SourceProperty, binding);
泅渡 2024-12-12 03:32:33

我知道这个问题很老了,但我刚刚遇到了同样的问题并想分享解决方案。

其实Chen Kinnrot的答案已经很完整了,唯一需要做的就是修复绑定。

而不是:

binding.ElementName = "myVlcControl";

应该是:

binding.Source = p; // p - the instance of the VlcControl

并与 Chen 代码结合:

Binding binding = new Binding("VideoSource");
binding.Source = p;
Image img = new Image();
img.SetBinding(Image.SourceProperty, binding);

I know this question is quite old, but I just encountered the same problem and thought to share the solution.

actually Chen Kinnrot's answer is quite complete, the only thing needed is to fix the binding.

instead of:

binding.ElementName = "myVlcControl";

it should be:

binding.Source = p; // p - the instance of the VlcControl

and combined with Chen code:

Binding binding = new Binding("VideoSource");
binding.Source = p;
Image img = new Image();
img.SetBinding(Image.SourceProperty, binding);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文