开发者

mysterious runtime error in applying a template to wpf window

i have a hard problem dealing with template. please help me.

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>

App.xaml.cs

using System;
using System.Windows;

namespace WpfApplication1
{
    public partial class App : Application
    {

        CMainWindow winMain;

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            winMain = new CMainWindow();
            winMain.ShowDialog();

        }

    }
}

CMainWindow.xaml

<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" W开发者_Go百科idth="525" Style="{StaticResource myWindowStyle}" Background="Red">
</Window>

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

question #1

when run this program, ide occure a runtime error : XmlParseException. so i add a line in app.xaml, it runs properly. that line is : StartupUri="CMainWindow.xaml".

what is this? what relationship between template and startupuri? please tell me about this.

question #2

when i add control to CMainWindow, it didn't apeear even i set a in window's template.

how can i add control properly in this situation?

thanks.


question #1 A WPF application is always centered around a window. You're override of OnStartup is unnecessary. By setting the StartupURI the application will automatically start by displaying the window.

There is no actual relationship between template and startupuri. You just happen to be using App.xaml to store global styles.

question #2

The magic field to add is "TargetType" on the control template. You have to explicitly say its for the window type.

<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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