I'm trying to change Source property of MediaElement in my windows-phone application , I have two mp3 files in Assets file :
"/Assets/fail.mp3"
"/Assets/success.mp3"
when I set this proeperty in the designer to /Assets/success.mp3" for example, and invoke :
mediaElement.play();
the sound plays properly , but I want to change the source in my code and then play the sound again , I've tried these options :
mediaElement.Source = new Uri(BaseUri, "/Assets/fail.mp3");
mediaElement.Source = new Uri("ms-appx:///Assets/fail.mp3", UriKind.RelativeOrAbsolute);
mediaElement.Source = new Uri("/Assets/fail.mp3", UriKind.RelativeOrAbsolute);
mediaElement.Source = new Uri("/Assets/fail.mp3");
but none of them works I got no error with no sound !, any one have an Idea what to do ?
This is UWP? Maybe you need to update your resource strings as shown here:
Uri syntax in ResourceDictionary Source (Universal Windows Platform)
Related
So I built a WPF user control as part of a larger solution. I'm building another app where this user control would work nice. I added a reference to the DLL from the new app, wired it up and compiled. The window loads and I can see the image that it's telling me it can't find. After the main window displays and the user control is populated, an exception is thrown saying...
System.IO.IOException: 'Cannot locate resource 'resources/nosortnofilter.png'.'
The user control is a DataGrid with some extensions added to it. The column it threw on was "id". As you can see in the image, the red arrow shows the nosortnofilter.png image being displayed in all columns. So why is it throwing this exception?
The line of code it throws on is here.
If ImageName = "" Then ImageName = "NoSortNoFilter"
img.Source = New BitmapImage(New Uri("pack://application:,,,/Resources/" & ImageName & ".png"))
So it all looks good from my perspective. Hoping someone can see what I'm not seeing.
EDIT: Found a solution. This works. But it still doesn't answer the questions why the original pack:// formatted URI only worked with the original solution.
img.Source = New BitmapImage(New Uri($"Resources/{ImageName}.png", UriKind.Relative))
EDIT: Thanks to rfmodulator for giving me the correct URI for DLL's.
img.Source = New BitmapImage(New Uri("pack://application:,,,/AdvancedSortFilterDataGrid;component/Resources/" & ImageName & ".png"))
This is the URI of a resource in the Application's assembly:
pack://application:,,,/Resources/[RESOURCENAME]
To get a resource in a DLL, the URI looks more like this:
pack://application:,,,/[DLLNAME];component/Resources/[RESOURCENAME]
For details, see Pack URIs in WPF.
I'm having a little issue here in showing images.
So when I'm trying to load images from XAML, I can use a relative uri to the image source like this :
<Image Source="/Assets/image.jpg" />
But if I try to change the image source programatically from code behind, I always get an exception and I'm sure it's because of the false Uri. I tried something like this :
BitmapImage bitmapImage = new BitmapImage(new Uri("/Assets/image.jpg"));
Am I doing it wrong? Any help will be appreciated, thanks!
You can also use it with BaseUri.
BitmapImage bitmapImage = new BitmapImage(new Uri(this.BaseUri, "/Assets/image.jpg"));
Assets is a folder name and you can change it with your any custom folder name :)
To access files stored inside the application package, but from code where there is no inferred root authority, you need to specify the ms-appx: scheme :
So in your case it will be something like :
BitmapImage bitmapImage =
new BitmapImage(new Uri("ms-appx:///[project-name]/Assets/image.jpg"));
Read this documentation for more details : https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh965322.aspx
Hope it helps.
I believe WPF is more forgiving and paths like "Assets/image.png" are possible. If you are writing a UWA (win 10) app then you need to use the "ms-appx" URI's.
My experience is "ms-appx:///Assets/image.png" works fine.
Yes, three "/"s (!) and no need for the name of the app in the path.
Image img = new Image();
img.Source = new BitmapImage(new Uri("http://www.contoso.com/images/logo.png"));
Just use it like this
<Image Source="Assets/image.jpg" />
When I want to change an image's source, the image simply disappears.
The code I'm using:
tactImg.Source=tactImgList[i];
tactImgList = new BitmapImage[32];
tactImgList[0] = new BitmapImage(new Uri("ms-appx:///Assets/Images/1.png", UriKind.Absolute));
What am I doing wrong? Is there something I need to add to an xaml file or I'm making the global array wrong?
You don't need to use ms-appx in Windows Phone 8 projects , it's for WinRT projects.
Make sure your image's Build type is Content , remove the ms-appx part and since your image has relative uri , set your UriKind to Relative.
First, fill in the data, and then change the source.
tactImgList = new BitmapImage[32];
tactImgList[0] = new BitmapImage(new Uri("ms-appx:///Assets/Images/1.png", UriKind.Absolute));
tactImg.Source=tactImgList[i];
Or use ObservableCollection for storing images.
I want to open an Image from the IsolatedStorage?
The image was downloaded before and correctly written (I checked that with the Isolated Storage Explorer).
When I try to open the Image with BitmapImage(uriInIsolatedStorage) and set that as source for a Silverlight Image Control it crashs when I listen to the image failed event.
The exception said "AG_E_NETWORK_ERROR"
Does anyone has an idea?
Uri imageSource = new Uri("/cover.jpg", UriKind.Relative);
BitmapImage bi = new BitmapImage(imageSource);
bi.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(MainPage_ImageFailed);
bi.ImageOpened +=new EventHandler<RoutedEventArgs>(bi_ImageOpened);
imageCtrl.Source = bi;
Unfortunately you cannot load an image directly from isolated storage via URI. You have to open the file and do some more steps as described here or (a bit easier) here.
It boils down to:
creating an IsolatedStorageFileStream for your image
creating a BitmapImage from file data
setting BitmapImage as Image source
There is also an isostore: URI scheme but it doesn't work.
Morning folks,
I've been trying to cut out some of my app's processing when I stumbled upon a suggestion on SO to load all images into a resource dictionary via BitmapImages, then referencing them rather than loading an image each time. My problem is that I need to do this all programmatically, so:
<BitmapImage x:Key="MyImageSource" UriSource="../Media/Image.png" />
has become:
BitmapImage bI = new BitmapImage();
Uri imgUri = new Uri(fI.FullName, UriKind.RelativeOrAbsolute);
bI.UriSource = imgUri;
DataTemplateKey dTK = new DataTemplateKey(typeof(BitmapImage));
imageDictionary.Add(dTK, bI);
Which I think should work, but as it loops due to the images loading based on database content, I immediatly get a key already added error on the second loop-through. Is there any way to create a datatemplatekey that I can name myself rather than have it based on the type?
Thanks,
Becky
It is not possible to add a key to the datatemplate but maybe you fix your problem by creating a DataTemplateSelector. On the link below you can find very good information on how to do that:
http://www.switchonthecode.com/tutorials/wpf-tutorial-how-to-use-a-datatemplateselector