How to use Vlc.DotNet in WPF? - wpf

I am trying to use VLC in WPF via Vlc.DotNet. I have been successful in getting Vlc.DotNet to work in Winforms, but thus far unsuccessful with WPF.
I get no errors, but I also get no video... just a blank white pane.
Here is my very simple XAML:
<Window x:Class="VLC.Wpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Closing="Window_Closing">
<Grid x:Name="Grid1">
</Grid>
</Window>
And here is the codebehind I use to insert and start the Vlc Wpf control.
public MainWindow()
{
VlcContext.LibVlcDllsPath = CommonStrings.LIBVLC_DLLS_PATH_DEFAULT_VALUE_AMD64;
VlcContext.LibVlcPluginsPath = CommonStrings.PLUGINS_PATH_DEFAULT_VALUE_AMD64;
VlcContext.StartupOptions.IgnoreConfig = true;
VlcContext.Initialize();
InitializeComponent();
var vlcPlayer = new VlcControl();
var media = new LocationMedia("rtsp://admin:12345#192.168.42.200:554/MediaInput/h264");
Grid1.Children.Add(vlcPlayer);
var vlcBinding = new Binding("VideoSource");
vlcBinding.Source = vlcPlayer;
var vImage = new Image();
vImage.SetBinding(Image.SourceProperty, vlcBinding);
var vBrush = new VisualBrush();
vBrush.TileMode = TileMode.None;
vBrush.Stretch = Stretch.Uniform;
vBrush.Visual = vImage;
Grid1.Background = vBrush;
vlcPlayer.Play();
}
Does anyone see anything wrong with this?
Using Vlc 2.1.5 win32

You haven't set vlcPlayer's Media property.
var vlcPlayer = new VlcControl();
var media = new LocationMedia("rtsp://admin:12345#192.168.42.200:554/MediaInput/h264");
vlcPlayer.Media = media; //add this
Btw, you don't need to add vlcPlayer to Grid1.

Related

TextBlock content becomes invisible when TreeViewItem Header is selected

Included below is the source to a simple WPF application which uses a TreeView. Here's what it looks like when run:
Note however that if I click on one of the TreeViewItem headers:
the text in the TreeViewItem is no longer visible.
What's a good way to fix this so that the text remains visible when the header is selected?
As you can see, the code is primarily in C# so C#-based answers are preferred, but XAML is welcome too; I'll just convert it to C#.
UPDATE
If I set the Foreground of the TextBlock explitly to Black as suggested in an answer below:
{
var tree_view_item = new TreeViewItem() { Header = "abc" };
tree_view_item.Items.Add(new ScrollViewer() { Content = new TextBlock() { Text = "123", Foreground = new SolidColorBrush(Colors.Black) } });
tree_view.Items.Add(tree_view_item);
}
it does indeed appear to help:
However, if I then select the TextBlock, the textblock is shown as black on blue which is a little awkward:
Is there a way to also change the background color used when the item is highlighted?
MainWindow.xaml:
<Window x:Class="TreeViewItemHighlightColor.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:TreeViewItemHighlightColor"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
</Grid>
</Window>
MainWindow.xaml.cs:
using System.Windows;
using System.Windows.Controls;
namespace TreeViewItemHighlightColor
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var tree_view = new TreeView();
{
var tree_view_item = new TreeViewItem() { Header = "abc" };
tree_view_item.Items.Add(new ScrollViewer() { Content = new TextBlock() { Text = "123" } });
tree_view.Items.Add(tree_view_item);
}
{
var tree_view_item = new TreeViewItem() { Header = "bcd" };
tree_view_item.Items.Add(new ScrollViewer() { Content = new TextBlock() { Text = "234" } });
tree_view.Items.Add(tree_view_item);
}
{
var tree_view_item = new TreeViewItem() { Header = "cde" };
tree_view_item.Items.Add(new ScrollViewer() { Content = new TextBlock() { Text = "345" } });
tree_view.Items.Add(tree_view_item);
}
Content = tree_view;
}
}
}
There's a chance that the Foreground property of the TextBlock is being inherited and altered by the state of the TreeViewItem.
Set the Foreground property of the TextBlock to black. That way, the TreeViewItem will not override it.

