簡體   English   中英

如何在 C# 中使用內聯 XAML 在代碼隱藏中輕松創建自定義對象

[英]How to use inline XAML in C# to easily make custom objects in code-behind

我正在嘗試使用 XAML 解析器引用 LinearGradientBrush,但它無法識別對象,並且出現異常:

“無法創建未知類型的‘LinearGradientBrush’”

是否可以在運行時識別這種類型?

這是我正在使用的代碼:

public static class CustomBrushes
{
    public static Brush LinGrad_Bevel()
    {
        
        
        StringReader sr = new StringReader(@"<LinearGradientBrush EndPoint='0.5,1' MappingMode='RelativeToBoundingBox' StartPoint='0.5,0'>
                <GradientStop Color='#00F7F7F7' Offset='0'/>
                <GradientStop Offset='1'/>
                <GradientStop Color='Black' Offset='0.741'/>
                <GradientStop Color='Black' Offset='0.75'/>
                <GradientStop Color='White' Offset='0.25'/>
            </LinearGradientBrush>");
        XmlReader xr = XmlReader.Create(sr);


        return (Brush)XamlReader.Load(xr);
    }
}

我真的不喜歡在 XAML 中編程(特別是因為我所做的大部分工作都是依賴於運行時程序流的設計,但有些對象比 C# 更容易在其中進行原型設計,我更願意能夠使用這種方法...

我讀過我應該在某處包含這樣的行,但老實說我不明白為什么,如果我把它放在所有“使用”下似乎不起作用

[assembly: XmlnsDefinition("http://schemas.microsoft.com/netfx/2007/xaml/presentation" , "System.Windows.Media")]

無論如何,對於 C# 代碼文件中 XAML 的運行時解析的任何幫助將不勝感激。

要回答您的特定問題,需要在編譯 XAML 時提供命名空間聲明。 最簡單的方法是將它們插入到您的字符串中。 例如:

StringReader sr = new StringReader(@"
    <LinearGradientBrush
        xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
        xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
        EndPoint='0.5,1' MappingMode='RelativeToBoundingBox' StartPoint='0.5,0'>
    <GradientStop Color='#00F7F7F7' Offset='0'/>
    <GradientStop Offset='1'/>
    <GradientStop Color='Black' Offset='0.741'/>
    <GradientStop Color='Black' Offset='0.75'/>
    <GradientStop Color='White' Offset='0.25'/>
</LinearGradientBrush>");
XmlReader xr = XmlReader.Create(sr);

return (Brush)XamlReader.Load(xr);

現在,話雖如此,我想說以上在我看來是最理想的方式來解決這個問題。 如果您想要一個可重用的Brush對象,並且想要使用 XAML 來聲明它而不是通過實際的 WPF 編程 API,我認為更好的方法是將聲明放在程序中的相關Resources集合之一中。

例如,如果您希望能夠在整個程序中使用它,在多個不同的窗口中,您可以通過將其放入App.xaml文件來使其成為全局資源:

<Application x:Class="TestSO28999367XamlReader.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
  <Application.Resources>
    <LinearGradientBrush x:Key="beveledLinearGradientBrush"
                    EndPoint='0.5,1' MappingMode='RelativeToBoundingBox' StartPoint='0.5,0'>
      <GradientStop Color='#00F7F7F7' Offset='0'/>
      <GradientStop Offset='1'/>
      <GradientStop Color='Black' Offset='0.741'/>
      <GradientStop Color='Black' Offset='0.75'/>
      <GradientStop Color='White' Offset='0.25'/>
    </LinearGradientBrush>
  </Application.Resources>
</Application>

如果您的使用僅限於一個Window對象或什至它的某個子元素,您可以在該對象的Resources集合中添加聲明,而不是將其放在App.xaml

無論如何,您可以使用以下表達式訪問它:

(Brush)FindResource("beveledLinearGradientBrush")

當然,您可以為資源制作任何您想要的鍵……它不必是beveledLinearGradientBrush

您仍然可以使用它來初始化一些代碼隱藏資源,通過使用上面的 C# 表達式來檢索對象。 但是,請注意,當您按照上述方式進行操作時,畫筆也可通過表達式"{StaticResource beveledLinearGradientBrush}"直接在任何 XAML 中使用。

例如:

<Border Background="{StaticResource beveledLinearGradientBrush}"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM