简体   繁体   English

在WPF中应用程序中创建可换肤图像的最佳方法

[英]Best way to create a skinnable images in application in WPF

I have a big WPF application and I have to make it skinnable. 我有一个很棒的WPF应用程序,我必须让它可以换肤。 My problem is that I have a lot of icons created with fireworks (so they are vector images). 我的问题是我有很多用烟花创建的图标(因此它们是矢量图像)。

Now, all of them are white. 现在,他们都是白人。 If I will want the entire application to have a blue theme, then all my icons has to be blue. 如果我希望整个应用程序都有蓝色主题,那么我的所有图标都必须是蓝色的。 I would like not to replace the pictures with others in order to do that. 为了做到这一点,我不想替换他人的图片。 Is there any way to change the color of an image in WPF? 有没有办法在WPF中更改图像的颜色? A specific format? 具体格式?

If they are all vector images then you should be able to export the paths directly and use them in a Path to which you apply colour separately. 如果它们都是矢量图像,那么您应该能够直接导出路径并在分别应用颜色的Path使用它们。

see Xaml abbreviated syntax in http://msdn.microsoft.com/en-us/library/ms747393(v=vs.110).aspx 请参阅http://msdn.microsoft.com/en-us/library/ms747393(v=vs.110).aspx中的 Xaml缩写语法

and http://www.adobe.com/devnet/fireworks/articles/fw_xaml_panel.html http://www.adobe.com/devnet/fireworks/articles/fw_xaml_panel.html

In this case, you can use DrawingImage . 在这种情况下,您可以使用DrawingImage He is responsible for displaying the Image , which consists of several parts of the vector graphics. 他负责显示ImageImage由矢量图形的几个部分组成。

The DrawingGroup is a collection of figures, which can be accessed from the code and set the in the cycle for each desired color. DrawingGroup是一组图形,可以从代码中访问,并在每个所需颜色的循环中设置。

Here is a sample: 这是一个示例:

XAML

<Window.Resources>
    <DrawingGroup x:Key="drawingGroup">
        <GeometryDrawing Brush="#FF22BAFD" Geometry="F1 M 14.72,15.68L 12.38,15.68L 7.205,5.92L 7.11,5.92L 2.29,15.68L 0,15.68L 6.58,2.56L 7.595,2.56L 14.72,15.68 Z "/>
        <GeometryDrawing Brush="#FF22BAFD" Geometry="F1 M 21.585,25.6C 21.1017,25.6 20.69,25.4275 20.35,25.0825C 20.01,24.7375 19.84,24.3267 19.84,23.85C 19.84,23.37 20.01,22.955 20.35,22.605C 20.69,22.255 21.1017,22.08 21.585,22.08C 22.0783,22.08 22.4975,22.255 22.8425,22.605C 23.1875,22.955 23.36,23.37 23.36,23.85C 23.36,24.3267 23.1875,24.7375 22.8425,25.0825C 22.4975,25.4275 22.0783,25.6 21.585,25.6 Z "/>
        <GeometryDrawing Brush="#FF22BAFD" Geometry="F1 M 28.625,25.6C 28.1417,25.6 27.73,25.4275 27.39,25.0825C 27.05,24.7375 26.88,24.3267 26.88,23.85C 26.88,23.37 27.05,22.955 27.39,22.605C 27.73,22.255 28.1417,22.08 28.625,22.08C 29.1183,22.08 29.5375,22.255 29.8825,22.605C 30.2275,22.955 30.4,23.37 30.4,23.85C 30.4,24.3267 30.2275,24.7375 29.8825,25.0825C 29.5375,25.4275 29.1183,25.6 28.625,25.6 Z "/>
        <GeometryDrawing Brush="#FF22BAFD" Geometry="F1 M 35.665,25.6C 35.1817,25.6 34.77,25.4275 34.43,25.0825C 34.09,24.7375 33.92,24.3267 33.92,23.85C 33.92,23.37 34.09,22.955 34.43,22.605C 34.77,22.255 35.1817,22.08 35.665,22.08C 36.1583,22.08 36.5775,22.255 36.9225,22.605C 37.2675,22.955 37.44,23.37 37.44,23.85C 37.44,24.3267 37.2675,24.7375 36.9225,25.0825C 36.5775,25.4275 36.1583,25.6 35.665,25.6 Z "/>
        <GeometryDrawing Brush="#FF22BAFD" Geometry="F1 M 48.96,25.155L 48.96,28.48L 47.36,28.48L 47.36,25.28C 45.1267,25.28 43.3133,24.8217 41.92,23.905L 41.92,21.12C 42.5267,21.6633 43.3567,22.1192 44.41,22.4875C 45.4633,22.8558 46.4467,23.04 47.36,23.04L 47.36,15.14C 45.08,14.04 43.6033,13.0258 42.93,12.0975C 42.2567,11.1692 41.92,10.0717 41.92,8.805C 41.92,7.30167 42.4325,6.0025 43.4575,4.9075C 44.4825,3.8125 45.7833,3.15667 47.36,2.94L 47.36,9.53674e-007L 48.96,9.53674e-007L 48.96,2.88C 51.12,2.94333 52.6133,3.23333 53.44,3.75L 53.44,6.4C 52.3167,5.60667 50.8233,5.18 48.96,5.12L 48.96,13.24C 51.1733,14.27 52.6867,15.2658 53.5,16.2275C 54.3133,17.1892 54.72,18.2833 54.72,19.51C 54.72,20.9867 54.2117,22.2267 53.195,23.23C 52.1783,24.2333 50.7667,24.875 48.96,25.155 Z M 47.36,12.37L 47.36,5.215C 46.4733,5.38833 45.7717,5.76917 45.255,6.3575C 44.7383,6.94583 44.48,7.66 44.48,8.5C 44.48,9.38 44.6908,10.1017 45.1125,10.665C 45.5342,11.2283 46.2833,11.7967 47.36,12.37 Z M 48.96,15.945L 48.96,22.915C 51.0933,22.4817 52.16,21.4133 52.16,19.71C 52.16,18.29 51.0933,17.035 48.96,15.945 Z "/>
    </DrawingGroup>

    <DrawingImage x:Key="drawingImage" 
                  Drawing="{StaticResource drawingGroup}" />
</Window.Resources>

<Grid>
    <Image Name="TestImage"
           Source="{StaticResource drawingImage}"
           Width="100"
           Height="100"/>

    <Button Content="TestClick"
            HorizontalAlignment="Center" 
            VerticalAlignment="Top" 
            Click="Button_Click" />
</Grid>

Code-behind

private void Button_Click(object sender, RoutedEventArgs e)
{
    DrawingGroup group = this.Resources["drawingGroup"] as DrawingGroup;

    foreach (GeometryDrawing geometry in group.Children)
    {
        geometry.Brush = Brushes.Red;
    }
 }

In this sample for each figure set the Red color. 在每个图的样本中设置Red Also you can set different colors for each figure, which is in DrawingGroup . 您还可以为每个图形设置不同的颜色,这在DrawingGroup

Or you can use to Path if your icon fit into in it. 或者,如果您的图标符合其中,则可以使用Path In Resources App.xaml , <Window.Resources> , etc. add Path with key: 在Resources App.xaml<Window.Resources>等中,使用键添加Path:

<Path x:Key="MyPath" Data="F1 M 0,0L ..." />

And in Style or where else use like this: Style或其他地方使用如下:

<Path x:Name="MyPathButton"
      ...
      Fill="{StaticResource ButtonBackground}" 
      Data="{Binding Source={StaticResource MyPath}, Path=Data}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建应用程序以从 PDF 创建 JPEG 图像(缩略图)的最佳方式 - Best Way to Create Application to Create JPEG Images (thumbnails) From PDFs 制作可换肤用户界面组件的第二好的方法是什么? - What's the second-best way to make a skinnable user interface component? 在WPF应用程序中提高性能的最佳方法 - Best way to improve performance in WPF application 创建应用程序图标的最佳方法 - Best way to create an application icon 在Prism 6多模块WPF MVVM应用程序中创建共享串行端口访问的最佳方法是什么? - What is the best way to create shared serial port access in Prism 6 multimodule WPF MVVM application? 在WPF应用程序的mvvm中实现导航的最佳方法 - Best way to implement navigation in mvvm for wpf application 从目录在ListBox WPF或Silverlight中显示图像的最佳方法是什么 - What is the BEST Way to display images in a ListBox WPF or Silverlight FROM a directory 在C#中创建数据库应用程序的最佳方式 - Best way to create database application in C# WPF Caliburn.Micro - 在Singe Window应用程序中导航的最佳方式 - WPF Caliburn.Micro - Best way to navigate in Singe Window Application 在WPF应用程序中构造非可视方法的最佳方法? - Best way to structurizing non-visual methods in the WPF application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM