Variable Images In XAML

Hi,

I'm trying to make a poker app. I'm trying to create variables in an xaml that will change the image source used based on checkbox selection from a subsequent xaml window.

Does anyone know if this is possible and if so, what am I doing wrong?


Any help would be awesome, cheers.

Here is my current script (Just a test not using the full deck of cards yet):

function PokerTableWindow { [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework') [xml]$XAML = @' <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Poker" Height="700" Width="1050" Background="White" WindowStartupLocation="CenterScreen"> <Grid Background="#FF00EEFF" RenderTransformOrigin="0.473,0.537"> <Rectangle Fill="#FF058C2E" HorizontalAlignment="Left" Height="650" Margin="251,7,0,0" Stroke="Black" VerticalAlignment="Top" Width="778"/> <Button Name="btnhand" Content="HAND" HorizontalAlignment="Left" Height="150" Margin="5,7,0,0" VerticalAlignment="Top" Width="241" Background="White" BorderThickness="2" FontSize="36" FontWeight="Bold" Foreground="Black" FontFamily="Segoe UI Light"/> <Button Name="btnflop" Content="FLOP" HorizontalAlignment="Left" Height="150" Margin="5,174,0,0" VerticalAlignment="Top" Width="241" Background="#FFFFF8F8" BorderThickness="2" FontFamily="Segoe UI Light" FontSize="36" Foreground="Black" FontWeight="Bold"/> <Button Name="btnturn" Content="TURN" HorizontalAlignment="Left" Height="150" Margin="5,341,0,0" VerticalAlignment="Top" Width="241" Background="White" BorderThickness="2" FontFamily="Segoe UI Light" FontSize="36" FontWeight="Bold"/> <Button Name="btnriver" Content="RIVER" HorizontalAlignment="Left" Height="150" Margin="5,507,0,0" VerticalAlignment="Top" Width="241" Background="#FFFFFDFD" BorderThickness="2" FontFamily="Segoe UI Light" FontSize="36"/> <Image Name="card3pic" HorizontalAlignment="Left" Height="188" Margin="257,236,0,0" VerticalAlignment="Top" Width="149" Source="$card3" Cursor="Arrow"/> <Image Name="card5pic" HorizontalAlignment="Left" Height="188" Margin="567,236,0,0" VerticalAlignment="Top" Width="149" Source="$card5" Cursor="Arrow"/> <Image Name="card4pic" HorizontalAlignment="Left" Height="188" Margin="413,236,0,0" VerticalAlignment="Top" Width="149" Source="$card4" Cursor="Arrow"/> <Image Name="card6pic" HorizontalAlignment="Left" Height="188" Margin="721,236,0,0" VerticalAlignment="Top" Width="149" Source="$card6" Cursor="Arrow"/> <Image Name="card7pic" HorizontalAlignment="Left" Height="188" Margin="875,236,0,0" VerticalAlignment="Top" Width="149" Source="$card7" Cursor="Arrow"/> <Image Name="card1pic" HorizontalAlignment="Left" Height="188" Margin="257,444,0,0" VerticalAlignment="Top" Width="149" Source="$card1" Cursor="Arrow"/> <Image Name="card2pic" HorizontalAlignment="Left" Height="188" Margin="413,444,0,0" VerticalAlignment="Top" Width="149" Source="$card2" Cursor="Arrow"/> </Grid> </Window> '@ $reader=(New-Object System.Xml.XmlNodeReader $xaml) try{$Form=[Windows.Markup.XamlReader]::Load( $reader )} catch{Write-Host "Unable to load Windows.Markup.XamlReader. Some possible causes for this problem include: .NET Framework is missing PowerShell must be launched with PowerShell -sta, invalid XAML code was encountered."; exit} $xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)} $btnhand.Add_Click({$form.Close()}) $card1pic = <Image Name="card1pic" HorizontalAlignment="Left" Height="188" Margin="257,444,0,0" VerticalAlignment="Top" Width="149" Source="$hcard1, $hcard2, $hcard3, $hcard4, $hcard5, $hcard6, $hcard7" Cursor="Arrow"/> $Form.ShowDialog() | out-null } function HeartsSelectionPreFlop { [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework') [xml]$XAML = @' <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Poker" Height="700" Width="1050" Background="White" WindowStartupLocation="CenterScreen"> <Grid Background="#FF00EEFF" RenderTransformOrigin="0.473,0.537"> <Rectangle Fill="#FF058C2E" HorizontalAlignment="Left" Height="650" Margin="251,7,0,0" Stroke="Black" VerticalAlignment="Top" Width="778"/> <Button Name="btnHEARTS" Content="HEARTS" HorizontalAlignment="Left" Height="150" Margin="5,7,0,0" VerticalAlignment="Top" Width="241" Background="#FFF7FFC8" BorderThickness="2" FontSize="36" FontWeight="Bold" Foreground="#FFFF0101" FontFamily="Segoe UI Light"/> <Button Name="btnDIAMONDS" Content="DIAMONDS" HorizontalAlignment="Left" Height="150" Margin="5,174,0,0" VerticalAlignment="Top" Width="241" Background="#FFFFF8F8" BorderThickness="2" FontFamily="Segoe UI Light" FontSize="36" Foreground="Red" FontWeight="Bold"/> <Button Name="btnSPADES" Content="SPADES" HorizontalAlignment="Left" Height="150" Margin="5,341,0,0" VerticalAlignment="Top" Width="241" Background="White" BorderThickness="2" FontFamily="Segoe UI Light" FontSize="36" FontWeight="Bold"/> <Button Name="btnCLUBS" Content="CLUBS" HorizontalAlignment="Left" Height="150" Margin="5,507,0,0" VerticalAlignment="Top" Width="241" Background="#FFFFFDFD" BorderThickness="2" FontFamily="Segoe UI Light" FontSize="36"/> <Image Name="AceofHearts" HorizontalAlignment="Left" Height="188" Margin="257,22,0,0" VerticalAlignment="Top" Width="149" Source="D:\Documents\Poker\Cards\Aceofhearts.png" Cursor="Arrow"/> <Image Name="_3ofHearts" HorizontalAlignment="Left" Height="188" Margin="567,22,0,0" VerticalAlignment="Top" Width="149" Source="D:\Documents\Poker\Cards\3ofhearts.png" Cursor="Arrow"/> <Image Name="_2ofHearts" HorizontalAlignment="Left" Height="188" Margin="413,22,0,0" VerticalAlignment="Top" Width="149" Source="D:\Documents\Poker\Cards\2ofhearts.png" Cursor="Arrow"/> <Image Name="_4ofHearts" HorizontalAlignment="Left" Height="188" Margin="721,22,0,0" VerticalAlignment="Top" Width="149" Source="D:\Documents\Poker\Cards\4ofhearts.png" Cursor="Arrow"/> <Image Name="_5ofHearts" HorizontalAlignment="Left" Height="188" Margin="875,22,0,0" VerticalAlignment="Top" Width="149" Source="D:\Documents\Poker\Cards\5ofhearts.png" Cursor="Arrow"/> <Image Name="_8ofHearts" HorizontalAlignment="Left" Height="188" Margin="875,238,0,0" VerticalAlignment="Top" Width="149" Source="D:\Documents\Poker\Cards\8ofHearts.png" Cursor="Arrow"/> <Image Name="_7ofHearts" HorizontalAlignment="Left" Height="188" Margin="567,238,0,0" VerticalAlignment="Top" Width="149" Source="D:\Documents\Poker\Cards\7ofHearts.png" Cursor="Arrow"/> <Image Name="_6ofHearts" HorizontalAlignment="Left" Height="188" Margin="257,238,0,0" VerticalAlignment="Top" Width="149" Source="D:\Documents\Poker\Cards\6ofHearts.png" Cursor="Arrow"/> <Image Name="_9ofHearts" HorizontalAlignment="Left" Height="188" Margin="257,453,0,0" VerticalAlignment="Top" Width="149" Source="D:\Documents\Poker\Cards\9ofHearts.png" Cursor="Arrow"/> <Image Name="_10ofHearts" HorizontalAlignment="Left" Height="188" Margin="413,453,0,0" VerticalAlignment="Top" Width="149" Source="D:\Documents\Poker\Cards\10ofHearts.png" Cursor="Arrow"/> <Image Name="JackofHearts" HorizontalAlignment="Left" Height="188" Margin="567,453,0,0" VerticalAlignment="Top" Width="149" Source="D:\Documents\Poker\Cards\JackofHearts.png" Cursor="Arrow"/> <Image Name="QueenofHearts" HorizontalAlignment="Left" Height="188" Margin="721,453,0,0" VerticalAlignment="Top" Width="149" Source="D:\Documents\Poker\Cards\QueenofHearts.png" Cursor="Arrow"/> <Image Name="KingofHearts" HorizontalAlignment="Left" Height="188" Margin="875,453,0,0" VerticalAlignment="Top" Width="149" Source="D:\Documents\Poker\Cards\KingofHearts.png" Cursor="Arrow"/> <CheckBox Name="aceofheartscb" Content="" HorizontalAlignment="Left" Height="19" Margin="321,215,0,0" VerticalAlignment="Top" Width="18"/> <CheckBox Name="_2ofheartscb" Content="" HorizontalAlignment="Left" Height="19" Margin="477,215,0,0" VerticalAlignment="Top" Width="20"/> <CheckBox Name="_3ofheartscb" Content="" HorizontalAlignment="Left" Height="19" Margin="631,215,0,0" VerticalAlignment="Top" Width="16"/> <CheckBox Name="_4ofheartscb" Content="" HorizontalAlignment="Left" Height="19" Margin="787,215,0,0" VerticalAlignment="Top" Width="20"/> <CheckBox Name="_5ofheartscb" Content="" HorizontalAlignment="Left" Height="19" Margin="939,215,0,0" VerticalAlignment="Top" Width="16"/> <CheckBox Name="_8ofheartscb" Content="" HorizontalAlignment="Left" Height="19" Margin="939,432,0,0" VerticalAlignment="Top" Width="16"/> <CheckBox Name="_7ofheartscb" Content="" HorizontalAlignment="Left" Height="19" Margin="631,432,0,0" VerticalAlignment="Top" Width="16"/> <CheckBox Name="_6ofheartscb" Content="" HorizontalAlignment="Left" Height="19" Margin="319,432,0,0" VerticalAlignment="Top" Width="16"/> <CheckBox Name="_9ofheartscb" Content="" HorizontalAlignment="Left" Height="19" Margin="319,641,0,0" VerticalAlignment="Top" Width="16"/> <CheckBox Name="_10ofheartscb" Content="" HorizontalAlignment="Left" Height="19" Margin="477,641,0,0" VerticalAlignment="Top" Width="15"/> <CheckBox Name="Jackofheartscb" Content="" HorizontalAlignment="Left" Height="19" Margin="631,641,0,0" VerticalAlignment="Top" Width="16"/> <CheckBox Name="Queenofheartscb" Content="" HorizontalAlignment="Left" Height="19" Margin="786,641,0,0" VerticalAlignment="Top" Width="16"/> <CheckBox Name="kingofheartscb" Content="" HorizontalAlignment="Left" Height="19" Margin="940,641,0,0" VerticalAlignment="Top" Width="15"/> </Grid> </Window> '@ $reader=(New-Object System.Xml.XmlNodeReader $xaml) try{$Form=[Windows.Markup.XamlReader]::Load( $reader )} catch{Write-Host "Unable to load Windows.Markup.XamlReader. Some possible causes for this problem include: .NET Framework is missing PowerShell must be launched with PowerShell -sta, invalid XAML code was encountered."; exit} $xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)} $btndiamonds.Add_Click({$form.Close()}) $Form.ShowDialog() | out-null }

PokerTableWindow

HeartsSelectionPreFlop

if ($aceofheartscb.ischecked -eq $true){ $hcard1 = "D:\Documents\Poker\Cards\AceOfHearts.png" } if ($_2ofheartscb.ischecked -eq $true){ $hcard2 = "D:\Documents\Poker\Cards\2OfHearts.png" } if ($_3ofheartscb.ischecked -eq $true){ $hcard3 = "D:\Documents\Poker\Cards\3OfHearts.png" } if ($_4ofheartscb.ischecked -eq $true){ $hcard4 = "D:\Documents\Poker\Cards\4OfHearts.png" } if ($_5ofheartscb.ischecked -eq $true){ $hcard5 = "D:\Documents\Poker\Cards\5OfHearts.png" } if ($_6ofheartscb.ischecked -eq $true){ $hcard6 = "D:\Documents\Poker\Cards\6OfHearts.png" } if ($_7ofheartscb.ischecked -eq $true){ $hcard7 = "D:\Documents\Poker\Cards\7OfHearts.png" }

PokerTableWindow



August 30th, 2015 5:46am

XAML is not script.  YOU should post in the Microsoft Developer forums for assistance with using XAML.

HINT: To change an image you need to reload the image into the control:
$imagename.Source=< new image source >

Free Windows Admin Tool Kit Click here and download it now
August 30th, 2015 6:12am

Thanks for the hint I will try that out.

So for example you mean something like:

$reader=(New-Object System.Xml.XmlNodeReader $xaml) 
try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Host "Unable to load Windows.Markup.XamlReader. Some possible causes for this problem include: .NET Framework is missing PowerShell must be launched with PowerShell -sta, invalid XAML code was encountered."; exit}

$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}

    
    $btnhand.Add_Click({$form.Close()})
    $card1pic.Source = $hcard1, $hcard2, $hcard3, $hcard4, $hcard5, $hcard6, $hcard7
    
    
 $Form.ShowDialog() | out-null

    }

