7
\$\begingroup\$

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 !

\$\endgroup\$
0

3 Answers 3

6
\$\begingroup\$

I won't suggest you any miraculous improvements because the code is extremely hard to read this is, currently the worst mess in your code are the meaningless names.

private void PlayVideo(ref MediaElement a, string url)
private void PlayVideoHolo(ref Display x, string a)
string str = openFileDialog.FileName.Remove(..);

a, x or str are terrible.

This is the first thing you really need to fix. You probably cannot read it anymore let alone someone else.

The General Naming Conventions might help you to choose better names if you're not sure about them.


You also use ref quite often - actually everywhere. This isn't necessary because C# already passes reference types by reference. You need ref only if you want to overwrite the original variable and you're not doing this.


XAML is equally readable

x:Name="image1"
x:Name="textBox"
x:Name="wano__listBx"

Finally. You should get rid of the #regions. They don't do any good to readability.

You might want to read: Are #regions an antipattern or code smell?


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

One can hardly tell what is what. You expect wonders.

\$\endgroup\$
4
  • \$\begingroup\$ I made changes. Ref, regions are now removed, var renamed. I didn't change names in XAML, those with same name (*1 , *2, etc...) beacause they are made to display the same this. - Would you separate the windows : no direct interaction between them ? I don't really know how. \$\endgroup\$ Jan 13, 2017 at 21:56
  • 2
    \$\begingroup\$ @CédricMEYER I had to rollback those changes, sorry, rules :-] see the link under your question. \$\endgroup\$
    – t3chb0t
    Jan 13, 2017 at 21:58
  • \$\begingroup\$ Should I create a new question now with update code / do you have any other improvements idea ? Is there something else I can do to help you help me :-] ? \$\endgroup\$ Jan 13, 2017 at 22:27
  • 1
    \$\begingroup\$ @CédricMEYER I'd say wait a few hours (days?) for more answers, implement the suggestions and ask a new question with the improved code. \$\endgroup\$
    – t3chb0t
    Jan 13, 2017 at 22:31
3
\$\begingroup\$

i would like them to 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...)

Long story short - you will have to adopt WPF way of doing things, which is MVVM way. Frankly, your code is a mess and I can't see any minor refactoring that would change that. You will have to refactor the whole thing:

  • learn what MVVM pattern is and how to use it in WPF
  • look at your code and try to figure out what code should stay on UI layer, what should go to viewmodel layer and what should go to business (model) layer. To give you an example: code that opens media files is UI-independent, so it clearly should be a part of business logic, while enabling/disabling of buttons is something that is easier done in viewmodels by utilizing ICommand api.
  • start extracting non-UI code from your main window's code-behind accordingly

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

State sharing between the views is normally done by:

  • either sharing a single viewmodel instance between different views/windows
  • or establishing communication between two different viewmodels. Most modern MVVM frameworks already have something in place for such scenario, normally in a form of event aggregator.

It is not clear what problem you are trying to solve exactly, so it is hard to give a more specific advice. Hopefully this will make more sense, once you wrap your head around MVVM.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ For now a virtual +1, tommorow a real one, I need to reaload :-) \$\endgroup\$
    – t3chb0t
    Jan 13, 2017 at 22:27
  • \$\begingroup\$ Thank you for you answer, that's what i'm trying to do, now I have to see how to adapt it to my messy code ! I know that i'm not specific enough but as I am pretty much a beginner it is also a mess in my head right now :-) \$\endgroup\$ Jan 13, 2017 at 22:33
  • 1
    \$\begingroup\$ @CédricMEYER here is a decent article that can get you started codeproject.com/articles/165368/wpf-mvvm-quick-start-tutorial , but I really recommend you read some serious book on WPF/MVVM to avoid making too many mistakes. It will save you some time in a long run. \$\endgroup\$
    – Nikita B
    Jan 13, 2017 at 22:37
3
\$\begingroup\$

Below I'll comment on the xaml.

First of all: there are many ways to do things and we may all have our preferences, so the below is my way:

Position of controls: in general do not use margins to position controls absolute in the window. Instead use the grid with appropriate column- and row definitions. For your MainWindow it could be something like this:

  <Grid Margin="10">
<Grid.ColumnDefinitions>
  <ColumnDefinition Width=".5*" />
  <ColumnDefinition Width=".5*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
  <RowDefinition Height="Auto" />
  <RowDefinition Height="Auto" />
  <RowDefinition Height="Auto" />
  <RowDefinition Height="Auto" />
  <RowDefinition Height="Auto" />
  <RowDefinition Height="*" />
  <RowDefinition Height="Auto" />
  <RowDefinition Height="Auto" />
</Grid.RowDefinitions>
 ...
  <Button x:Name="disp_btn" Margin="0,2.5" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" IsDefault="True" ...
 ...
</Grid>

Use Styles: when ever possible use styles for properties shared on controls of the same type:

  <Application.Resources>

    <SolidColorBrush x:Key="ForegroundBrush" Color="#FF707070" />
    <SolidColorBrush x:Key="BackgroundBrush" Color="Black" />

    <Style TargetType="Button">
      <Setter Property="Height" Value="75" />
      <Setter Property="Foreground" Value="{StaticResource ForegroundBrush}" />
      <Setter Property="Background" Value="{StaticResource BackgroundBrush}" />
      <Setter Property="FontWeight" Value="Bold" />
    </Style>

  </Application.Resources>

Make resources: as show above the ForegroundBrush and BackgroundBrush are created as resources and referenced in the Button Style - and you can reference them inline on other controls as well. It makes it easy to maintain if you later want another color.

Fixed Sizes: Be cautious about fixed size Windows. Your Display Window is defined with Title="HoloAVC" Height="950.333" Width="1600"... ResizeMode="NoResize". A better solution would be to make your content sizable, which gives the user a possibility to resize the window to his/hers needs and a better experience.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ I think the fixed window size is OP's hack to keep the title in the middle because it contains a fixed number of spaces. \$\endgroup\$
    – t3chb0t
    Jan 14, 2017 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.