Powershell 中的自定义 XAML 命名空间
Posted
技术标签:
【中文标题】Powershell 中的自定义 XAML 命名空间【英文标题】:Custom XAML Namespace in Powershell 【发布时间】:2018-02-02 05:43:14 【问题描述】:我正在尝试使用 Material Design in XAML Toolkit 在 Powershell 中制作 WPF 应用程序,但我无法使用“materialDesign”自定义命名空间中的属性。我尝试了各种方法来定义命名空间,但都给出了相同的错误:
"Exception calling "Load" with "1" argument(s): "Cannot set unknown member 'http://materialdesigninxaml.net/winfx/xaml/themesHintAssist.Hint'.""
我有一个 Visual Studio 项目,我在其中通过 NuGet 安装了 Material Design Themes 包,该包使用相同的属性可以正常工作,但如果我复制 XAML 以在 Powershell 中解析,我似乎无法使其工作。
一旦我尝试使用文本框属性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 项目的代码,其中 HintAssist 属性可以正常工作:
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 命名空间的资源,如果有人能帮助我,我将不胜感激。
【问题讨论】:
【参考方案1】:我设法解决了这个问题。
如果我将命名空间定义如下,只要 DLL 文件命名相同,它就可以工作:
xmlns:materialDesign="clr-namespace:MaterialDesignThemes.Wpf;assembly=MaterialDesignThemes.Wpf"
【讨论】:
以上是关于Powershell 中的自定义 XAML 命名空间的主要内容,如果未能解决你的问题,请参考以下文章