I just posted it here because I'm using it in Powershell and my question is based around how the powershell commands can interact with the xaml.



August 30th, 2015 6:28am

If you have bound all objects by name then there is a variable for each control.  Now all control operations and properties are available in any event.

You can load the form and examine it at a prompt by declaring the form variable as a global and inspecting and changing it after it has closed. You can test reassigning values and redisplay the form to see the results.

Free Windows Admin Tool Kit Click here and download it now
August 30th, 2015 6:46am

I will also note that your code is not copy-able and cannot be run as posted because it is written in a way that it will only work on you machine.

Posting large code blocks is not a good idea because it is too difficult to copy correctly.  Post a link to a file or post only a small piece of code that illustrates you issue.

August 30th, 2015 6:51am

I tried using:

$card1pic.Source=$hcard1, $hcard2, $hcard3, $hcard4, $hcard5, $hcard6, $hcard7

I got the following error:

Exception setting "Source": "Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Windows.Media.ImageSource"."
At D:\Documents\Poker\Poker.ps1:52 char:5
+     $card1pic.Source=$hcard1, $hcard2, $hcard3, $hcard4, $hcard5, $hcard6, $hcar ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : ExceptionWhenSetting

I'm not sure how to make the code block smaller. I will have to work on that as I need to use those two xaml functions and an if statement as a minimum for this question to make sense. I usually just drag and select the scripts from this site and it seems to work.

