Answered by:
Total Newbie Question: How to display an image when a button is clicked?

Question
-
Simple enough. I created a button, that when clicked is to display an image. Just a simple jpeg or whatever.
From the Toolbox I dragged an "image" box over to the Designer, but from there I'm stumped. After doing all types of searches, none of the answers pertain to Windows 8, C# and Visual Studio 2012.
Please help! Thanks so much!
And question 2: Are their any online video tutorials on programming for Windows 8 in C#? I've found only stray ones on YouTube that only go so far. There were some wonderful ones for Windows Phone that were very helpful and I was curious if there were some for Windows 8 as well.
Friday, August 10, 2012 2:17 PM
Answers
-
add this xaml into a Page
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Grid.RowDefinitions> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <Button Content="set image" Click="ButtonBase_OnClick"></Button> <Image x:Name="myImage" Grid.Row="1"></Image> </Grid>
and define the click handler in the code-behind file, like this..
private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { myImage.Source = new BitmapImage(new Uri("ms-appx:/Assets/Logo.png")); }
Friday, August 10, 2012 2:42 PM -
Question2
- Everything about Windows 8.
- Windows 8 Dev Camp in a box
- http://www.microsoft.com/en-us/download/details.aspx?id=29854 (8 Sessions, contains documents which take you through each step)
- http://msdn.microsoft.com/en-us/jj206431 (Virtual labs which you can use to practice above)
- Windows 8 User experience training
- Blogs to watch out for
Friday, August 10, 2012 4:00 PM - Everything about Windows 8.
All replies
-
add this xaml into a Page
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Grid.RowDefinitions> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <Button Content="set image" Click="ButtonBase_OnClick"></Button> <Image x:Name="myImage" Grid.Row="1"></Image> </Grid>
and define the click handler in the code-behind file, like this..
private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { myImage.Source = new BitmapImage(new Uri("ms-appx:/Assets/Logo.png")); }
Friday, August 10, 2012 2:42 PM -
Question2
- Everything about Windows 8.
- Windows 8 Dev Camp in a box
- http://www.microsoft.com/en-us/download/details.aspx?id=29854 (8 Sessions, contains documents which take you through each step)
- http://msdn.microsoft.com/en-us/jj206431 (Virtual labs which you can use to practice above)
- Windows 8 User experience training
- Blogs to watch out for
Friday, August 10, 2012 4:00 PM - Everything about Windows 8.
-
Thanks for the answers, guys!
Not there is one problem (likely just me) but when I did all of the code as shown, and tried to run it, I got an error:
"The type or namespace name 'Bitmapimage' could not be found (are you missing a using directive or an assembly reference?)"
I went over everything to make sure I didnt mis-type, so I completely stuck again.
Friday, August 10, 2012 4:56 PM -
Ah yes, you would need this line at the top of your code-behind file
using Windows.UI.Xaml.Media.Imaging;
Friday, August 10, 2012 5:11 PM