How to install these Pie Chart controls in Visual Studio?

https://www.codeproject.com/Articles/442506/Simple-and-Easy-to-Use-Pie-Chart-Controls-in-WPF
Can't seem to get these working in my WPF project. How do I add the references to get going with this pie chart?
You need to sign in and download the binaries. You should then unpack the zip file and add a reference (Project->Add Reference->Browse->Browse in Visual Studio) to the downloaded and unpacked PieControls.dll file. After you have done this, the following sample code should work.
MainWindow.xaml:
<Window x:Class="WpfApp1.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:pie="clr-namespace:PieControls;assembly=PieControls"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="300">
<Grid>
<pie:PieChart x:Name="chart1" Width="260" Height="140" PieWidth="120" PieHeight="120"/>
</StackPanel>
</Window>
MainWindow.xaml.cs:
using PieControls;
...
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ObservableCollection<PieSegment> pieCollection = new ObservableCollection<PieSegment>();
pieCollection.Add(new PieSegment { Color = Colors.Green, Value = 5, Name = "Dogs" });
pieCollection.Add(new PieSegment { Color = Colors.Yellow, Value = 12, Name = "Cats" });
pieCollection.Add(new PieSegment { Color = Colors.Red, Value = 20, Name = "Mice" });
pieCollection.Add(new PieSegment { Color = Colors.DarkCyan, Value = 22, Name = "Lizards" });
chart1.Data = pieCollection;
}
}

mouse wheel event is not getting executed

I am trying to use canvas inside windows form and zoom and pan that canvas for that first i put element host and then put canvas inside it and then put picture box in canvas and then trying to zoom canvas i have tried various ways but event of any control does not executed i have also written all mouse wheel events but no one is executed so please suggest me solution below is my code for adding controls and mouse wheel events
elementHost1.Height = picVarify.Height;
elementHost1.Width = picVarify.Width;
elementHost1.Location = picVarify.Location;
touchcanvas = new System.Windows.Controls.Canvas();
WindowsFormsHost hst = new WindowsFormsHost();
hst.Name = "Host";
hst.Child = picVarify;
hst.Height = picVarify.Height;
hst.Width = picVarify.Width;
touchcanvas.Height = picVarify.Height;
touchcanvas.Width = picVarify.Width;
touchcanvas.Children.Add(hst);
zm = new ZoomAndPan.ZoomAndPanControl();
zm.Name = "zm";
zm.Content = touchcanvas;
zm.MouseWheel += new System.Windows.Input.MouseWheelEventHandler(zoomAndPanControl_MouseWheel);
elementHost1.Child = zm;
touchcanvas.MouseWheel += new System.Windows.Input.MouseWheelEventHandler(touchcanvas_MouseWheel);
hst.MouseWheel += new System.Windows.Input.MouseWheelEventHandler(hst_MouseWheel);
picVarify.MouseWheel += new MouseEventHandler(picverify_MouseWheel);
here is the code that's working fine for me. I added canvas inside a canvas and set background property of both canvases.
XAML
<Window x:Class="WpfStack.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="rootGrid" Margin="10,0,0.4,3.8">
<Canvas Name="BaseCanvas" Background="AliceBlue" Margin="10,0,0,10">
</Canvas>
</Grid>
Code
public MainWindow()
{
InitializeComponent();
Canvas touchcanvas = new System.Windows.Controls.Canvas();
touchcanvas.Height =100;
touchcanvas.Width = 100;
touchcanvas.Background = Brushes.Transparent;
touchcanvas.MouseWheel += new System.Windows.Input.MouseWheelEventHandler(touchcanvas_MouseWheel);
BaseCanvas.Children.Add(touchcanvas);
BaseCanvas.Background = Brushes.Transparent;
}
public void touchcanvas_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs args)
{
args.Handled = true;
}

Dynamically creating buttons in WPF xaml file/xaml.cs file after reading an xml file?

