两个XAML文件可以有一个.cs
文件吗?
答案 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
文件(查看模型)
你能解释一下为什么需要这个吗?可能是你正在采取错误的方法