两个XAML文件可以在WPF中有一个cs文件吗?

时间:2012-01-10 10:03:55

标签: c# .net wpf xaml

两个XAML文件可以有一个.cs文件吗?

2 个答案:

答案 0 :(得分:5)

是的,因为支持源文件只包含一个部分类,我们可以将它移动到我们想要的任何地方。然而,这是期望的做法。

的UserControl1:

<UserControl x:Class="WpfApplication1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <TextBlock Text="UserControl1"/>
</UserControl>

UserControl2:

<UserControl x:Class="WpfApplication1.UserControl2"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <TextBlock Text="UserControl2"/>
</UserControl>

来源:

namespace WpfApplication1
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }
    }

    public partial class UserControl2 : UserControl
    {
        public UserControl2()
        {
            InitializeComponent();
        }
    }
}

答案 1 :(得分:2)

不,那是不可能的。

虽然使用MVVM模式可以轻松实现这一点。您可以为多个cs文件(视图)保存相同的XAML文件(查看模型)

你能解释一下为什么需要这个吗?可能是你正在采取错误的方法