gpt4 book ai didi

wpf - Powershell 中的自定义 XAML 命名空间

转载 作者:行者123 更新时间:2023-12-03 00:12:12 39 4
gpt4 key购买 nike

我正在尝试使用 Material Design in XAML Toolkit 在 Powershell 中制作 WPF 应用程序但我不能使用“materialDesign”自定义命名空间中的属性。我尝试了各种方法来定义命名空间,但都给出了相同的错误:
"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,我似乎无法让它工作。

一旦我尝试使用文本框属性 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 属性工作正常:

主窗口.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>

应用程序.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"

关于wpf - Powershell 中的自定义 XAML 命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45859958/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com