2011-07-21 9 views
2

私はテンプレートを扱うのが難しいです。私を助けてください。wpfウィンドウにテンプレートを適用する際の不思議なランタイムエラー

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" Width="525" Style="{StaticResource myWindowStyle}" Background="Red"> 
</Window> 

======== ========このプログラムを実行するとき=====

質問#1

は、IDEはランタイムエラーをoccure:XmlParseException。 私はapp.xamlに行を追加するので、正しく実行されます。その行は:StartupUri = "CMainWindow.xaml"です。

これはなんですか?テンプレートとstartupuriの間の関係は?これについて教えてください。私はCMainWindowにコントロールを追加するとき

質問#2

は、それは私は、ウィンドウのテンプレートに設定してもapeearませんでした。

どうすればこの状況に適切にコントロールを追加できますか?

ありがとうございました。

答えて

2

質問#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> 
+0

ご回答いただきありがとうございます。私はグローバルスタイルのためのリソース辞書に別々のウィンドウスタイルを行い、それが正しく動作します。 – mjk6026