【问题标题】:How get this xaml to work from app.xaml如何让这个 xaml 从 app.xaml 工作
【发布时间】:2009-08-26 06:35:21
【问题描述】:

我通过将我们的图形人员制作的 .ai 文件转换为 UpButton.xaml 文件制作了这个文件,但我无法让它工作。 我正在考虑为按钮制作某种模板/样式,但我们的按钮是由矩形而不是按钮对象组成的。

自上周四以来我一直在摆弄这个,谷歌搜索并尝试了我找到的所有东西,但它不起作用。

我们希望将其作为“资源”放在单独的 xaml 文件中,然后将其添加到 app.xaml 中,并让它在整个应用程序中绘制所有矩形。 我们正在构建的应用程序应该使用 xaml 和尽可能少的 C# 编码。

谁能帮我搞定这件事?

感谢您让我了解代码示例 :)

<?xml version="1.0" encoding="utf-8"?>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <DrawingBrush x:Key="UnnamedObject0" Stretch="Uniform">
        <DrawingBrush.Drawing>
            <DrawingGroup>
                <DrawingGroup.Children>
                    <GeometryDrawing Geometry="F1 M 141.781,503.813C 140.737,503.813 139.891,504.659 139.891,505.703L 139.891,524.601C 139.891,525.645 140.737,526.491 141.781,526.491L 160.679,526.491C 161.723,526.491 162.568,525.645 162.568,524.601L 162.568,505.703C 162.568,504.659 161.723,503.813 160.679,503.813L 141.781,503.813 Z ">
                        <GeometryDrawing.Brush>
                            <LinearGradientBrush StartPoint="0.499992,-1.61488e-005" EndPoint="0.499992,1">
                                <LinearGradientBrush.GradientStops>
                                    <GradientStop Color="#FFD1D3D4" Offset="0"/>
                                    <GradientStop Color="#FFFFFFFF" Offset="1"/>
                                </LinearGradientBrush.GradientStops>
                            </LinearGradientBrush>
                        </GeometryDrawing.Brush>
                    </GeometryDrawing>
                    <DrawingGroup ClipGeometry="F1 M 0,0L 793.701,0L 793.701,1122.52L 0,1122.52L 0,0 Z ">
                        <DrawingGroup.Children>
                            <GeometryDrawing Geometry="F1 M 139.891,505.703C 139.891,504.659 140.737,503.814 141.781,503.814L 160.679,503.814C 161.723,503.814 162.568,504.659 162.568,505.703L 162.568,524.6C 162.568,525.644 161.723,526.491 160.679,526.491L 141.781,526.491C 140.737,526.491 139.891,525.644 139.891,524.6L 139.891,505.703 Z ">
                                <GeometryDrawing.Pen>
                                    <Pen Thickness="1.33333" LineJoin="Round" Brush="#FFA7A9AC"/>
                                </GeometryDrawing.Pen>
                            </GeometryDrawing>
                        </DrawingGroup.Children>
                    </DrawingGroup>
                    <GeometryDrawing Geometry="F1 M 144.048,506.081C 143.005,506.081 142.159,506.927 142.159,507.969L 142.159,522.333C 142.159,523.377 143.005,524.223 144.048,524.223L 158.412,524.223C 159.455,524.223 160.301,523.377 160.301,522.333L 160.301,507.969C 160.301,506.927 159.455,506.081 158.412,506.081L 144.048,506.081 Z ">
                        <GeometryDrawing.Brush>
                            <LinearGradientBrush StartPoint="0.499951,1.00003" EndPoint="0.499951,-3.36442e-006">
                                <LinearGradientBrush.GradientStops>
                                    <GradientStop Color="#FFE6E8E8" Offset="0"/>
                                    <GradientStop Color="#FFFFFFFF" Offset="1"/>
                                </LinearGradientBrush.GradientStops>
                            </LinearGradientBrush>
                        </GeometryDrawing.Brush>
                    </GeometryDrawing>
                    <GeometryDrawing Geometry="F1 M 144.645,515.816L 147.521,515.816L 147.521,521.56L 151.24,521.56L 154.939,521.56L 154.939,515.816L 157.813,515.816L 151.219,508.743L 144.645,515.816 Z ">
                        <GeometryDrawing.Brush>
                            <LinearGradientBrush StartPoint="0.499986,1.00003" EndPoint="0.499986,5.23812e-005">
                                <LinearGradientBrush.GradientStops>
                                    <GradientStop Color="#FF50C400" Offset="0"/>
                                    <GradientStop Color="#FF91F456" Offset="1"/>
                                </LinearGradientBrush.GradientStops>
                            </LinearGradientBrush>
                        </GeometryDrawing.Brush>
                    </GeometryDrawing>
                    <DrawingGroup ClipGeometry="F1 M 0,0L 793.701,0L 793.701,1122.52L 0,1122.52L 0,0 Z ">
                        <DrawingGroup.Children>
                            <GeometryDrawing Geometry="F1 M 151.24,521.56L 147.521,521.56L 147.521,515.815L 144.646,515.815L 151.218,508.743L 157.814,515.815L 154.938,515.815L 154.938,521.56L 151.24,521.56 Z ">
                                <GeometryDrawing.Pen>
                                    <Pen Thickness="1.33333" LineJoin="Round" Brush="#FF076F00"/>
                                </GeometryDrawing.Pen>
                            </GeometryDrawing>
                        </DrawingGroup.Children>
                    </DrawingGroup>
                </DrawingGroup.Children>
            </DrawingGroup>
        </DrawingBrush.Drawing>
    </DrawingBrush>
</ResourceDictionary>

【问题讨论】:

  • 发布它,我将对其进行编辑,使其正确。但是您可以将其全部复制到文本框中,选择它,然后单击看起来像“1010”的按钮。
  • 要发布代码示例,只需将 xmal 粘贴到编辑窗口中,突出显示代码部分,然后按代码示例按钮。这将为您格式化。

标签: c# .net wpf xaml app.xaml


【解决方案1】:

你需要把它写成这样的样式:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type Rectangle}">
        <Setter Property="Fill">
            <Setter.Value>
                <DrawingBrush Stretch="Uniform">
                    <DrawingBrush.Drawing>
                        <DrawingGroup>
                            <DrawingGroup.Children>
                                <GeometryDrawing Geometry="F1 M 141.781,503.813C 140.737,503.813 139.891,504.659 139.891,505.703L 139.891,524.601C 139.891,525.645 140.737,526.491 141.781,526.491L 160.679,526.491C 161.723,526.491 162.568,525.645 162.568,524.601L 162.568,505.703C 162.568,504.659 161.723,503.813 160.679,503.813L 141.781,503.813 Z ">
                                    <GeometryDrawing.Brush>
                                        <LinearGradientBrush StartPoint="0.499992,-1.61488e-005" EndPoint="0.499992,1">
                                            <LinearGradientBrush.GradientStops>
                                                <GradientStop Color="#FFD1D3D4" Offset="0"/>
                                                <GradientStop Color="#FFFFFFFF" Offset="1"/>
                                            </LinearGradientBrush.GradientStops>
                                        </LinearGradientBrush>
                                    </GeometryDrawing.Brush>
                                </GeometryDrawing>
                                <DrawingGroup ClipGeometry="F1 M 0,0L 793.701,0L 793.701,1122.52L 0,1122.52L 0,0 Z ">
                                    <DrawingGroup.Children>
                                        <GeometryDrawing Geometry="F1 M 139.891,505.703C 139.891,504.659 140.737,503.814 141.781,503.814L 160.679,503.814C 161.723,503.814 162.568,504.659 162.568,505.703L 162.568,524.6C 162.568,525.644 161.723,526.491 160.679,526.491L 141.781,526.491C 140.737,526.491 139.891,525.644 139.891,524.6L 139.891,505.703 Z ">
                                            <GeometryDrawing.Pen>
                                                <Pen Thickness="1.33333" LineJoin="Round" Brush="#FFA7A9AC"/>
                                            </GeometryDrawing.Pen>
                                        </GeometryDrawing>
                                    </DrawingGroup.Children>
                                </DrawingGroup>
                                <GeometryDrawing Geometry="F1 M 144.048,506.081C 143.005,506.081 142.159,506.927 142.159,507.969L 142.159,522.333C 142.159,523.377 143.005,524.223 144.048,524.223L 158.412,524.223C 159.455,524.223 160.301,523.377 160.301,522.333L 160.301,507.969C 160.301,506.927 159.455,506.081 158.412,506.081L 144.048,506.081 Z ">
                                    <GeometryDrawing.Brush>
                                        <LinearGradientBrush StartPoint="0.499951,1.00003" EndPoint="0.499951,-3.36442e-006">
                                            <LinearGradientBrush.GradientStops>
                                                <GradientStop Color="#FFE6E8E8" Offset="0"/>
                                                <GradientStop Color="#FFFFFFFF" Offset="1"/>
                                            </LinearGradientBrush.GradientStops>
                                        </LinearGradientBrush>
                                    </GeometryDrawing.Brush>
                                </GeometryDrawing>
                                <GeometryDrawing Geometry="F1 M 144.645,515.816L 147.521,515.816L 147.521,521.56L 151.24,521.56L 154.939,521.56L 154.939,515.816L 157.813,515.816L 151.219,508.743L 144.645,515.816 Z ">
                                    <GeometryDrawing.Brush>
                                        <LinearGradientBrush StartPoint="0.499986,1.00003" EndPoint="0.499986,5.23812e-005">
                                            <LinearGradientBrush.GradientStops>
                                                <GradientStop Color="#FF50C400" Offset="0"/>
                                                <GradientStop Color="#FF91F456" Offset="1"/>
                                            </LinearGradientBrush.GradientStops>
                                        </LinearGradientBrush>
                                    </GeometryDrawing.Brush>
                                </GeometryDrawing>
                                <DrawingGroup ClipGeometry="F1 M 0,0L 793.701,0L 793.701,1122.52L 0,1122.52L 0,0 Z ">
                                    <DrawingGroup.Children>
                                        <GeometryDrawing Geometry="F1 M 151.24,521.56L 147.521,521.56L 147.521,515.815L 144.646,515.815L 151.218,508.743L 157.814,515.815L 154.938,515.815L 154.938,521.56L 151.24,521.56 Z ">
                                            <GeometryDrawing.Pen>
                                                <Pen Thickness="1.33333" LineJoin="Round" Brush="#FF076F00"/>
                                            </GeometryDrawing.Pen>
                                        </GeometryDrawing>
                                    </DrawingGroup.Children>
                                </DrawingGroup>
                            </DrawingGroup.Children>
                        </DrawingGroup>
                    </DrawingBrush.Drawing>
                </DrawingBrush>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

然后在您的 App.Xaml 中包含以下资源:

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

如果不设置 &lt;Style&gt; 资源上的 x:Key 将使用指定的填充修改应用程序中的所有矩形... 如果您想为矩形添加更多样式,则需要将 x:Key="RectStyle1" 属性添加到 &lt;Style&gt; 并指定矩形需要使用的样式,如下所示:

<Rectangle Style="{StaticResource RectStyle1}" />

玩得开心:)

【讨论】:

  • 是的,工作,我想我尝试了所有这些但我不能:P非常感谢
猜你喜欢
  • 2020-08-16
  • 2015-01-01
  • 2013-04-15
  • 1970-01-01
  • 2016-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多