Reputation: 1703
I have some trouble with uri in my C# code
This is working:
var uri = new Uri("d:/Programozas dotNet/Ugyfel/Ugyfel.ClientWpf/Skins/MainSkin.xaml");
But unfortunately non of these:
var uri = new Uri("/Skins/MainSkin.xaml", UriKind.Relative);
var uri = new Uri("pack://application:,,,/Skins/MainSkin.xaml");
var uri = new Uri("Ugyfel.ClientWpf;/Skins/MainSkin.xaml", UriKind.Relative);
IOException: Cannot locate resource 'skins/mainskin.xaml'.
How can I use relative uri insted of absolute?
Upvotes: 6
Views: 33292
Reputation: 1703
That was a stupid misstake. The correct method is without leading "/"
var uri = new Uri("Skins/MainSkin.xaml", UriKind.Relative);
Thank you for your effort
Upvotes: 29
Reputation: 9019
Urikind.Relative
makes sense when you are trying to access a resource in the same location where the app is located. For example, in this code, it is using an image located in the bin folder of the app.
private void btnDisplayDetails_Click(object sender, RoutedEventArgs e)
{
Person person = _ucPersons.GetSelectedPerson();
if (person != null)
{
lblName.Content = person.Name;
lblAge.Content = person.BirthDay.ToShortDateString();
Uri uri = new Uri( "m_" + person.ImageRef + ".jpg",
UriKind.Relative);
imgPerson.Source = BitmapFrame.Create(uri);
}
}
Courtesy: What exactly mean by this Urikind.relative
Or it is also used when you already have defined a base URI. Check the UriKind MSDN link:
Absolute URIs are characterized by a complete reference to the resource (example: http://www.contoso.com/index.html), while a relative Uri depends on a previously defined base URI (example: /index.html).
Upvotes: 0
Reputation: 13286
Relative Url (in WPF or any other desktop application) is relative to Environment.CurrentDirectory. Usually this is the folder where your exe resides, but it can be different in VS Unit Testing environment.
I assume that in your project you have a folder called "Skins", and probably your exe is in "bin\debug".
The easiest thing to do is to set the MainSkin.xaml to be copied to the output directory (in the file properties) so you can refer to it with the name only with no path.
Upvotes: 4