PowerShell中的自定义XAML命名空间

人气:158 发布:2023-01-03 标签: wpf xaml powershell xml-namespaces visual-studio-2015

问题描述

我正在尝试使用Material Design in XAML Toolkit在PowerShell中创建一个WPF应用程序,但我无法使用自定义命名空间中的属性。我尝试了各种方法来定义命名空间,但都给出了相同的错误:

"Exception calling "Load" with "1" argument(s): "Cannot set unknown member '{http://materialdesigninxaml.net/winfx/xaml/themes}HintAssist.Hint'.""

我有一个Visual Studio项目,其中我通过NuGet安装了Material Design Themes包,该包与相同的属性一起工作得很好,但如果我复制要在PowerShell中分析的XAML,似乎无法使其工作。

只要我尝试使用TextBox属性materialDesign:HintAssist.Hint="Name",它就会断开。

PowerShell代码

Import-Module "$($PSScriptRoot)MaterialDesignColors.dll"
Import-Module "$($PSScriptRoot)MaterialDesignThemes.wpf.dll"

[xml]$xaml = @"
<Window
        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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        Title="MainWindow" Width="1040" Height="495"

        TextElement.Foreground="{DynamicResource MaterialDesignBody}"
        Background="{DynamicResource MaterialDesignPaper}"
        TextElement.FontWeight="Medium"
        TextElement.FontSize="14"
        FontFamily="pack://application:,,,/MaterialDesignThemes.Wpf;component/Resources/Roboto/#Roboto"
        >

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Amber.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.DeepPurple.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    <Grid>
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="27" Margin="142,266,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" materialDesign:HintAssist.Hint="Name"/>
    </Grid>

</Window>

"@

$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load( $reader )

$Window.ShowDialog() | Out-Null

Visual Studio项目代码

下面是来自Visual Studio项目的代码,在该项目中,HintAssistt属性工作正常:

MainWindow.xaml:

<Window x:Class="CreatePrinterWPF.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    mc:Ignorable="d"
    Title="MainWindow" Width="1040" Height="495"

    TextElement.Foreground="{DynamicResource MaterialDesignBody}"
    Background="{DynamicResource MaterialDesignPaper}"
    TextElement.FontWeight="Medium"
    TextElement.FontSize="14"
    FontFamily="pack://application:,,,/MaterialDesignThemes.Wpf;component/Resources/Roboto/#Roboto"
    >
<Grid>
    <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="27" Margin="142,266,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" materialDesign:HintAssist.Hint="Name"/>
</Grid>

App.xaml:

<Application x:Class="CreatePrinterWPF.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:CreatePrinterWPF"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>

            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Amber.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.DeepPurple.xaml" />

        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

所需的结果是文本框有一个 faded text in it until the user enters something。

我似乎找不到任何有关使用PowerShell中已编译的库文件中的XAML命名空间的资源,如果有人能帮助我,我将不胜感激。

推荐答案

我设法解决了该问题。

如果我按如下方式定义命名空间,只要DLL文件的名称相同,它就可以工作:

xmlns:materialDesign="clr-namespace:MaterialDesignThemes.Wpf;assembly=MaterialDesignThemes.Wpf"

22