I am new to WPF,
Problem Statement: I have a xml file that gives me the number of items that i need to create,
for each item, i need a button.
If there are 20 items---> on loading the xaml file,
the xml will be read,
count(of number of items) will be read and created.
Is there a way to do this in xaml file?
Here is a simple/quick fix:
Expose a Panel (say StackPanel) in the Xaml and add the new buttons to them as Children on run time...
MainWindow.xaml:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Loaded="Window_Loaded">
<StackPanel x:Name="mainPanel"/>
</Window>
MainWindow.xaml.cs
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var buttonNames = new List<string>();
// Parse the XML, Fill the list..
// Note: You could do it the way you prefer, it is just a sample
foreach (var buttonName in buttonNames)
{
//Create the button
var newButton = new Button(){Name = buttonName};
//Add it to the xaml/stackPanel
this.mainPanel.Children.Add(newButton);
}
}
Solution using Data Binding
MainWindow.xaml:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" >
<ItemsControl ItemsSource="{Binding YourCollection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Window>
MainWindow.xaml.cs
public MainWindow()
{
InitializeComponent();
YourCollection = new List<Button>();
// You could parse your XML and update the collection
// Also implement INotifyPropertyChanged
//Dummy Data for Demo
YourCollection.Add(new Button() { Height = 25, Width = 25 });
YourCollection.Add(new Button() { Height = 25, Width = 25 });
this.DataContext = this;
}
public List<Button> YourCollection { get; set; }

How to set multiple canvas in scroll viewer in wpf?

I want to set an image in a canvas control and have 20 canvases that I am creating by using a loop. The problem is that when I want to add all those canvas items in to a scroll viewer it does not work. Here is my code:
private void CreateAndShowCanvas()
{
List<Canvas> list = new List<Canvas>();
for (int i = 0; i < 20; i++)
{
Canvas myCanvas1 = new Canvas();
myCanvas1.Background = new SolidColorBrush(Colors.Transparent);
myCanvas1.Height = 235;
myCanvas1.Width = 626;
//Canvas.SetZIndex(myCanvas1, 4);
Image MainImage = new Image();
MainImage.Width = 275;
MainImage.Height = 235;
BitmapImage mi = new BitmapImage(new Uri("select_1.png", UriKind.Relative));
MainImage.Source = mi;
Canvas.SetTop(MainImage, 0);
Canvas.SetLeft(MainImage, 0);
myCanvas1.Children.Add(MainImage);
Image SeparatorImage = new Image();
BitmapImage si = new BitmapImage(new Uri("Sentre Seprator.png", UriKind.Relative));
SeparatorImage.Height = 270;
SeparatorImage.Source = si;
Canvas.SetTop(SeparatorImage, -5);
Canvas.SetLeft(SeparatorImage, 310);
myCanvas1.Children.Add(SeparatorImage);
Image SecondImage = new Image();
SecondImage.Width = 275;
SecondImage.Height = 235;
BitmapImage sci = new BitmapImage(new Uri("select_2.png", UriKind.Relative));
SecondImage.Source = sci;
Canvas.SetTop(SecondImage, 0);
Canvas.SetLeft(SecondImage, 350);
myCanvas1.Children.Add(SecondImage);
list.Add(myCanvas1);
}
scv.Content = list;
}
and in XML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ScrollViewer Name="scv" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" Margin="60,40,59,46" Opacity="99" Background="Transparent" />
</Grid>
</Window>
and when I run it, it only shows ("Collection"). Please help me out, thanks in advance...
Should you not just be using a ListBox with an ItemTemplate that has Canvases inside it instead? There must be an easier way to do whatever you are trying to achieve than creating 20 canvases manually.
Here's some reading to do with ListBoxes.
A ScrollViewer can hold just a single child, so you need another panel type like a Grid or StackPanel which holds your canvases.
<Grid>
<ScrollViewer Name="scv" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" Margin="60,40,59,46" Opacity="99" Background="Transparent">
<StackPanel Name="stp" />
</ScrollViewer>
</Grid>
Add canvases to stp instead.

Resources