creating universal commands that can be shared between views

Joined
Feb 9, 2023
Messages
16
Programming Experience
10+
Good morning,

i am having a little issue here i have created a command that i want to use in 2 different views but unable to seem to find away to do this,

i have done the following that somewhat works,

so this is my downloadvm

DownloadVM:
        private ICommand _Vs2015installed;
        public ICommand Vs2015Installed
        {
            get
            {
                if (_Vs2015installed == null)
                {
                    _Vs2015installed = new RelayCommand(param => Vs2015Installed_Execute(), x=> Vs2015Installed_CanExecute() );
                }
                return _Vs2015installed;


            }

        }


        public bool vsisinstalled()
        {
            if (elfendownload.Versionpy() == String.Empty)
            {
                return true;
            }
            return false;
        }



        public void Vs2015Installed_Execute()
        {
            Test = spotifydownloader.test;
            if (spotifydownloader.test == "True")
            {
                vsinstalled = true;
                vsinstalledstring = "https://aka.ms/vs/17/release/vc_redist.x64.exe";
                Test = vsinstalledstring;
                ObservableCollection<Clients> MyClients = new ObservableCollection<Clients>();
                MyClients.Clear();
                cvs.Source = GetData();
            }
            else
            {
                vsinstalled = false;
                vsinstalledstring = "Already Installed";
                Test = vsinstalledstring;
                ObservableCollection<Clients> MyClients = new ObservableCollection<Clients>();
                MyClients.Clear();
                cvs.Source = GetData();
            }
        }

and as you can see this sets the download button well test button depending on what value is get from exacuted


the issue i have is i want to use this in the install control as well as if its installed dont want them to download or install it so i have setup the same on install,


InstallVM:
        DownloadVM downloadModel = new DownloadVM();

        public string vstext { get; set; }
        public bool isinstalled { get; set; }
        public void download()
        {
            if(downloadModel.VSisalreadyinstalled() == true)
            {
                isinstalled = true;
            }
            else
            {
                isinstalled = false;
            }
        }


        private ICommand _ButtonEnabled;
        public ICommand ButtonEnabled
        {
            get
            {
                if (_ButtonEnabled == null)
                {
                    _ButtonEnabled = new RelayCommand(param => downloadModel.Vs2015Installed_Execute(), x=> downloadModel.Vs2015Installed_CanExecute());
                    
                }
                else
                {

                }
                
                return _ButtonEnabled;
            }
        }

as you can see have had to copy over the code and use download to get the button to work,
this does work however dispite me trying to add button text to the disabled button just fails,

and idealy i would not like hacving to copy same command multible times if at all possible

but unsure how to do it on ui

Install UI:
<UserControl x:Class="Project_Alicization.View.Install"
             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:local="clr-namespace:Project_Alicization.View"
             xmlns:vm="clr-namespace:Project_Alicization.ViewModel"
             xmlns:customcommand="clr-namespace:Project_Alicization.Utilitys"
             mc:Ignorable="d"
             d:DesignHeight="600" d:DesignWidth="622">

    <UserControl.DataContext>
        <vm:InstallVM/>
    </UserControl.DataContext>



    <Grid>



        <Border CornerRadius="5" >


            <Border CornerRadius="0,20,20,0"
            BorderThickness="1"
            Opacity="0.8">

                <Border.Background>
                    <LinearGradientBrush StartPoint="0,1" EndPoint="1,0">
                        <GradientStop Color="{StaticResource primaryBackColor1}" Offset="0"/>
                        <GradientStop Color="{StaticResource primaryBackColor2}" Offset="0.75"/>
                    </LinearGradientBrush>
                </Border.Background>

            </Border>
        </Border>


        <StackPanel Orientation="Vertical"
                    Width="300"
                    Height="500">


            <Button x:Name="vsinstall" Content="{Binding vstext}"
                    Margin="4"
                    Height="20"
                    Command="{Binding ButtonEnabled}"/>


        </StackPanel>
        
        
        
        

    </Grid>
</UserControl>

Picture of Both Download and Install

downloadview.png


As you can see from top of download view it states already installed,

Install view
InstallView.png


trying to get the command to be used across both and to indicate the same Already Installed if it is
kind regards,
Elfenliedtopfan5
 

Latest posts

Back
Top Bottom