What exactly mean by this Urikind.relative

拥有回忆 提交于 2020-01-01 09:23:10

问题


What i am doing is i will have a datagridview when i select a row and click on show button i would like to display the image along with some information for that i written the following code

public partial class WpfWindow : Window
{
    private UCPersons _ucPersons;

    public WpfWindow()
    {
        InitializeComponent();

        // Create WinForms Usercontrol and 
        // add it to the WindowsFormsHost
        _ucPersons = new UCPersons();
        winFormHost.Child = _ucPersons;

        List<Person> persons = CreateSamplePersons();
        _ucPersons.SetData(persons);


    }

    private List<Person> CreateSamplePersons()
    {
        List<Person> persons = new List<Person>();
        persons.Add(Person.Create("Dorababu", "Madhuranagar", "Hyd", 
            DateTime.Now.AddYears(-34), "1"));

        persons.Add(Person.Create("Krish", "Sat", "RJY",
            DateTime.Now.AddYears(-64), "2"));


        return persons;
    }

    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);
        }
    }
}

But the same is not working if i copy and paste my images out of Bin.

So i would like to know some thing about this UriKInd


回答1:


Relative, as opposed to Absolute. "chicken/pot/pie.jpg" would be relative, because it's relative to the current directory. Whereas something like "C:/images/food/chicken/pot/pie.jpg" would be absolute because it's ...err... relative to the root of the drive.

The only real point to initializing the Uri this way is to avoid (or cause) exceptions when the uri is not well-formed; useful when the Uri is not predetermined.

MSDN Reference

Relative and absolute URIs have different restrictions on their format. For example, a relative URI does not require a scheme or an authority. The value you specify in uriKind must match the type of URI passed in uriString. However, if RelativeOrAbsolute is specified, the URI string can be relative or absolute.




回答2:


It means it's a relative URI - but we don't know what it's relative to without you providing more information.

As an example, the URI here is relative:

<img src="person.jpg" />

... and so is this, although it's in some ways "less" relative than the previous one, as it only relies on the existing host/post/scheme rather than the current URI's path:

<img src="/images/person.jpg" />

This is absolute though - it contains all the information required, without any other context:

<img src="http://microsoft.com/images/person.jpg" />

From the UriKind documentation:

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).

It's hard to say what problem you're having without more information though.



来源:https://stackoverflow.com/questions/5242391/what-exactly-mean-by-this-urikind-relative

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!