【问题标题】:Create and display control using code behind使用后面的代码创建和显示控件
【发布时间】:2011-11-19 07:53:42
【问题描述】:

对于我的一个 WPF 项目,我需要集成 VLC 播放器,我一直在使用来自 Codeplex Project 的示例应用程序

但是,我需要在运行时创建超过 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 中的绑定转换为其等效代码的指针吗?

非常感谢

【问题讨论】:

  • 您是否尝试过在 XAML 中创建 VLC 控件的多个实例,以验证它是否支持多个实例?
  • @HiredMind:是的,它确实支持多个实例。

标签: c# wpf xaml binding code-behind


【解决方案1】:
        Binding binding = new Binding("VideoSource");
        binding.ElementName = "myVlcControl";
        Image img = new Image();
        img.SetBinding(Image.SourceProperty, binding);

【讨论】:

  • @Chen Kinnrot:非常感谢您的回答。我已根据您的回答更新了我的代码,但问题仍然存在,即我可以听到电影声音但看不到电影。任何进一步的指针?
【解决方案2】:

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

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

代替:

binding.ElementName = "myVlcControl";

应该是:

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

并结合陈码:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-25
    • 2013-03-18
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多