【问题标题】:Defining and using resources from code and xaml从代码和 xaml 定义和使用资源
【发布时间】:2014-07-22 14:19:54
【问题描述】:

我正在尝试将颜色、画笔和其他一些东西定义为系统资源,然后在代码和 xaml 中使用它们,就像我在 AppResources.resx 中定义了字符串并在类似代码中使用它们一样

MyApp.Resources.AppResources.MyStringResource

和在 xaml 中一样

Text="{Binding Source={StaticResource LocalizedStrings}, Path=LocalizedResources.MyStringResource}"

我在寻找解决方案时在几个地方遇到了ResourceDictionary。但我不知道如何以及在哪里添加ResourceDictionary

我尝试像这样在 App.xaml 中添加它:

<Application.Resources>
        <ResourceDictionary>
            <local:LocalizedStrings xmlns:local="clr-namespace:DataBoundApp1" x:Key="LocalizedStrings"/>
            <SolidColorBrush Color="#FF0000" x:Key="ErrorColor"></SolidColorBrush>
        </ResourceDictionary>
</Application.Resources>

但我不知道如何从 Code 和 xaml 中使用它。还有其他方法或地方可以让我添加ResourceDictionary 或任何其他可以在以后反复使用的东西。

请帮忙!

【问题讨论】:

    标签: c# xaml windows-phone-8 windows-phone


    【解决方案1】:

    我在尝试实现这一目标时遇到了很多解决方案,最终得到了最好的解决方案。只需按照以下步骤操作即可。

    首先在您的项目中创建一个名为Themes 的文件夹,并在其中添加一个类文件。将类文件命名为 Generic.xaml,替换现有的 C# 代码并将以下文件添加到其中。

    <ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">    
    <SolidColorBrush Color="#FF0000" x:Key="ErrorColor"></SolidColorBrush>
    </ResourceDictionary>
    

    然后重建项目。然后在 App.xaml 中添加以下代码行。

    <Application.Resources>
       <ResourceDictionary>
           <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="/Themes/Generic.xaml"/>
           </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    

    最后像这样在项目中任意位置应用样式,

    <TextBlock Text="Hello" Foreground="{StaticResource ErrorColor}"/>
    

    Generic.xaml 中添加您想要的样式并在您的项目中调用这些样式。

    【讨论】:

    • 这是我一直在寻找的解决方案。但它给出了这个错误。 Failed to assign to property 'System.Windows.ResourceDictionary.Source'. 在我添加 MergedDictionaries 的 App.xaml 中
    • 尝试在 App.xaml 的 ResourceDictionary 的源代码中添加 /YourProjectName;component/Themes/Generic.xaml。
    • 酷。有效。但是你能解释一下为什么它现在有效,为什么不更早。我在很多地方看到 Source 被简单地定义为Source="/Themes/Generic.xaml"。那么为什么它对我的情况不起作用
    • 无法在您的代码中直接使用 generic.xaml (31783) 中的源的相对 URI 存在一个错误。在这种情况下,非相对形式应该可以正常工作。
    • 好的。感谢您的帮助。
    【解决方案2】:

    您可以像这样在 xaml 中使用solidColorBrush:

    <Border BorderThickness="1"
            BorderBrush="{StaticResource ErrorColor}">
    </Border>
    

    See link for more details on using Application Resources

    See here for a more in depth look at Xaml resources in general

    编辑

    在后面的代码中,虽然我不建议这样做,因为你会犯更多错误:

    SolidColorBrush = this.FindResource("ErrorColor") as SolidColorBrush;
    

    【讨论】:

    • 好的。但是如何在 Code 中使用它呢?
    • 查看编辑以了解如何在代码中使用 Xaml 资源
    • FindResourceTryFindResource 不适用于 Windows Phone。
    • 我得到了代码的解决方案。我已经像这样使用它App.Current.Resources["ErrorColor"]。但是感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多