C# - 尝试将文件路径添加到 NavigateUri 中,每个用户可能会有所不同

问题描述 投票:0回答:1

我正在尝试将文件路径超链接添加到我的 WPF 项目中,该项目的部分内容对于每个用户而言可能会有所不同。我知道一个注册表位置,我可以在其中获取路径的第一部分,并且路径的第二部分将始终相同。

例如,我尝试访问的路径(以这种方式编码时有效)在我的 .xaml 文件中如下所示:

<TextBlock Grid.Row="1" Grid.Column="1" HorizontalAlignment="Right" FontSize="18"
           FontWeight="Bold" Margin="0,-35,140,0">
    <Hyperlink RequestNavigate="Hyperlink_RequestNavigate"
               NavigateUri="G:\Applications\SimHub\DashTemplates\OMSC - Settings\readme.pdf">Readme Link</Hyperlink>
</TextBlock>

第一部分“G:\Applications\SimHub”是有可能更改的部分,第二部分“\DashTemplates\OMSC - 设置” eadme.pdf”是始终相同的部分。

我知道注册表函数和绑定方法,但我不知道如何正确使用它们来获得我需要的结果。

最终我需要做的是,我需要一个函数来从注册表中获取路径的“变量”部分。然后需要在末尾添加路径的“常量”部分(我假设整个路径需要成为一个字符串)。然后,我需要将 .xaml 中的 NavigateUri 部分绑定到代码中创建的结果字符串。

有人可以帮忙吗? :)

c# wpf-controls
1个回答
0
投票

您可以使用值转换器。在这里,我将 Navigate Uri 绑定到自定义转换器,该转换器将在注册表中查找应用程序的基本路径,然后将路径附加到某个文件。

using Microsoft.Win32;
using System;
using System.Globalization;
using System.IO;
using System.Windows.Data;

 namespace ApplicationHyperLink
 {
    internal class LocationConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // registry information about what you are looking for. 
            // here i am looking to see where 7-Zip is installed
            string keyPath = $"SOFTWARE\\7-Zip";
            string valueName = "Path";
            string location = string.Empty;
            try
            {
                using (RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(keyPath))
                {
                     if (key != null)
                     {
                        object val = key.GetValue(valueName);

                        if (val != null && val is string)
                        {
                            location = (string)val;
                        }
                    }
                }
             }
             catch (Exception ex)
             {
                 Console.WriteLine($"Error accessing registry:{ex.Message}");
                 return string.Empty;
             }

             // here i am appending the static path to whatever you need. 
             // in this case its a file in the Lang directory. 
             return Path.Combine(location, "Lang", "af.txt");
         }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

您将转换器用作资源并将其绑定到 uri。

    <Window
    x:Class="ApplicationHyperLink.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:ApplicationHyperLink"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    <Window.Resources>
        <local:LocationConverter x:Key="LocationConverter" />
    </Window.Resources>
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">
        <Hyperlink NavigateUri="{Binding Converter={StaticResource LocationConverter}}" RequestNavigate="Hyperlink_RequestNavigate">
            Link
        </Hyperlink>
    </TextBlock>
</Window>

您可以将一个值绑定到 uri 并且仍然使用转换器。在这种情况下,绑定值将作为转换函数的值参数传递。最重要的是,您可以传入参数值。如果您想变得更奇特并使转换器可重用,您可以绑定应用程序并使用静态路径作为参数。

© www.soinside.com 2019 - 2024. All rights reserved.