【问题标题】:app.xaml resources won't work. dot.net core stuff?app.xaml 资源不起作用。 dot.net 核心的东西?
【发布时间】:2020-10-09 18:15:34
【问题描述】:

我在 app.xaml 文件中定义了以下资源,效果很好:

<Application.Resources>
    <Style TargetType="Button" x:Key="homeButtom">
        <Setter Property="Margin" Value="5" />
        <Setter Property="Padding" Value="4" />
        <Setter Property="Width" Value="110" />
        <Setter Property="Background" Value="CadetBlue" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="FontSize" Value="14" />
    </Style>
</Application.Resources>

然后,我将实体框架添加到解决方案中,并且按照建议,O 从标头中删除了“StartupUri="MainWindow.xaml”,并将调用插入到后面的代码中:

protected override async void OnStartup(StartupEventArgs e)
    {
        await host.StartAsync();

        var mainWindow = host.Services.GetRequiredService<MainWindow>();
        mainWindow.Show();

        base.OnStartup(e);
    }

除非在需要的页面上设置资源,否则资源将无法使用。

只是补充一下,我想这就是原因。事实是我的资源只能在 app.xaml 中使用...

【问题讨论】:

    标签: c# wpf xaml code-behind app.xaml


    【解决方案1】:

    我可以确认您呈现资源的方式在 dot net core 中不起作用。但是,我发现如果将资源放在 ResourceDictionary 中,它会起作用。

    App.xaml.cs:

            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="pack://application:,,,/MyResourceDictionary.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
    

    MyResourceDictionary.xaml:

        <Style TargetType="Button" x:Key="homeButtom">
            <Setter Property="Margin" Value="5" />
            <Setter Property="Padding" Value="4" />
            <Setter Property="Width" Value="110" />
            <Setter Property="Background" Value="CadetBlue" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="FontSize" Value="14" />
        </Style>
    

    【讨论】:

      猜你喜欢
      • 2022-06-11
      • 1970-01-01
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      • 2017-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多