c# - mysterious runtime error in applying a template to wpf window -
i have hard problem dealing template. please 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 didn't set 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>
=====================
question #1
when run program, ide occure runtime error : xmlparseexception. add line in app.xaml, runs properly. line : startupuri="cmainwindow.xaml".
what this? relationship between template , startupuri? please tell me this.
question #2
when add control cmainwindow, didn't apeear set in window's template.
how can add control in situation?
thanks.
question #1 wpf application centered around window. you're override of onstartup unnecessary. setting startupuri application automatically start displaying window.
there no actual relationship between template , startupuri. happen using app.xaml store global styles.
question #2
the magic field add "targettype" on control template. have explicitly 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 window --> <controltemplate targettype="window"> <grid> <rectangle fill="gray" radiusx="30" radiusy="30"/> <contentpresenter/> </grid> </controltemplate> </setter.value> </setter> </style> </application.resources> </application>
Comments
Post a Comment