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

btqmn9zl  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(126)

我有2个页面几乎相同的代码隐藏(. xaml.cs文件)。他们有不同的布局虽然(.xaml文件是不同的)。在代码隐藏文件,唯一的区别是变量的类型。所有其他过程/函数是完全相同的。
例如:
第一页:

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

第二页:

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

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

是否可以只使用1个代码隐藏文件而不是复制它?

0mkxixxg

0mkxixxg1#

我想你需要创建一个ViewModel,并把所有的通用逻辑放在那里。

汽车版本.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);
    }
}

视图模型.cs

using CommunityToolkit.Mvvm.ComponentModel;

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

第一页. 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();
}

第二页. 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();
}

第一页. 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>

第二页. 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>

主窗口.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>
1l5u6lss

1l5u6lss2#

你可以为两个页面创建一个BaseViewModel。然后继承它。你可以参考这个sample example

相关问题