简体   繁体   English

如何在WPF中的一个XAML文件中添加多个类文件

[英]How to add multiple class file in one XAML file in WPF

I am designing a UI Library and I want to add two C# file to a XAML file so that in one file I can define only event Handler to be used in that Xaml file and in another file i can define some other UI related Style for controls. 我正在设计一个UI库 ,我想向XAML文件中添加两个 C# file ,以便在一个文件中我只能定义要在该Xaml文件中使用的事件处理程序,而在另一个文件中,我可以为控件定义其他一些与UI相关的样式。 And i want my xaml file to have access to my both C# file class directly. 我希望我的xaml文件可以直接访问我的两个C#文件类。

I am doing something like this using x:Class : 我正在使用x:Class做这样的事情:

For example: 例如:

I want to add a MainWindow.cs file to my XAML file: 我想将MainWindow.cs文件添加到我的XAML文件中:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="TemplateLibrary.MainWindow">

But unfortunately XAML supports for only one class to get added to my XAML. 但是不幸的是,XAML仅支持将一个类添加到我的XAML中。 Is there some way to add multiple C# file to a XAML file. 有某种方法可以将多个C#文件添加到XAML文件。

I think in your case you need to look in the direction of MVVM , because the further complication of the project will cause some difficulties. 我认为在您的情况下,您需要朝MVVM方向发展,因为该项目的进一步复杂化将带来一些困难。 You can create multiple ViewModel's for a one View . 您可以为一个View创建多个ViewModel's In the role of View may suit: View可能适合的角色:

  • UserControl 用户控件

  • DataTemplate DataTemplate中

In the case of UserControl this class can be partial, which will complement the other class. UserControl的情况下,该类可以是部分的,这将补充另一个类。 In the case of DataTemplate can dynamically change the content depending on View type. 如果是DataTemplate可以根据View类型动态更改内容。

In addition, all styles of Control's must be contained in ResourceDictionary and merged into the App.xaml file. 此外,所有控件的样式都必须包含在ResourceDictionary并合并到App.xaml文件中。

It seems that the MainWindow code behind is already a partial class file, and therefore we could choose to place some members into additional cs files using the same partial class in the same namespace. 似乎后面的MainWindow代码已经是局部类文件,因此,我们可以选择在同一名称空间中使用相同的局部类将一些成员放入其他CS文件中。

namespace TemplateLibrary
{
    public partial class MainWindow : Window
    {
        ...

The same applies when making a UserControl. 制作UserControl时也是如此。

More basic is "How to bind an XAML visual object to code". 更基本的是“如何将XAML可视对象绑定到代码”。 Ranny Meier's suggestion on declaring my class on it's page as a 'partial class' coupled with (can't find him again) suggestion of using a redirection on the mainwindow page actually works fine - all in the same name space of course. 兰尼·迈耶(Ranny Meier)关于在页面上将我的班级声明为“部分班级”的建议,再加上(找不到他),在主窗口页面上使用重定向的建议实际上是可行的-所有这些当然都在相同的名称空间中进行。

xaml XAML

    <Button x:Name="Pnl_Btn_EditPaste" Click="Do_Btn_EditPaste_Click" VerticalAlignment="Top" Padding="2" Margin="-67,18,0,0" ScrollViewer.VerticalScrollBarVisibility="Disabled" Width="36" Height="30" RenderTransformOrigin="1.976,-0.568">
         <Button.OpacityMask>
             <ImageBrush ImageSource="pack://siteoforigin:,,,/Resources/BnFace_paste.png"/>
         </Button.OpacityMask>
         <Button.Background>
             <ImageBrush ImageSource="pack://siteoforigin:,,,/Resources/BnFace_paste.png"/>
         </Button.Background>
    </Button>

It's the "Click=" portion, or binding, on the Button opening statement we're interested in . 这是我们感兴趣的Button打开语句上的“ Click =“部分或绑定。 . .

mainwindow c# 主窗口C#

   namespace WpfAppFoolAround
   {
     public partial class MainWindow : Window
     {
        public MainWindow()
        {
            InitializeComponent();
        }

        // -- Edit Controls ---------------------------------------------------------

        private void Do_Btn_EditPaste_Click(object sender, RoutedEventArgs e)
        {
            ContextControls.Btn_EditPaste(sender, e);
        }
     }
   }

The shim on mainwindow to redirect us to the class method . 主窗口上的垫片将我们重定向到class方法。 . .

the class on a different page 在不同页面上的课程

    namespace WpfAppFoolAround
    {
        public partial class ContextControls
        {

           // -- Edit Controls ---------------------------------------------------

            internal static void Btn_EditPaste(object sender, RoutedEventArgs e)
            {
               Console.WriteLine("PASTE Editing Button Clicked.");
            }
        }
    }

And the actual method located in the class on the odd project page we added. 实际的方法位于我们添加的奇数项目页面上的类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM