Invalid URI: URI format could not be determined - C #

I am trying to set the source of a resource dictionary in C # to a folder location in a project, but I get the above error.

Can anyone please advise what the problem is?

Here is the code:

myResourceDictionary.Source = new Uri("../Resources/Styles/Shared.xaml");

      

Please let me know if you need more information.

+3


source to share


3 answers


You have to use UriKind.Relative



myResourceDictionary.Source = new Uri("../Resources/Styles/Shared.xaml",  UriKind.Relative);

      

+8


source


I kept facing the same question even after passing the argument UriKind.Relative

. The weird bit was that some XAML Uris pages worked using @Magnus suggested method, but most of them were throwing:

The format of the URI could not be determined

an exception.

Finally, I read Pack Uris for WPF Apps which solved my problem 100%.

I started using URLs like this:



// ResourceFile is in the Root of the Application
myResourceDictionary.Source = 
    new Uri("pack://application:,,,/ResourceFile.xaml", UriKind.RelativeOrAbsolute);

      

or

// ResourceFile is in the Subfolder of the Application
myResourceDictionary.Source = 
    new Uri("pack://application:,,,/SubFolder/ResourceFile.xaml", UriKind.RelativeOrAbsolute);

      

Hope this helps someone else wrestling with the same problems I am facing.

+1


source


I was trying to set the background of the window to an ImageBrush.

Changing UriKind.Absolute to UriKind.Relative fixed the problem for me.

private void SetBackgroundImage()
{
    ImageBrush myBrush = new ImageBrush
    {
        ImageSource =
        new BitmapImage(new Uri("background.jpg", UriKind.Relative))
    };
    this.Background = myBrush;
}

      

0


source







All Articles