使用隐藏代码创建和显示控件
对于我的一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道这个问题很老了,但我刚刚遇到了同样的问题并想分享解决方案。
其实Chen Kinnrot的答案已经很完整了,唯一需要做的就是修复绑定。
而不是:
应该是:
并与 Chen 代码结合:
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:
it should be:
and combined with Chen code: