Right now i have a WPF application supposed to display some media into a WPF Window, from actions in another WPF Window ( for example : you write text in a textbox, you click on a button, and the text is displayed in a textblock in the second Window ).
It's working even tho it is completely not optimized, and i would like two things : Separate both windows in different projects in my solution (easy), and maque them communicate not just buy instanciating the display in the control Window, but with design patterns etc... (can't find how because of the Antippatern of my code...)
Here are the Xaml :
MainWindow :
<Window x:Class="Wpf_AvcTeam.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_AvcTeam"
mc:Ignorable="d"
Title=" Command Panel" Height="637" Width="320" Background="Black" WindowStyle="ToolWindow" MaxWidth="1920" MaxHeight="1080" MinWidth="320" MinHeight="637">
<Grid>
<TextBlock x:Name="textBlockTitle" Margin="10,10,10,0" TextWrapping="Wrap" Text="Texte à afficher" VerticalAlignment="Top" Foreground="#FFABADB3" TextAlignment="Center" FontWeight="Bold" Height="16"/>
<TextBox x:Name="textBox" Margin="0,31,0,501" TextWrapping="Wrap" VerticalAlignment="Center" Background="Black" Foreground="#FF707070" TextAlignment="Center" BorderBrush="#FF707070" Height="75" Padding="10,0" HorizontalContentAlignment="Stretch" BorderThickness="2" SelectionBrush="White" FontWeight="Bold">
<TextBox.InputBindings>
<KeyBinding Key="Return" Command="{}" />
</TextBox.InputBindings>
</TextBox>
<Button x:Name="disp_btn" IsDefault="True" Content="Afficher" Margin="10,111,10,0" VerticalAlignment="Top" Background="Black" Foreground="#FF707070" Height="75" BorderThickness="2" OpacityMask="#FF303030" FontWeight="Bold" Click="disp_btn_Click"/>
<Border BorderBrush="#FF707070" BorderThickness="1" Height="2" Margin="0,196,0,409" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5">
<Border.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleY="-1"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Border.RenderTransform>
</Border>
<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="204" Margin="10,230,0,0" VerticalAlignment="Top" Width="141" Background="Black" Foreground="#FF707070">
<ListBoxItem x:Name="ironMan_listBx" Content="Iron Man" Height="50" FontWeight="Bold" Selected="ironMan_listBx_Selected"/>
<ListBoxItem x:Name="linkShield_listBx" Content="Link Shild" Height="50" FontWeight="Bold" Selected="linkShield_listBx_Selected"/>
<ListBoxItem x:Name="daftPunk_listBx" Content="Daft Punk" Height="50" FontWeight="Bold" Selected="daftPunk_listBx_Selected"/>
<ListBoxItem x:Name="anonymous_listBx" Content="Anonymous" Height="50" FontWeight="Bold" Selected="anonymous_listBx_Selected"/>
</ListBox>
<TextBlock x:Name="textBlock" Margin="10,209,10,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="16" Foreground="#FF707070" FontWeight="Bold" TextAlignment="Center" Background="Black"><Run Text="Vidéo"/><Run Text="s"/><Run Text=" Préfaite"/><Run Text="s"/><Run Text=" "/><Run Text="et "/><Run Text="M"/><Run Text="usiques "/><Run Text="à lancer"/></TextBlock>
<ListBox x:Name="listBox1" HorizontalAlignment="Right" Height="204" Margin="0,230,10,0" VerticalAlignment="Top" Width="141" Background="Black" Foreground="#FF707070">
<ListBoxItem x:Name="tetris_listBx" Content="Tetris" Height="50" FontWeight="Bold" Selected="tetris_listBx_Selected"/>
<ListBoxItem x:Name="wano__listBx" Content="We are number one" Height="50" FontWeight="Bold" Selected="wano__listBx_Selected"/>
<ListBoxItem x:Name="ultimate_listBx" Content="Ultimate" Height="50" FontWeight="Bold" Selected="ultimate_listBx_Selected"/>
<ListBoxItem x:Name="csGo_listBx" Content="Cs Go" Height="50" FontWeight="Bold" Selected="csGo_listBx_Selected"/>
</ListBox>
<Button x:Name="open_btn" Content="Open" Margin="10,439,10,0" VerticalAlignment="Top" Height="75" Background="Black" Foreground="#FF707070" FontWeight="Bold" BorderThickness="2" Click="open_btn_Click"/>
<Button x:Name="play_btn" Content="►" HorizontalAlignment="Left" Margin="10,519,0,10" VerticalAlignment="Bottom" Width="141" Height="78" Background="Black" Foreground="#FF707070" BorderThickness="2" FontWeight="Bold" Click="play_btn_Click"/>
<Button x:Name="pause_btn" Content="❚❚" HorizontalAlignment="Right" Margin="161,519,10,10" VerticalAlignment="Bottom" Width="141" Height="78" Background="Black" Foreground="#FF707070" BorderThickness="2" FontWeight="Bold" Click="pause_btn_Click"/>
</Grid>
Display :
<Window x:Class="Wpf_AvcTeam.Display"
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_AvcTeam"
mc:Ignorable="d"
Title=" HoloAVC" Height="950.333" Width="1600" Background="Black" WindowStyle="ToolWindow" SnapsToDevicePixels="True" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" >
<Window.BindingGroup>
<BindingGroup/>
</Window.BindingGroup>
<Grid>
<MediaElement x:Name="BackgroundMediaElement" HorizontalAlignment="Center" Height="720" Margin="159,120,155,82" VerticalAlignment="Center" Width="1280" LoadedBehavior="Manual"/>
<Grid Margin="336,0,336,50">
<MediaElement x:Name="FrontDisplayElement1" Height="300" VerticalAlignment="Top" Margin="0,311,622,0" RenderTransformOrigin="0.5,0.5" LoadedBehavior="Manual" Stretch="Fill" Width="300">
<MediaElement.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="-90"/>
<TranslateTransform/>
</TransformGroup>
</MediaElement.RenderTransform>
</MediaElement>
<MediaElement x:Name="FrontDisplayElement2" HorizontalAlignment="Center" Height="300" Width="300" Margin="311,622,311,-50" RenderTransformOrigin="0.5,0.5" VerticalAlignment="Top" LoadedBehavior="Manual" Stretch="Fill" IsMuted="True">
<MediaElement.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="180"/>
<TranslateTransform/>
</TransformGroup>
</MediaElement.RenderTransform>
</MediaElement>
<MediaElement x:Name="FrontDisplayElement3" HorizontalAlignment="Center" Height="300" VerticalAlignment="Top" Width="300" Margin="311,0" LoadedBehavior="Manual" Stretch="Fill" IsMuted="True"/>
<MediaElement x:Name="FrontDisplayElement4" HorizontalAlignment="Left" Height="300" VerticalAlignment="Top" Width="300" Margin="622,311,0,0" RenderTransformOrigin="0.5,0.5" LoadedBehavior="Manual" IsMuted="True" Stretch="Fill">
<MediaElement.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="+90"/>
<TranslateTransform/>
</TransformGroup>
</MediaElement.RenderTransform>
</MediaElement>
<TextBlock x:Name="textBlock2" HorizontalAlignment="Left" Margin="60,371,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="White" FontSize="48" Width="300" Height="180" RenderTransformOrigin="0.5,0.5" TextAlignment="Center">
<TextBlock.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="-90"/>
<TranslateTransform/>
</TransformGroup>
</TextBlock.RenderTransform>
</TextBlock>
</Grid>
<TextBlock x:Name="textBlock1" HorizontalAlignment="Left" Margin="647,120,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="White" FontSize="48" Width="300" Height="180" TextAlignment="Center"/>
<TextBlock x:Name="textBlock3" HorizontalAlignment="Left" Margin="898,371,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="White" FontSize="48" Width="300" Height="180" RenderTransformOrigin="0.5,0.5" TextAlignment="Center">
<TextBlock.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="90"/>
<TranslateTransform/>
</TransformGroup>
</TextBlock.RenderTransform>
</TextBlock>
<TextBlock x:Name="textBlock4" HorizontalAlignment="Left" Margin="647,622,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="White" FontSize="48" Width="300" Height="180
" RenderTransformOrigin="0.5,0.5" TextAlignment="Center">
<TextBlock.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="180"/>
<TranslateTransform/>
</TransformGroup>
</TextBlock.RenderTransform>
</TextBlock>
<Image x:Name="image1" HorizontalAlignment="Left" Height="300" Margin="647,0,0,0" VerticalAlignment="Top" Width="300" Stretch="Fill"/>
<Image x:Name="image2" HorizontalAlignment="Left" Height="300" Margin="958,311,0,0" VerticalAlignment="Top" Width="300" RenderTransformOrigin="0.5,0.5" Stretch="Fill">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="90"/>
<TranslateTransform/>
</TransformGroup>
</Image.RenderTransform>
</Image>
<Image x:Name="image3" HorizontalAlignment="Left" Height="300" Margin="646,0,0,0" VerticalAlignment="Bottom" Width="300" RenderTransformOrigin="0.5,0.5" Stretch="Fill">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="180"/>
<TranslateTransform/>
</TransformGroup>
</Image.RenderTransform>
</Image>
<Image x:Name="image" HorizontalAlignment="Left" Height="300" Margin="336,311,0,0" VerticalAlignment="Top" Width="300" RenderTransformOrigin="0.5,0.5" Stretch="Fill">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="-90"/>
<TranslateTransform/>
</TransformGroup>
</Image.RenderTransform>
</Image>
</Grid>
Here is my code :
Main Window :
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Application = System.Windows.Application;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using KeyEventHandler = System.Windows.Input.KeyEventHandler;
using OpenFileDialog = Microsoft.Win32.OpenFileDialog;
namespace Wpf_AvcTeam
{
public partial class MainWindow : Window
{
#region Medias
private Display _avc = new Display();
MediaPlayer raoul = new MediaPlayer();
#endregion
#region Booleans
private bool videoIsPlaying = false;
private bool imageIsDisplaying = false;
private bool backgroundImageIsPlayiing = false;
private bool textIsDisplaying = false;
private bool musicIsPlaying = false;
#endregion
#region Url
private string _videoUrl = @"D:\Cédric\Cesi\A2\Projets\DotNetA2\WPF_test_for_HoloAVC\Wpf_AvcTeam\Data\";
public string VideoUrl => _videoUrl;
#endregion
#region Constructor & override
public MainWindow()
{
//ICI FAIRE RECHERCHE DB
InitializeComponent();
_avc.Show();
pause_btn.IsEnabled = false;
play_btn.IsEnabled = false;
this.PreviewKeyDown += new KeyEventHandler(HandleEsc);
/*textBox.PreviewKeyDown += EnterClicked;*/
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
Application.Current.Shutdown();
}
#endregion
#region Buttons
private void disp_btn_Click(object sender, RoutedEventArgs e)
{
DisplayText(ref _avc);
}
private void open_btn_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Video files (*.mp4;)|*.mp4|Audio files (*.mp3;*.wav;*.flac)|*.mp3;*.wav;*.flac|Image files (*.png;*.jpg;*.jpeg)|*.png;*.jpg;*.jpeg|All files (*.*)|*.*";
if (openFileDialog.ShowDialog() == true)
{
string str = openFileDialog.FileName.Remove(0, openFileDialog.FileName.LastIndexOf(".", StringComparison.Ordinal));
switch (str)
{
case ".mp3":
case ".wav":
case ".flac":
raoul.Close();
raoul.Open(new Uri(openFileDialog.FileName));
raoul.Play();
break;
case ".mp4":
PlayVideoHolo(ref _avc, openFileDialog.FileName);
break;
case ".png":
case ".jpg":
case ".jpeg":
DisplayImage(ref _avc, openFileDialog.FileName);
break;
}
}
}
private void play_btn_Click(object sender, RoutedEventArgs e)
{
if (!musicIsPlaying)
{
raoul.Play();
musicIsPlaying = true;
}
if (backgroundImageIsPlayiing)
{
_avc.BackgroundMediaElement.Play();
}
else if (videoIsPlaying)
{
_avc.FrontDisplayElement1.Play();
_avc.FrontDisplayElement2.Play();
_avc.FrontDisplayElement3.Play();
_avc.FrontDisplayElement4.Play();
}
play_btn.IsEnabled = false;
pause_btn.IsEnabled = true;
}
private void pause_btn_Click(object sender, RoutedEventArgs e)
{
if (musicIsPlaying)
{
raoul.Pause();
musicIsPlaying = false;
}
if (backgroundImageIsPlayiing)
{
_avc.BackgroundMediaElement.Pause();
}
else if (videoIsPlaying)
{
_avc.FrontDisplayElement1.Pause();
_avc.FrontDisplayElement2.Pause();
_avc.FrontDisplayElement3.Pause();
_avc.FrontDisplayElement4.Pause();
}
pause_btn.IsEnabled = false;
play_btn.IsEnabled = true;
}
#endregion
#region ListBox of videos
private void ironMan_listBx_Selected(object sender, RoutedEventArgs e)
{
if (imageIsDisplaying)
{
HideImages(ref _avc);
}
if (textIsDisplaying)
{
HideText(ref _avc);
}
if (videoIsPlaying)
{
HideVideoHolo(ref _avc);
}
PlayVideo(ref _avc.BackgroundMediaElement, @"iron_man.mp4");
backgroundImageIsPlayiing = true;
}
private void linkShield_listBx_Selected(object sender, RoutedEventArgs e)
{
if (imageIsDisplaying)
{
HideImages(ref _avc);
}
if (textIsDisplaying)
{
HideText(ref _avc);
}
if (videoIsPlaying)
{
HideVideoHolo(ref _avc);
}
PlayVideo(ref _avc.BackgroundMediaElement, @"link_shield.mp4");
backgroundImageIsPlayiing = true;
}
private void daftPunk_listBx_Selected(object sender, RoutedEventArgs e)
{
if (imageIsDisplaying)
{
HideImages(ref _avc);
}
if (textIsDisplaying)
{
HideText(ref _avc);
}
if (videoIsPlaying)
{
HideVideoHolo(ref _avc);
}
PlayVideo(ref _avc.BackgroundMediaElement, @"playmobil_daftpunk.mp4");
backgroundImageIsPlayiing = true;
}
private void anonymous_listBx_Selected(object sender, RoutedEventArgs e)
{
if (imageIsDisplaying)
{
HideImages(ref _avc);
}
if (textIsDisplaying)
{
HideText(ref _avc);
}
if (videoIsPlaying)
{
HideVideoHolo(ref _avc);
}
PlayVideo(ref _avc.BackgroundMediaElement, @"anonymous_mask.mp4");
backgroundImageIsPlayiing = true;
}
#endregion
#region ListBox of music
private void tetris_listBx_Selected(object sender, RoutedEventArgs e)
{
PlaySound(ref raoul, @"tetris.mp3");
}
private void wano__listBx_Selected(object sender, RoutedEventArgs e)
{
PlaySound(ref raoul, @"wano.mp3");
}
private void ultimate_listBx_Selected(object sender, RoutedEventArgs e)
{
PlaySound(ref raoul, @"ultimate.mp3");
}
private void csGo_listBx_Selected(object sender, RoutedEventArgs e)
{
PlaySound(ref raoul, @"csgo.mp3");
}
#endregion
#region Videos, Text and Sound Player
private void PlayVideo(ref MediaElement a, string url)
{
var path = System.IO.Path.Combine(VideoUrl + url);
a.Source = new Uri(path);
a.Play();
a.MediaEnded += new RoutedEventHandler(me_MediaEnded);
pause_btn.IsEnabled = true;
}
private void PlayVideoHolo(ref Display x, string a)
{
if (imageIsDisplaying)
{
HideImages(ref x);
}
if (textIsDisplaying)
{
HideText(ref x);
}
if (backgroundImageIsPlayiing)
{
HideVideo(ref x);
}
if (x.FrontDisplayElement4.Visibility == Visibility.Hidden)
{
x.FrontDisplayElement1.Visibility =
x.FrontDisplayElement2.Visibility =
x.FrontDisplayElement3.Visibility = x.FrontDisplayElement4.Visibility = Visibility.Visible;
}
x.FrontDisplayElement1.Source = x.FrontDisplayElement2.Source = x.FrontDisplayElement3.Source = x.FrontDisplayElement4.Source = new Uri(a);
x.FrontDisplayElement1.Play();
x.FrontDisplayElement2.Play();
x.FrontDisplayElement3.Play();
x.FrontDisplayElement4.Play();
videoIsPlaying = true;
x.FrontDisplayElement1.MediaEnded += new RoutedEventHandler(vid_MediaEnded);
pause_btn.IsEnabled = true;
}
private void PlaySound(ref MediaPlayer b, string url)
{
var path = System.IO.Path.Combine(VideoUrl + url);
b.Open(new Uri(path));
b.Play();
b.MediaEnded += new EventHandler(sound_MediaEnded);
musicIsPlaying = true;
pause_btn.IsEnabled = true;
}
private void DisplayText(ref Display t)
{
HideVideoHolo(ref t);
HideImages(ref t);
t.textBlock1.Text = t.textBlock2.Text = t.textBlock3.Text = t.textBlock4.Text = textBox.Text;
textIsDisplaying = true;
}
private void DisplayImage(ref Display y, string x)
{
if (videoIsPlaying)
{
HideVideoHolo(ref y);
}
if (y.image.Visibility == Visibility.Hidden)
{
y.image.Visibility = y.image1.Visibility = y.image2.Visibility = y.image3.Visibility = Visibility.Visible;
imageIsDisplaying = false;
}
y.image.Source = y.image1.Source = y.image2.Source = y.image3.Source = new BitmapImage(new Uri(x));
imageIsDisplaying = true;
}
private void HideText(ref Display t)
{
t.textBlock1.Text = t.textBlock2.Text = t.textBlock3.Text = t.textBlock4.Text = null;
textIsDisplaying = false;
}
private void HideImages(ref Display f)
{
f.image.Visibility = f.image1.Visibility = f.image2.Visibility = f.image3.Visibility = Visibility.Hidden;
imageIsDisplaying = false;
}
private void HideVideoHolo(ref Display f)
{
f.FrontDisplayElement1.Visibility =
f.FrontDisplayElement2.Visibility =
f.FrontDisplayElement3.Visibility = f.FrontDisplayElement4.Visibility = Visibility.Hidden;
f.FrontDisplayElement1.Close();
f.FrontDisplayElement2.Close();
f.FrontDisplayElement3.Close();
f.FrontDisplayElement4.Close();
videoIsPlaying = false;
}
private void HideVideo(ref Display f)
{
f.BackgroundMediaElement.Close();
backgroundImageIsPlayiing = false;
}
void me_MediaEnded(object sender, EventArgs e)
{
//play video again
_avc.BackgroundMediaElement.Position = TimeSpan.FromMilliseconds(1);
_avc.BackgroundMediaElement.Play();
}
void vid_MediaEnded(object sender, EventArgs e)
{
//play video again
_avc.FrontDisplayElement1.Position = _avc.FrontDisplayElement2.Position = _avc.FrontDisplayElement3.Position = _avc.FrontDisplayElement4.Position = TimeSpan.FromMilliseconds(1);
_avc.FrontDisplayElement1.Play();
_avc.FrontDisplayElement2.Play();
_avc.FrontDisplayElement3.Play();
_avc.FrontDisplayElement4.Play();
}
void sound_MediaEnded(object sender, EventArgs e)
{
//play video again
raoul.Position = TimeSpan.FromMilliseconds(1);
raoul.Play();
musicIsPlaying = true;
}
#endregion
#region Keyhandler
private void HandleEsc(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
Close();
}
#endregion
}
}
The Display :
using System;
using System.Windows;
namespace Wpf_AvcTeam
{
public partial class Display : Window
{
public Display()
{
InitializeComponent();
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
Application.Current.Shutdown();
}
}
}
Sorry to put this much code but I'm a bit desperate, I am so much into this that I can't even find how to separate everything and do something properly SOLID ;)
I hope you can help me with this mess !