win10 uwp 使用 Geometry resources 在 xaml

來源:互聯網
上載者:User

標籤:顯示圖片   ted   sso   方式   svg   log   https   ddn   depend   

原文:win10 uwp 使用 Geometry resources 在 xaml

經常會遇到在 xaml 使用向量圖,對於 svg 的向量圖,一般都可以拿出來寫在 Path 的 Data ,所以可以寫為資源,但是寫出來的是字串,如何綁定 Geometry 到字串資源?

假如在資源寫一個圖片,看起來就是下面的代碼

<Page.Resources>    <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></Page.Resources>

然後發現使用的是 string ,如果這時建立了一個使用者控制項,裡面寫了一個屬性,請看代碼

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);

介面直接使用代碼

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

就會在運行出現無法從string轉換,但是如何把使用者控制項改為 Path ,就可以運行

那麼如何在使用者控制項使用資源的字串

可以使用綁定,如果無法轉換,可以寫一個轉換

先建立一個轉換類

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}}" />

可以看到,這個方法可以顯示圖片

所以,需要綁定字串,可以使用這個方法。

有人說,綁定到字串可以不使用轉換,他可以做到,直接使用綁定,但是我暫時沒法

https://stackoverflow.com/a/45142555/6116637


本作品採用知識共用署名-非商業性使用-相同方式共用 4.0 國際許可協議進行許可。歡迎轉載、使用、重新發布,但務必保留文章署名林德熙(包含連結:http://blog.csdn.net/lindexi_gd ),不得用於商業目的,基於本文修改後的作品務必以相同的許可發布。如有任何疑問,請與我聯絡。

win10 uwp 使用 Geometry resources 在 xaml

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.