我定义了自定义画笔,如 How to define and use resources in xaml so they can be used in C# 中所述.
但是,当我尝试将其分配给 Shape.Fill 或 Shape.Stroke 属性时,如下所示:
<Rectangle Stroke="MyBrush"/>
我的应用程序因 XamlParseException 而崩溃。那么如何仅使用 XAML 代码分配自定义画笔呢?
请您参考如下方法:
一旦进入 XAML,您就无法简单地指定资源名称
<Rectangle Stroke="{Binding Source={StaticResource OfferByBrand}}"/>
如果您通过代码进行绑定(bind),并且它本身就是预定义的,或者
<Rectangle Stroke="{DynamicResource OfferByBrand}"/>
如果在 res. 否则如回答的那样,您需要使用 C# 访问
Application.Current.Resources["BlaBrush"] as LinearGradientBrush
请查看 Resources
