【问题标题】:How can I split MainWindow.xaml into separate files? - Outsourcing of <Style>?如何将 MainWindow.xaml 拆分为单独的文件? - <风格>的外包?
【发布时间】:2012-07-16 02:27:00
【问题描述】:

您好,我想知道如何将 MainWindow.xaml 分成不同的 xaml 文件?我想外包我的样式,并在需要时将它们包括在内。我搜索了一些解决方案,发现了一些 stackoverflow 帖子,但没有一个对我有帮助。

我想实现这样的事情(伪代码)

<Window x:Class="MyApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyApp"
    Title="MyApp" Height="350" Width="525">
<Window.Resources>    
    //Import external xaml file with textbox style here 
    //instead of:
<Style TargetType="{x:Type TextBox}">
    <Style.Triggers>
        //add code here
    </Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
    <TextBox Width="60"/>
    <Button Content="Button" Height="23" Name="button1" Width="75" />
</StackPanel>

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    创建 XAML ResourceDictionary 文件,像这样包含它们:

    <Window.Resources>
        <ResourceDictionary>
             <ResourceDictionary.MergedDictionaries>
                  <ResourceDictionary Source="./Styles/TextBlockStyle.xaml" />
             </ResourceDictionary.MergedDictionaries>
             <!-- other resources here -->
        </ResourceDictionary>
    </Window.Resources>
    

    【讨论】:

    • 嗯,我以前试过这个,但没有用。也许我应该再给它一次机会……
    • 好的,现在可以了。之前试的时候不知道到底出了什么问题。谢谢! :)
    猜你喜欢
    • 2014-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 2020-01-12
    相关资源
    最近更新 更多