xaml - 在 Metro 应用程序中使用来自另一个程序集的样式

标签 xaml windows-8 microsoft-metro

我从 Windows 8 C# XAML user and custom controls sample 开始并移动了文件

Themes/Generic.xaml
BasicCustomControl.cs
BasicUserControl.xaml
BasicUserControl.xaml.cs
ImageWithLabelControl.cs

到名为 Controls 的 Metro 类库,在 UserAndCustomControls 项目中引用它并更正 local:...xmlns:local="using:Controls" 的引用。这很好用。

但是如果在类库中创建一个资源字典 "Style.xaml"
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Controls">
    <Color x:Key="ColorBackground">Red</Color>    
</ResourceDictionary>

并包含在 ScenarioList.xaml 文件中
<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Controls;component/Style.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Page.Resources>

我收到运行时错误
XamlParseException
Failed to assign to property 'Windows.UI.Xaml.ResourceDictionary.Source'.

如果我尝试将颜色应用于网格
<Grid>
    <Grid.Background>
        <SolidColorBrush Color="{StaticResource ColorBackground}" />
    </Grid.Background>

    <ListBox x:Name="Scenarios" ...
    [...]
</Grid>

[Q] 问题是,如何在 Metro 应用程序中正确声明、引用和使用外部样式?我的想法是创建作为单个 dll 文件提供的可重用控件和通用样式。

最佳答案

您的源路径错误,因为不支持组件语法。假设您的控件库 DLL 被称为“控件”,那么它会是这样的:

<ResourceDictionary Source="ms-appx:///Controls/Files/Style.xaml" />

您可以查看 http://timheuer.com/blog/archive/2012/03/07/creating-custom-controls-for-metro-style-apps.aspx 以获得更深入的解释。

关于xaml - 在 Metro 应用程序中使用来自另一个程序集的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9911435/

相关文章:

.net - 属性(property)无法识别或无法访问 Telerik

javascript - 使用 WinJS 在 Windows Metro 中进行优雅的异常处理

html - 即使控件是从 HTML/JS 或 XAML 创建的,Metro 控件是否会在低级别重用相同的代码路径?

c# - 如何使用 MVVM 根据 Canvas 大小居中形状

C# WPF 限制 ListView 中每行的项目

visual-c++ - 如何在 Metro 应用程序中写入控制台/登录 VC++?

.net - Windows 8 商店应用程序是否支持 Ms Access 数据库?

javascript - Windows 8 : Wait until setup tasks complete before tearing down extended splash screen

javascript - 溢出 div 上的 "Scroll down to update"(或左侧)特征

c# - 如何从 Win8 Javascript metro 应用程序引用 C# 类库?