这个Urikind.relative究竟是什么意思

Dor*_*eka 8 c# wpf datagridview

我正在做的是当我选择一行并单击显示按钮时,我将有一个datagridview我想显示图像以及一些信息,我写了下面的代码

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我将我的图像复制并粘贴到Bin中,则无法正常工作.

所以我想知道关于这个UriKInd的一些事情

mpe*_*pen 12

相对而言,与绝对相对."chicken/pot/pie.jpg"是相对的,因为它对于当前目录.而像"C:/images/food/chicken/pot/pie.jpg"这样的东西是绝对的,因为它......错误......相对于驱动器的.

以这种方式初始化Uri的唯一真正意义是当uri格式不正确时避免(或导致)异常; 当Uri未预先确定时有用.

MSDN参考

相对URI和绝对URI对其格式有不同的限制.例如,相对URI不需要方案或权限.您指定的值uriKind必须与传入的URI类型相匹配uriString.但是,如果指定了RelativeOrAbsolute,则URI字符串可以是相对的或绝对的.


Jon*_*eet 10

这意味着它是一个相对URI - 但如果没有您提供更多信息,我们不知道它的相对性.

例如,这里的URI是相对的:

<img src="person.jpg" />
Run Code Online (Sandbox Code Playgroud)

...这样,虽然它在某种程度上比前一个"相对"更少,因为它只依赖于现有的主机/帖子/方案而不是当前URI的路径:

<img src="/images/person.jpg" />
Run Code Online (Sandbox Code Playgroud)

这是绝对的 - 它包含所需的所有信息,没有任何其他上下文:

<img src="http://microsoft.com/images/person.jpg" />
Run Code Online (Sandbox Code Playgroud)

UriKind文档:

绝对URI的特点是对资源的完整引用(例如:http://www.contoso.com/index.html),而相对Uri依赖于先前定义的基URI(例如:/index.html).

如果没有更多信息,很难说你遇到了什么问题.