Home > other >  2 XAML pages with almost identical code-behind
2 XAML pages with almost identical code-behind

Time:11-16

I have 2 Pages with almost identical code behind (.xaml.cs file). They have different layout though (.xaml files are different). In Code-behind file, the only difference is the type of the variable. All other procedures/functions are exactly the same.

For example:

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;
   }
}

Is there anyway to use just 1 code-behind file instead of duplicating it?

CodePudding user response:

You can create a BaseViewModel for both of the Pages. Then inherit it. you can refer to this sample example for reference.

CodePudding user response:

I guess you need to create a ViewModel and put all your common logics there. Something like this:

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>
  • Related