How to create a common WPF base window style?

前端 未结 5 1323
一个人的身影
一个人的身影 2020-12-06 04:01

Is there any recommended way with WPF to create a common window style to be used across an application? I have several dialogs that appear in my app, and I would like them a

5条回答
  •  臣服心动
    2020-12-06 04:08

    Create a Xaml Form template and add the template to the VS Installed ItemTemplates directory.

    1) create a wpf xaml and xaml.cs file that has all the desired components wanted for a new form added to your application. In my case I wanted the title and toolbar buttons.

    2) test the new xaml files through the current system flow.

    3) copy xaml / xaml.cs to temp location and rename both the filenames to something you want to be recognized as a good template name. a) Change first line within xaml file to -- Window x:Class="$rootnamespace$.$safeitemname$"

    b) Make 3 changes within xaml.cs file to ensure the new name will be copied when using the template - -- namespace $rootnamespace$ (//dynamic namespace name) -- public partial class $safeitemname$ (//dynamic class name) -- public $safeitemname$() (//dynamic constructor name)

    4) Now create a vstemplate file: ie. MyTemplate.vstemplate with the following content:

    
      
        WpfFormTemplate.xaml
        WpfFormTemplate
        Wpf/Entities form
        CSharp
        10
        Logo.ico
      
      
        
            
                System.Windows.Forms
            
            
                Workplace.Data.EntitiesModel
            
            
                Workplace.Forms.MainFormAssemb
            
        
        WpfFormTemplate.xaml
        WpfFormTemplate.xaml.cs
      
    
    

    5) Once you have all these files, zip the files and place the zip file under the ....\Documents\Visual Studio 2012\Templates\ItemTemplates\WPF directory. Now you can go into VS2012 and use the ADD\New feature to see the template, select and rename as in the normal process. The template can be used in the same way for VS2010 by placing the zip file under the 2010 Templates Wpf directory.

    The Logo file should be included in the zip file as well or if you don't have a file then remove that line from the MyTemplate.vstemplate file.

提交回复
热议问题