WPF and codeBehind file problem....

M

Marko Vuksanovic

I have built an WPF application that gets links to photos from a web service (flickr) and used the following code to achieve it:

<Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="31,10,39.0000000000001,17" Width="Auto" Height="Auto" x:Name="Button" Content="Get Photos" RenderTransformOrigin="-0.462962962962963,-0.40625" Grid.ColumnSpan="1" Grid.RowSpan="1" Command="{Binding GetDataCommand, Mode=Default, Source={StaticResource PhotosCollectionDS}}">
<Button.RenderTransform>
<TransformGroup>
<TranslateTransform X="0" Y="0"/>
<ScaleTransform ScaleX="1" ScaleY="1"/>
<SkewTransform AngleX="0" AngleY="0"/>
<RotateTransform Angle="0"/>
<TranslateTransform X="0" Y="0"/>
<TranslateTransform X="0" Y="0"/>
</TransformGroup>
</Button.RenderTransform>
</Button>
<ListBox HorizontalAlignment="Stretch" Margin="12,21,12,10" Width="Auto" x:Name="ListBox" Grid.Row="1" Grid.ColumnSpan="1" ItemsSource="{Binding Photos, Mode=Default, Source={StaticResource PhotosCollectionDS}}" ItemTemplate="{DynamicResource PhotosTemplate}"/>

Then I have tried to modify this using Visual Studio so that the Click event is handled in the codebehind file <Button.... Click="OnButtonClick">.....</Button>

have tried calling refresh() on the listbox items but this didn't work...
If I use the code in the previous post, everything works fine....

If I use the following code, the thing does not work....

<Grid
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/interactivedesigner/2006"
mc:Ignorable="d"
Background="#FFFFFFFF"
x:Name="DocumentRoot"
x:Class="UntitledProject1.Scene1"
Width="640" Height="480" xmlns:FlickrAPI="clr-namespace:FlickrAPI;assembly=FlickrActivityLibrary">
<Grid.Resources>
<Storyboard x:Key="OnLoaded"/>
<ObjectDataProvider x:Key="PhotosCollectionDS" d:IsDataSource="True" ObjectType="{x:Type FlickrAPI:photosCollection}"/>
<DataTemplate x:Key="PhotosTemplate">
<StackPanel x:Name="StackPanel">
<Image x:Name="Image" Source="{Binding ThumbNailPhoto}"/>
</StackPanel>
</DataTemplate>
</Grid.Resources>
<Grid.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard x:Name="OnLoaded_BeginStoryboard" Storyboard="{DynamicResource OnLoaded}"/>
</EventTrigger>
</Grid.Triggers>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.3046875*"/>
<ColumnDefinition Width="0.6953125*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.13125*"/>
<RowDefinition Height="0.86875*"/>
</Grid.RowDefinitions>
<Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="31,10,39.0000000000001,17" Width="Auto" Height="Auto" x:Name="Button" Content="Get Photos" RenderTransformOrigin="-0.462962962962963,-0.40625" Grid.ColumnSpan="1" Grid.RowSpan="1" Click="OnButtonClick">
<Button.RenderTransform>
<TransformGroup>
<TranslateTransform X="0" Y="0"/>
<ScaleTransform ScaleX="1" ScaleY="1"/>
<SkewTransform AngleX="0" AngleY="0"/>
<RotateTransform Angle="0"/>
<TranslateTransform X="0" Y="0"/>
<TranslateTransform X="0" Y="0"/>
</TransformGroup>
</Button.RenderTransform>
</Button>
<ListBox HorizontalAlignment="Stretch" Margin="12,21,12,10" Width="Auto" x:Name="ListBox" Grid.Row="1" Grid.ColumnSpan="1" ItemsSource="{Binding Photos, Mode=Default, Source={StaticResource PhotosCollectionDS}}" ItemTemplate="{DynamicResource PhotosTemplate}"/>
</Grid>

The problematic part is in Black and bolded. Now the OnButtonClick is defined as follows:

public void OnButtonClick(object sender, EventArgs e)
{
InitializeWorkflow();
instance.Start();
string tempString = "";
waitHandle.WaitOne();
for (int i = 0; i < ds1.Tables.Count; i++)
{
for (int j = 0; j < ds1.Tables.Rows.Count; j++)
{
tempString = tempString + "Table: {" + ds1.Tables.TableName + "}, Row: {" + j + "}:" + "\n";
for (int k = 0; k < ds1.Tables.Columns.Count; k++)
{
tempString = tempString + "Table: {" + ds1.Tables.TableName + "}, Column: {" + ds1.Tables.Columns[k].ColumnName + "}, Value: {" + ds1.Tables.Rows[j][k] + "}" + "\t";
}
//Console.WriteLine("Table: {0}, Column: {1}, Value: {2}", ds.Tables.TableName, ds.Tables.Columns[j].ColumnName, ds.Tables.);
}
}
System.Windows.MessageBox.Show(tempString);
FlickrAPI.PhotosCollection pc = new FlickrAPI.PhotosCollection();
pc.ds = ds1;
pc.GetData(); //This should populate the listBox
}

And the part where GetDataCommand is defined is:

public class PhotosCollection
{
public DataSet ds= new DataSet();
private static DataTable photoDataTable= new DataTable();
private ObservableCollection<Photo> photos = new ObservableCollection<Photo>();
public ObservableCollection<Photo> Photos { get { return this.photos; } }
private DelegateCommand getDataCommand;
public PhotosCollection()
{
getDataCommand = new DelegateCommand(delegate() { GetData(); });
}
public DelegateCommand GetDataCommand { get { return getDataCommand; } }
public void GetData()
{
string apiSig = FlickrAPI.HelperMathods.Md5Sum("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
FlickrAPI.Response res = new FlickrAPI.Response();
string tempString = "";
tempString = res.GetResponse("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
StringReader sreader = new StringReader(tempString);
ds.ReadXml(sreader);
photoDataTable=ds.Tables["photo"];
photos.Clear();
for (int i = 0; i < photoDataTable.Rows.Count; i++)
{
photos.Add(new Photo((string)photoDataTable.Rows[0], (string)photoDataTable.Rows[1], (string)photoDataTable.Rows[2], (string)photoDataTable.Rows[3], (string)photoDataTable.Rows[4], (string)photoDataTable.Rows[5], (string)photoDataTable.Rows, (string)photoDataTable.Rows[7]));
}
}
}

public class Photo
{
private string id;
private string owner;
private string secret;
private string server;
private string title;
private string ispublic;
private string isfriend;
private string isfamily;
private String photoURL;
public Photo(string id, string owner, string secret, string server, string title, string ispublic, string isfriend, string isfamily)
{
this.id = id;
this.owner = owner;
this.secret = secret;
this.server=server;
this.title = title;
this.ispublic = ispublic;
this.isfriend = isfriend;
this.isfamily = isfamily;
photoURL=("http://static.flickr.com/"+this.server+"/"+this.id+"_"+this.secret+"_t.jpg");
this.thumbNailPhoto = ImageFromString(photoURL);
}

public ImageSource ThumbNailPhoto
{
get { return this.thumbNailPhoto; }
}
ImageSource thumbNailPhoto;
public ImageSource ImageFromString(string url)
{
BitmapImage image = null;
if (url != null)
{
image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(url);
image.EndInit();
}
return image;
}
}

I am pretty sure that it is a very st*** mistake.. But I cannot figure out what is the mistake....

Any help is really appreciated.

Thanks,
M.V.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top