Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将模板应用于wpf窗口时出现神秘的运行时错误_C#_Wpf_Wpf Controls - Fatal编程技术网

C# 将模板应用于wpf窗口时出现神秘的运行时错误

C# 将模板应用于wpf窗口时出现神秘的运行时错误,c#,wpf,wpf-controls,C#,Wpf,Wpf Controls,我在处理模板时遇到了一个难题。请帮帮我 App.xaml <Application x:Class="WpfApplication1.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> //Note i didn't set a S

我在处理模板时遇到了一个难题。请帮帮我

App.xaml

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

//Note i didn't set a StartupURI in Application tag please.

    <Application.Resources>

        <Style TargetType="Window" x:Key="myWindowStyle">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Grid>
                            <Rectangle Fill="gray" RadiusX="30" RadiusY="30"/>
                            <ContentPresenter/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </Application.Resources>
</Application>
<Window x:Class="WpfApplication2.CMainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Style="{StaticResource myWindowStyle}" Background="Red">
</Window>
CMainWindow.xaml

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

//Note i didn't set a StartupURI in Application tag please.

    <Application.Resources>

        <Style TargetType="Window" x:Key="myWindowStyle">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Grid>
                            <Rectangle Fill="gray" RadiusX="30" RadiusY="30"/>
                            <ContentPresenter/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </Application.Resources>
</Application>
<Window x:Class="WpfApplication2.CMainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Style="{StaticResource myWindowStyle}" Background="Red">
</Window>

=====================

问题1

运行此程序时,ide发生运行时错误:XmlParseException。 所以我在app.xaml中添加了一行,它运行正常。这行代码是:StartupUri=“CMainWindow.xaml”

这是什么?模板和startupuri之间有什么关系?请告诉我这件事

问题2

当我向CMainWindow添加控件时,它甚至没有在窗口的模板中设置一个

在这种情况下,如何正确添加控件

谢谢。

问题1 WPF应用程序始终以窗口为中心。您不需要覆盖OnStartup。通过设置StartupURI,应用程序将通过显示窗口自动启动

模板和startupuri之间没有实际关系。您正好使用App.xaml来存储全局样式

问题2

要添加的魔术字段是控件模板上的“TargetType”。对于窗口类型,您必须明确地说出它的名称

<Application x:Class="SimpleWPF.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <Style TargetType="Window" x:Key="myWindowStyle">
            <Setter Property="Template">
                <Setter.Value>
                    <!-- Explicitly setting TargetType to Window -->
                    <ControlTemplate TargetType="Window">
                        <Grid>

                            <Rectangle Fill="gray" RadiusX="30" RadiusY="30"/>  
                            <ContentPresenter/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Application.Resources>
</Application>

感谢anderew的回答。我将窗口样式和全局样式的资源字典分开,然后它就可以正常工作了。