【问题标题】:Handling spaces in Uri path处理 Uri 路径中的空格
【发布时间】:2021-02-21 12:13:04
【问题描述】:

我有一个来自 DB 的图像的路径,格式为:WPF 中的 Pictures/image_1.jpg 和 我正在使用转换器。

我正在尝试在当前目录所在的位置创建一个 Uri; G:\Projects - Visual Studio\Stamps\Stamps\bin\debug

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType == typeof(ImageSource))
            {
                if (value is string)
                {
                    string str = value.ToString();
                    Uri uri = null;

                    if (str == "None")
                    {
                        uri = new Uri(str, UriKind.RelativeOrAbsolute);
                        return new BitmapImage(new Uri("Pictures/NoImage.jpg", UriKind.RelativeOrAbsolute));
                    }
                    else
                    {
                        // This one does not work because my path has spaces
                        uri = new Uri(str, UriKind.RelativeOrAbsolute);
                        // The following works because it has no spaces in path name
                        //uri = new Uri("Z:/Zipped/" + str);
                        BitmapImage bmi = new BitmapImage();
                        bmi.BeginInit();
                        bmi.UriSource = uri;
                        bmi.EndInit();
                        return bmi;
                    }
                }
                else if (value is Uri)
                {
                    Uri uri = (Uri)value;
                    return new BitmapImage(uri);
                }
            }
            return value;
        }

然后在我的 xmal 文件中;

<Window.Resources>
    <con:ImageToSourceConverter x:Key="ImageSourceConverter"/>
</Window.Resources>

<DataGrid Name="dgUsers" AutoGenerateColumns="False"
          IsReadOnly="True"
          CanUserSortColumns="False"
          VerticalGridLinesBrush="Transparent"
          ItemsSource="{Binding}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Scott #" Binding="{Binding [ScottNumber]}" />
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Image Width="42" Margin="0 5px" Source="{Binding Path=[ImagePath], Converter={StaticResource ImageSourceConverter}}"></Image>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTextColumn Header="Description" Binding="{Binding [Description]}" />
    </DataGrid.Columns>
</DataGrid>

找不到文件的路径,当路径中有空格并且没有空格时,它可以正常工作。 所以我现在的问题是我在创建 Uri 时是否处理路径中的空格?

我也尝试过创建一个绝对 Uri 并用“%20”和“+”替换空格,都导致了异常。

【问题讨论】:

    标签: c# wpf uri


    【解决方案1】:

    This article solved my problem

       var baseUri = Environment.CurrentDirectory;
        uri = new Uri($"file:///{baseUri}" + "/" + str, UriKind.RelativeOrAbsolute);
    

    通过将 file:/// 添加到它成功解析的 Uri!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多