使用 LoadComponent 加载外部 XAML 文件

ale*_*2k8 2 wpf

我发现一些证据表明这是可能的

self.scene = Canvas()
Application.LoadComponent(self.scene, Uri('app.xaml', UriKind.Relative))
Run Code Online (Sandbox Code Playgroud)

但我的代码失败了:

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        Canvas scene = new Canvas();
        Application.LoadComponent(scene, new Uri("app.xaml", UriKind.Relative));
    }
}
Run Code Online (Sandbox Code Playgroud)

我使用相同的 app.xaml 和“构建操作:无”和“始终复制”。

<Canvas
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="System.Windows.Controls.Canvas"
    x:Name="ball_design" >

    <Canvas x:Name="workaround_canvas" >

        <TextBlock x:Name="fps" Canvas.Left="10" Canvas.Top="10" Height="40" Canvas.ZIndex="10000" Text="-- fps" />
    <Canvas x:Name="wpfe_ball_0" Width="52" Height="52" Canvas.Left="0" Canvas.Top="30">
      <!-- Layer 3/<Group>/<Path> -->
      <Path Opacity="0.900000" StrokeThickness="2.000000" Stroke="#ffa6d000" StrokeMiterLimit="1.000000" Fill="#ffcbff00" Data="F1 M 51.000000,26.000000 C 51.000000,39.806641 39.807129,51.000000 26.000000,51.000000 C 12.192871,51.000000 1.000000,39.806641 1.000000,26.000000 C 1.000000,12.193359 12.192871,1.000000 26.000000,1.000000 C 39.807129,1.000000 51.000000,12.193359 51.000000,26.000000 Z"/>

      <!-- Layer 3/<Group>/<Path> -->
      <Path Opacity="0.740000" Data="F1 M 43.143066,13.087891 C 50.602051,22.888672 49.009766,36.642578 39.590332,43.812500 C 30.170898,50.980469 16.489258,48.842773 9.032715,39.042969 C 1.573242,29.240234 3.166016,15.486328 12.584961,8.316406 C 22.003906,1.149414 35.685547,3.285156 43.143066,13.087891 Z">
        <Path.Fill>
          <RadialGradientBrush MappingMode="Absolute" GradientOrigin="156.791016,170.453125" Center="156.791016,170.453125" RadiusX="53.626404" RadiusY="53.626404">
            <RadialGradientBrush.GradientStops>
              <GradientStop Offset="0.000000" Color="#ffffffff"/>
              <GradientStop Offset="0.361685" Color="#fff5f7dd"/>
              <GradientStop Offset="0.415730" Color="#ffebf0bc"/>
              <GradientStop Offset="1.000000" Color="#ffcbff00"/>
            </RadialGradientBrush.GradientStops>
            <RadialGradientBrush.Transform>
              <MatrixTransform Matrix="1.190000,0.165000,-0.165000,-1.281300,-113.414185,241.757843" />
            </RadialGradientBrush.Transform>
          </RadialGradientBrush>
        </Path.Fill>
      </Path>

      <!-- Layer 3/<Group>/<Path> -->
      <Path Fill="#ffffffff" Data="F1 M 23.100586,9.477539 C 24.741699,11.634766 23.116211,15.630859 19.470703,18.404297 C 15.825684,21.178711 11.540039,21.678711 9.899414,19.522461 C 8.258301,17.365234 9.883789,13.369141 13.529297,10.594727 C 17.174316,7.821289 21.459961,7.321289 23.100586,9.477539 Z"/>
    </Canvas>
    <TextBlock x:Name="dbgwin" FontSize="10" Canvas.Top="10" Canvas.Left="250" Height="500" Width="200" Text="IronPython DLR" />

  </Canvas>
</Canvas>
Run Code Online (Sandbox Code Playgroud)

我收到 IOException: 无法找到资源“app.xaml”。

什么是解决方案?

Jim*_*lli 5

我让这个工作。我的具体目标是将 ResourceDictionary 加载到 App 类中,这样我就可以从原始 XAML 文件加载自定义皮肤。就是这样。首先,在 App.xaml 中添加以下内容:

    protected override void OnStartup(StartupEventArgs e) {
        base.OnStartup(e);
        LoadSkin("Skins/Skin1.xaml");

    }

    private void LoadSkin(string FilePath) {
        XmlReader XmlRead = XmlReader.Create(FilePath);
        Application.Current.Resources = 
            (ResourceDictionary)XamlReader.Load(XmlRead);
        XmlRead.Close();
    }
Run Code Online (Sandbox Code Playgroud)

创建一个“bin\Debug\Skins”文件夹并添加一个“Skin1.xaml”,内容如下:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
    <Style x:Key="ButtonAStyle" TargetType="{x:Type Button}">
        <Setter Property="Control.Background" Value="Green" />
        <Setter Property="Control.Foreground" Value="White" />
    </Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

特定于皮肤

在这种特殊情况下,您必须确保在各个窗口中正确使用样式资源。这是一个例子:

<Button 
    Name="button1" 
    Style="{DynamicResource ButtonAStyle}" 
    Click="button1_Click"
>Button</Button>
Run Code Online (Sandbox Code Playgroud)

这是一个值得尝试的巧妙技巧。在button1_Click()中,尝试添加与上面App.OnStartUp()中相同的代码,但使用“Skin2.xaml”和“Red”作为按钮的背景颜色。皮肤会立即改变,无需重新加载应用程序或窗口。