How can I change image.source with c #?

I am developing an application for Windows Phone. I have an image. This is his XAML code:

<Image x:Name="imageclock" Grid.Row="1" Source="Image/Myimage.png" Height="240" Width="240" /> 

And I want to change image.source with this code:

 private void ClickonBtn(object sender, EventArgs e) { BitmapImage bm = new BitmapImage(new Uri("Image/Darktheme.png", UriKind.RelativeOrAbsolute)); imageclock.Source = bm; } 

But when I executed, imageclock.Source = Null and this is an error:

An exception of type 'System.NullReferenceException' occurred in Newappver1.DLL, but was not processed in user code

+6
source share
2 answers

Your code looks fine, but maybe you need to add @ before the image path for processing / in the code below:

 BitmapImage bm = new BitmapImage(new Uri(@"Image/Darktheme.png", UriKind.RelativeOrAbsolute)); 
+7
source
 imageclock.Source = new BitmapImage(new Uri("ms-appx:///Image/Darktheme.png")); 

source https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.image.source.aspx

0
source

Source: https://habr.com/ru/post/949223/


All Articles