2 个 XAML 页面具有几乎相同的代码隐藏

问题描述 投票:0回答:2

我有 2 个页面,后面的代码几乎相同(.xaml.cs 文件)。但它们具有不同的布局(.xaml 文件不同)。在代码隐藏文件中,唯一的区别是变量的类型。所有其他程序/功能都完全相同。

例如:

第1页:

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

第2页:

public sealed partial class Page2 : Page
{
   public List<CarVersion2> cars = new List<CarVersion2>();
   public CarVersion2 currentCar;
   ...

   private UpdatePrice(int p) {
       currentCar.Price = p;
   }
}

有没有办法只使用 1 个代码隐藏文件而不是复制它?

c# xaml uwp winui-3
2个回答
0
投票

我想你需要创建一个 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>

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.