Invalid URI: The format of the URI could not be determined - C#

前端 未结 3 2041
醉酒成梦
醉酒成梦 2020-12-19 12:17

Im trying to set the source of a resource dictionary in c# to a location of a folder within the project, but get the above error.

Could someone advise what the issue

相关标签:
3条回答
  • 2020-12-19 13:02

    I kept running into the same issue even after passing the UriKind.Relative argument. The weird bit was that some of the Uris to XAML pages were working using the method @Magnus suggested - but most of them would throw the:

    The format of the URI could not be determined

    exception.

    Finally, I read up on Pack Uris for WPF applications which solved my problem a 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 struggling with the same issues I was facing.

    0 讨论(0)
  • 2020-12-19 13:04

    I was trying to set the background of a 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 讨论(0)
  • 2020-12-19 13:22

    You have to use UriKind.Relative

    myResourceDictionary.Source = new Uri("../Resources/Styles/Shared.xaml",  UriKind.Relative);
    
    0 讨论(0)
提交回复
热议问题