image is not displaying, and list items are not displaying in the listbox. Can anyone help me with this?
What I have tried:
My Xaml code:
<Window x:Class="solitair.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:solitair"
mc:Ignorable="d"
Title="Get the Close Card" Height="447.881" Width="478.284" ResizeMode="NoResize">
<Grid>
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FFEE5E5E" Offset="0.277"/>
</LinearGradientBrush>
</Grid.Background>
<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="38" Stroke="Black" VerticalAlignment="Top" Width="517"/>
<Label HorizontalAlignment="Left" VerticalAlignment="Top" Width="470" Height="38">
<Label.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FF79CCE4"/>
</LinearGradientBrush>
</Label.Background>
<TextBox Height="23" TextWrapping="Wrap" HorizontalContentAlignment="Center" Text="How Close Can you Get to 25" Width="470" TextChanged="TextBox_TextChanged" BorderBrush="#FF8DE0E4">
<TextBox.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FF79CCE4"/>
</LinearGradientBrush>
</TextBox.Background>
</TextBox>
</Label>
<StackPanel Name="sp" Margin="7,-3,10,3"/>
<Canvas HorizontalAlignment="Left" Height="198" Margin="10,59,0,0" VerticalAlignment="Top" Width="144" Background="#FFF9F4F4">
<Image Name="ImgCard" Height="178" Width="112" Canvas.Top="6" Canvas.Left="16"/>
</Canvas>
<Label Content="# of cards" HorizontalAlignment="Left" Margin="238,46,0,0" VerticalAlignment="Top" Width="65">
<Label.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FFEAD02A" Offset="0.5"/>
</LinearGradientBrush>
</Label.Background>
</Label>
<Label Content="Card" HorizontalAlignment="Left" Margin="320,46,0,0" VerticalAlignment="Top" Width="54">
<Label.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FFEAD02A" Offset="0.5"/>
</LinearGradientBrush>
</Label.Background>
</Label>
<Label Content="Total" HorizontalAlignment="Left" Margin="403,46,0,0" VerticalAlignment="Top" Width="54">
<Label.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FFEAD02A" Offset="0.5"/>
</LinearGradientBrush>
</Label.Background>
</Label>
<Label Name="OfCardstext" HorizontalAlignment="Left" Height="23" Margin="238,80,0,0" Content="" VerticalAlignment="Top" Width="65" Background="#FFFAF0FF"/>
<Label Name="Cardtext" HorizontalAlignment="Left" Height="23" Margin="321,79,0,0" Content="" VerticalAlignment="Top" Width="54" Background="#FFFAF0FF"/>
<Label Name="Totaltext" HorizontalAlignment="Left" Height="23" Margin="404,79,0,0" Content="" VerticalAlignment="Top" Width="54" Background="#FFFAF0FF"/>
<ListBox Name="listview" HorizontalAlignment="Left" Height="140" Margin="232,116,0,0" VerticalAlignment="Top" Width="225">
<Grid Height="116" Width="209">
<ListView x:Name="ListHeader" Height="25" BorderThickness="1" ItemsSource="{Binding Path=''}"
IsSynchronizedWithCurrentItem="True" Margin="0,2,0,89">
<ListView.View>
<GridView>
<GridViewColumn Header="Card" DisplayMemberBinding="{Binding Path=Card}" Width="150"/>
<GridViewColumn Header="Total" DisplayMemberBinding="{Binding Path=Total}" Width="50"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</ListBox>
<Button Content="Get Card" HorizontalAlignment="Left" Margin="334,289,0,0" VerticalAlignment="Top" Width="97" Height="37" FontSize="18" Click="Button_Click_1">
<Button.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FFEAD02A" Offset="0.43"/>
</LinearGradientBrush>
</Button.Background>
</Button>
<Button Content="Restart" HorizontalAlignment="Left" Margin="35,290,0,0" VerticalAlignment="Top" Width="97" Height="37" FontSize="18" Click="Button_Click">
<Button.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FFEAD02A" Offset="0.438"/>
</LinearGradientBrush>
</Button.Background>
</Button>
<ProgressBar Name="pbStatus" Minimum="0" Maximum="100" HorizontalAlignment="Left" Height="19" Margin="85,370,0,0" VerticalAlignment="Top" Width="307" IsIndeterminate="false"/>
<Label Name="Shuffle" Content=" Shuffle" HorizontalAlignment="Left" Margin="193,343,0,0" VerticalAlignment="Top" Width="70"/>
</Grid>
</Window>
My .cs 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.Threading;
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.Windows.Controls.Primitives;
using System.Data;
using System.ComponentModel;
using static System.Net.Mime.MediaTypeNames;
using System.IO;
namespace solitair
{
/// <summary>
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
BackgroundWorker backgroundWorker1 = new BackgroundWorker();
public ImageSource imageSource;
public MainWindow()
{
InitializeComponent();
pbStatus.Visibility = Visibility.Visible;
Reset();
}
public void Reset()
{
//Uri("image\\Cards\\back.png");
ImgCard.Source = imageSource;
OfCardstext.Content = "";
Cardtext.Content = "";
Totaltext.Content = "";
ListHeader.Items.Clear();
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
}
private void hscroll_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Shuffle.Visibility = Visibility.Visible;
pbStatus.Visibility = Visibility.Visible;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.DoWork += backgroundWorker1_DoWork;
backgroundWorker1.ProgressChanged +=
backgroundWorker1_ProgressChanged;
backgroundWorker1.RunWorkerCompleted +=
backgroundWorker1_RunWorkerCompleted;
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = true;
backgroundWorker1.RunWorkerAsync(100);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int result = 0;
for (int i = 1; i <= 100; i++)
{
if (backgroundWorker1.CancellationPending)
{
e.Cancel = true;
break;
}
Thread.Sleep(100);
// Report progress.
backgroundWorker1.ReportProgress(i);
//TODO: your result code here
}
e.Result = result;
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
int item = e.ProgressPercentage;
// Change the value of the ProgressBar to the BackgroundWorker progress.
pbStatus.Value = item;
// Set the text.
}
void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
}
else
{
Random RMNumber = new Random();
int Card = RMNumber.Next(1, 12);
var CardCodes = new List<string> { " c", " d ", "s", " h" };
Random RMChar = new Random();
int index = RMChar.Next(CardCodes.Count);
var Codes = CardCodes[index];
String CardName = "image\\Cards\\" + Codes;
if (Card > 10)
{
CardName = CardName + Convert.ToString(Card) + ".png";
}
else
{
CardName = CardName + "0" + Convert.ToString(Card) + ".png";
}
BitmapImage myBitmapImage = new BitmapImage();
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(@"C://image//cards//*.png");
ImageSource imageSource = myBitmapImage;
ImgCard.Source = imageSource;
string CardNm = "";
if (Codes == "c")
{
if (Card == 11)
{
CardNm = "Jack of Clubs";
}
else if (Card == 12)
{
CardNm = "Queen of Clubs";
}
else if (Card == 13)
{
CardNm = "King of Clubs";
}
else
{
CardNm = Convert.ToString(Card) + "of Clubs";
}
}
else if (Codes == "d")
{
if (Card == 11)
{
CardNm = "Jack of Diamonds";
}
else if (Card == 12)
{
CardNm = "Queen of Diamonds";
}
else if (Card == 13)
{
CardNm = "King of Diamonds";
}
else
{
CardNm = Convert.ToString(Card) + "of Diamonds";
}
}
else if (Codes == "s")
{
if (Card == 11)
{
CardNm = "Jack of Spades";
}
else if (Card == 12)
{
CardNm = "Queen of Spades";
}
else if (Card == 13)
{
CardNm = "King of Spades";
}
else
{
CardNm = Convert.ToString(Card) + "of Spades";
}
}
else if (Codes == "h")
{
if (Card == 11)
{
CardNm = "Jack of Hearts ";
}
else if (Card == 12)
{
CardNm = "Queen of Hearts";
}
else if (Card == 13)
{
CardNm = "King of Hearts";
}
else
{
CardNm = Convert.ToString(Card) + "of Hearts";
}
}
int total = 0;
if (ListHeader.Items.Count> 0)
{
total = Card + Convert.ToInt32(Totaltext.Content);
OfCardstext.Content = Convert.ToInt32(OfCardstext.Content) + 1;
Totaltext.Content = total;
}
else
{
total = Card;
OfCardstext.Content = 1;
Cardtext.Content = Card;
Totaltext.Content = total;
}
Cardtext.Content = Card.ToString();
ListHeader.Items.Add(new card { Cards = CardNm, Total = total });
}
}
}
}