首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell中的自定义XAML命名空间

Powershell中的自定义XAML命名空间
EN

Stack Overflow用户
提问于 2017-08-24 19:03:46
回答 1查看 1.6K关注 0票数 4

我正在尝试使用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,我似乎无法让它工作。

只要我尝试使用textbox属性materialDesign:HintAssist.Hint="Name",它就会崩溃。

Powershell代码

代码语言:javascript
复制
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:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<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>

期望的结果是让textbox拥有一个faded text in it until the user enters something

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

EN

回答 1

Stack Overflow用户

发布于 2017-08-29 00:24:34

我设法解决了这个问题。

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

代码语言:javascript
复制
xmlns:materialDesign="clr-namespace:MaterialDesignThemes.Wpf;assembly=MaterialDesignThemes.Wpf"
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45859958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档