2个XAML页面,代码隐藏几乎相同



我有两个页面,后面有几乎相同的代码(.xaml.cs文件)。不过它们的布局不同。Xaml文件则不同)。在代码隐藏文件中,唯一的区别是变量的类型。所有其他过程/函数完全相同。

例如:

Page1:

public sealed partial class Page1 : Page
{
public List<CarVersion1> cars = new List<CarVersion1>();
public CarVersion1 currentCar;
...
private UpdatePrice(int p) {
currentCar.Price = p;
}
}

所以Page2:

public sealed partial class Page2 : Page
{
public List<CarVersion2> cars = new List<CarVersion2>();
public CarVersion2 currentCar;
...
private UpdatePrice(int p) {
currentCar.Price = p;
}
}

无论如何使用一个代码隐藏文件,而不是复制它?

我猜你需要创建一个ViewModel并把你所有的公共逻辑。像这样:

CarVersions.cs

namespace Pages;
public class CarVersion
{
public string Version { get; set; } = string.Empty;
}
public class CarVersion1 : CarVersion
{
public CarVersion1()
{
Version = nameof(CarVersion1);
}
}
public class CarVersion2 : CarVersion
{
public CarVersion2()
{
Version = nameof(CarVersion2);
}
}

ViewModel.cs

using CommunityToolkit.Mvvm.ComponentModel;
[ObservableObject]
public partial class ViewModel<T> where T : CarVersion, new()
{
[ObservableProperty]
private T carVersion = new();
}

Page1.xaml.cs

using Microsoft.UI.Xaml.Controls;
namespace Pages;
public sealed partial class Page1 : Page
{
public Page1()
{
this.InitializeComponent();
}
public ViewModel<CarVersion1> ViewModel { get; } = new();
}

Page2.xaml.cs

using Microsoft.UI.Xaml.Controls;
namespace Pages;
public sealed partial class Page2 : Page
{
public Page2()
{
this.InitializeComponent();
}
public ViewModel<CarVersion2> ViewModel { get; } = new();
}

Page1.xaml

<Page
x:Class="Pages.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:Pages"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">
<Grid>
<TextBlock Text="{x:Bind ViewModel.CarVersion, Mode=OneWay}" />
</Grid>
</Page>

Page2.xaml

<Page
x:Class="Pages.Page2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:Pages"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">
<Grid>
<TextBlock Text="{x:Bind ViewModel.CarVersion, Mode=OneWay}" />
</Grid>
</Page>

MainWindow.xaml

<Window
x:Class="Pages.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:Pages"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid ColumnDefinitions="*,*">
<local:Page1 Grid.Column="0" />
<local:Page2 Grid.Column="1" />
</Grid>
</Window>

您可以为两个页面创建一个BaseViewModel。然后继承它。您可以参考这个示例示例。

最新更新