I want to play video directly from the server in MediaElement. (The source will be the server)
I have a URL of the server:
http://videotherapy.co/dev/vt/api/dispatcher.php
And post the following json:
{"videoId":"22-1","api":"get-training-video"}
(where 22-1 is the videoId)
XAML code:
<Window x:Class="MediaElementApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="467.91" Width="1300">
<Grid>
<MediaElement x:Name="mediaElement" HorizontalAlignment="Left" Height="418" Margin="246,10,0,0" VerticalAlignment="Top" Width="1036" LoadedBehavior="Manual" UnloadedBehavior="Stop" Source="Images\Wildlife.wmv" />
<Button x:Name="play" HorizontalAlignment="Left" Margin="538,161,0,0" VerticalAlignment="Top" Width="100" Height="84" Click="play_Click" >
<Button.Background>
<ImageBrush ImageSource="Images/smiley.jpg"/>
</Button.Background>
</Button>
</Grid>
c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MediaElementApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void play_Click(object sender, RoutedEventArgs e)
{
mediaElement.Play();
}
}
}
How to do this?
I would like for help.
The MediaElement accepts a URI as Source. Like this, it can stream the video from the web to the client application.
If this is somehow not possible in your case. You'll have to download the video to a File. Then pass the location of this file to the Source property of the MediaElement
Related
I am relatively new to this platform, so forgive me for my lack of professionalism.
I encountered a problem while creating one of my first WPF applications.
I managed to include some buttons in MainWindow and run it, but I spotted some bugs, so I tried to start from the scratch.
I decided to run an empty window, but then i saw the previous "buggy" window at runtime. Since then, no matter what i change in MainWindow.xaml file i get the same old result.
I tried to chceck whether my program compiles, and for what I know it does.
Unfortunately I erased old xaml code responsible for "buggy" window, but I can recall it was something like this.
<Window x:Class="ProjektPO.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:ProjektPO"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button x:Name="A" Content="B" Click="A_Click"/>
</Grid>
</StackPanel>
</Grid>
</Window>
And the .cs file (almost default)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ProjektPO
{
/// <summary>
/// Logika interakcji dla klasy MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void A_Click(object sender, RoutedEventArgs e)
{
}
}
}
Program compiles and dispalys old "buggy" Window.
This question already has answers here:
How to make the animation smoother through code in WPF?
(3 answers)
WPF: Animation is not smooth
(1 answer)
WPF Jitters with TranslateTransform and Canvas.SetLeft
(1 answer)
Why creating non storyboard animation is not smooth
(1 answer)
Closed 5 years ago.
The following text block ticker animation is not smooth at all. So need some guidance on how to make this double animation smoother for more user friendliness as I will be integrating RSS news feed in there after that.
XAML CODE:
<Window x:Class="Wpf_Marquee.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:Wpf_Marquee"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Canvas Margin="50" Canvas.Left="150" Canvas.Top="100" ClipToBounds="True" Name="canMain" Background="Red" Height="100" Width="400" >
<TextBlock x:Name="marquee" Margin="0 50 0 0" FontSize="20pt" Foreground="Black" Background="Transparent" Canvas.Left="0" Canvas.Right="68">
Hello This is a Sample Marquee in Wpf. Please check the animation
</TextBlock>
<Button x:Name="btn1" Width="50" Height="50" Content="Marquee" Click="Button_Click" />
</Canvas>
Where as my .CS code is as follows
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
namespace Wpf_Marquee
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public void Button_Click(object sender, RoutedEventArgs e)
{
DoubleAnimation doubleAnimation = new DoubleAnimation();
doubleAnimation.From = this.ActualWidth;
doubleAnimation.To = -marquee.ActualWidth;
doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(2060)); // provide an appropriate duration
marquee.BeginAnimation(Canvas.LeftProperty, doubleAnimation);
}
}
}
Thanks in advance for your help :)
I am a complete "weekend warrior" to programming so go gentle on me, please...
I want to achieve a simple task, which I had no problem with in plain C# (TextBox.Text = "Something";)
I have a textbox and a textblock in XAML, and I want to populate them from C#, simple as that - run the web app and pull the predefined strings from the C# code. I have been searching for 2 days now and can't find a straight answer. I don't want any triggers (buttons), just filling e.g. textbox on load with a C# string. A simple code example on both XAML and C# side would be greatly appreciated!
Cheers,
Sasa
Not sure if I'm understanding your question...
<!--xaml-->
<TextBox x:Name="txtMyTextBox"/>
// C#
// Window constructor
public MyWindow()
{
InitializeComponent();
txtMyTextBox.Text = "Something";
}
One simple way to achieve what you try to achieve (if I understood correctly), is to add the UI elements to your window and set a Name for each of them. This way, you can access them in your code-behind as seen below.
<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" Loaded="Window_Loaded">
<Grid>
<TextBox Name="TextBox1" HorizontalAlignment="Left" Height="23" Margin="37,37,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<TextBlock Name="TextBlock1" HorizontalAlignment="Left" Margin="48,100,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="103" Width="239"/>
</Grid>
</Window>
While there are certainly more "refined" ways to achieve the same, once you named your UI elements in xaml, you can access them just as other object instances in your code-behind.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
TextBox1.Text = "Hello TextBox1!";
TextBlock1.Text = "Hello TextBlock1!";
}
}
}
I am trying to use WPFToolkit in my user control dll. I inserted the WPFToolkit in its reference, and my user control dll builds with no error.
Then I insert my user control dll into my application, but when my application new an object of my user control dll
MultiROIStats mroi = new MultiROIStats();
the exception occured, saying:
Additional information: Could not load file or assembly 'WPFToolkit, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
Here is my user control dll code, the constructor where the error occurs.
View xaml code:
<Window x:Class="MultiROIStats.MultiROIStats"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<dg:DataGrid ItemsSource="{Binding Path=FileData}" Margin="0,30,0,0" />
<Button Height="22" HorizontalAlignment="Left" Margin="8,4,0,0"
Name="button1" VerticalAlignment="Top" Width="48"
Command="{Binding Path=GetDataCommand}">Button
</Button>
</Grid>
</Window>
View C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MultiROIStats
{
using System.Windows;
using ViewModel;
//xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WpfToolkit"
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class MultiROIStats : Window
{
public MultiROIStats()
{
InitializeComponent(); // exception occurs here!
DataContext = new MultiROIStatsViewModel();
}
}
}
I also checked the binary folder of my user control dll, the WPFToolkei.dll is there. So I am confused, and I am wondering how can I correct this error? Thanks.
Have you maybe tried the NuGet package for the toolkit to see if that works instead?
I know there is information about this on the internet and I've searched for it. But I'm still getting the error, can anyone point out to me what I'm doing wrong?
Base class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
namespace ProgramManagementV2.screens
{
public abstract class AScreenUserControl : UserControl
{
public string GetScreenDescriptionName()
{
return "No name yet!";
}
}
}
MainUserControl.xaml
<UserControl x:Class="ProgramManagementV2.screens.MainUserControl"
xmlns:we="clr-namespace:ProgramManagementV2.screens"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBlock Height="23" HorizontalAlignment="Center" Name="textBlock1" Text="asdfasdf" VerticalAlignment="Center" />
</Grid>
</UserControl>
MainUserControl.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
namespace ProgramManagementV2.screens
{
/// <summary>
/// Interaction logic for MainUserControl.xaml
/// </summary>
public partial class MainUserControl : AScreenUserControl
{
public MainUserControl()
{
InitializeComponent();
}
}
}
As you can see I'm adding
xmlns:we="clr-namespace:ProgramManagementV2.screens"
to the user control xml but I'm still getting the error:
Partial declarations of 'ProgramManagementV2.screens.MainUserControl' must not specify different base classes
Can anyone explain to me what I'm doing wrong?
By using <UserControl ... you claim the base-class to be UserControl, you would need to change it to
<we:AScreenUserControl x:Class="ProgramManagementV2.screens.MainUserControl" ...
However UserControls only allow one level of inheritance i think, at least if the AScreenUserControl has a XAML this surely will not work.
Just do this:
<we:AScreenUserControl x:Class="ProgramManagementV2.screens.MainUserControl"
xmlns:we="clr-namespace:ProgramManagementV2.screens"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBlock Height="23" HorizontalAlignment="Center" Name="textBlock1" Text="asdfasdf" VerticalAlignment="Center" />
</Grid>
</we:AScreenUserControl>