I think I know what you meant by binding all objects by name and I believe I have done that. However I'm not sure what you mean by

"You can load the form and examine it at a prompt by declaring the form variable as a global and inspecting and changing it after it has closed. You can test reassigning values and redisplay the form to see the results."

Sorry I'm pretty new at this. I have only started in the past few days.

Free Windows Admin Tool Kit Click here and download it now
August 30th, 2015 7:33am

Sorry but I cannot teach you how to write script or how to use forms.  You will have to learn  that on your own.  Start by looking up what the "Source" property of the control is and what it does.

If you are not a programmer and have no scripting experience I highly recommend that you do not try to use XAML.  XAML is a very technical interface implementation that relies on Windows Presentation Foundation (WPF).  It was not designed with PowerShell in mind.  Myself and others have learned ways to use WPF.  After many years it is still not used much in PowerShell due to the high technical bar that it sets.

If you are just learning PowerShell start with classic WinForms.  They are much easier to create and manage and will do everything you are trying to do.  The plus is that the use of Windows Forms with PowerShell is well documented.

Start here: https://www.sapien.com/blog/topics/user-interface-design-for-administrators/

Search for "PowerShell and Windows Forms".  You will find many blogs and tutorial.

August 30th, 2015 7:47am

Thanks for the hint I will try that out.

So for example you mean something like:

$reader=(New-Object System.Xml.XmlNodeReader $xaml) 
try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Host "Unable to load Windows.Markup.XamlReader. Some possible causes for this problem include: .NET Framework is missing PowerShell must be launched with PowerShell -sta, invalid XAML code was encountered."; exit}

$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}

    
    $btnhand.Add_Click({$form.Close()})
    $card1pic.Source = $hcard1, $hcard2, $hcard3, $hcard4, $hcard5, $hcard6, $hcard7
    
    
 $Form.ShowDialog() | out-null

    }

I just posted it here because I'm using it in Powershell and my question is based around how the powershell commands can interact with the xaml.



Free Windows Admin Tool Kit Click here and download it now
August 30th, 2015 10:27am

Here is a perfect introduction for what you are trying to do.

Link

#Add Image to Button
$image = New-Object System.Windows.Controls.Image
$image.Source = "C:\users\Administrator\Desktop\Avengers.jpg"
$image.Stretch = 'Fill'
$button.Content = $image

August 30th, 2015 12:35pm

Thank you very much.
Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 7:15am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics