在XAML中使用几何资源

时间:2017-07-17 09:16:23

标签: xaml uwp uwp-xaml

我有一个名为Header的usercontrol,具有依赖属性IconData:

public Geometry IconData
{
    get { return (Geometry)GetValue(IconDataProperty); }
    set { SetValue(IconDataProperty, value); }
}

public static readonly DependencyProperty IconDataProperty = 
    DependencyProperty.Register(nameof(IconData), typeof(Geometry), typeof(Header), new PropertyMetadata(null);

我在Application

中定义了图标
<x:String x:Key="HomeIconGeometry">F1 M 24.0033,56.0078L 24.0033,38.0053L 22.0031,40.0056L 19.0027,35.0049L 38.0053,20.0028L 45.0063,25.5299L 45.0063,21.753L 49.0068,21.0029L 49.0068,28.6882L 57.008,35.0049L 54.0075,40.0056L 52.0073,38.0053L 52.0073,56.0078L 24.0033,56.0078 Z M 38.0053,26.9204L 27.0038,36.005L 27.0038,53.0074L 33.0046,53.0074L 33.0046,42.006L 43.006,42.006L 43.006,53.0074L 49.0068,53.0074L 49.0068,36.005L 38.0053,26.9204 Z</x:String>

我这样使用它:

<local:Header x:Name="HeaderPanel" IconData="{StaticResource HomeIconGeometry}" />

但是,XAML设计师一如既往地糟透了:

enter image description here

这个问题与PathGeometry in ResourceDictionary相似 与之不同的是,建议的答案在自定义控件/用户控件中不起作用

2 个答案:

答案 0 :(得分:2)

你想要做的是完全在WPF中工作,但遗憾的是,UWP不支持...

我找到的唯一方法是在页面/应用资源中声明您的路径,因此能够使用移动和绘制语法

<Page.Resources>
    <Path x:Key="pp" Data="F1 M 24.0033,56.0078L 24.0033,38.0053L 22.0031,40.0056L 19.0027,35.0049L 38.0053,20.0028L 45.0063,25.5299L 45.0063,21.753L 49.0068,21.0029L 49.0068,28.6882L 57.008,35.0049L 54.0075,40.0056L 52.0073,38.0053L 52.0073,56.0078L 24.0033,56.0078 Z M 38.0053,26.9204L 27.0038,36.005L 27.0038,53.0074L 33.0046,53.0074L 33.0046,42.006L 43.006,42.006L 43.006,53.0074L 49.0068,53.0074L 49.0068,36.005L 38.0053,26.9204 Z" />
    <local:PathToFiguresConverter x:Key="converter" />
</Page.Resources>

然后使用转换器提取创建的PathGeometry数据并将它们提供给路径对象:

class PathToFiguresConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        var path = value as Path;
        var figures = (path.Data as PathGeometry).Figures;
        return figures;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

在页面/控件中:

<Path Fill="{ThemeResource SystemControlForegroundAccentBrush}">
    <Path.Data>
        <PathGeometry Figures="{Binding Source={StaticResource pp}, Converter={StaticResource converter} }" />
    </Path.Data>
</Path>

这也意味着在您的情况下,您必须将您的依赖项属性更改为:

public PathFigureCollection IconData
{
    get { return (PathFigureCollection )GetValue(IconDataProperty); }
    set { SetValue(IconDataProperty, value); }
}

public static readonly DependencyProperty IconDataProperty = 
    DependencyProperty.Register(nameof(IconData), typeof(PathFigureCollection ), typeof(Header), [...])

答案 1 :(得分:0)

修复它的一种简单方法是使用bind。

您可以使用bind并设置源并转换为将字符串转换为Geometry。

代码如下。

public class GeometryConvert : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (value is string str)
        {
            var geometry = (Geometry) XamlReader.Load(
                "<Geometry xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>"
                + str + "</Geometry>");
            return geometry;
        }
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

您应该将代码更改为

<local:GeometryConvert x:Key="GeometryConvert"></local:GeometryConvert>

<local:Header x:Name="HeaderPanel" IconData="{Binding Source={StaticResource HomeIconGeometry},Converter={StaticResource GeometryConvert}}" />

正如我所看到的,我将代码作为图像运行。

enter image description here

但我找不到将字符串更改为Geometry的好方法。

请参阅:https://stackoverflow.com/a/34900291/6116637