0

I have a Windows Phone project and my business demands to create another one with some slight changes in the front-end (XAML). How to create another project that is identical to the first one but only the XAML files are different? I don't use MVVM. What I tried is creating a new project and copy the XAML files from the first one, and then LINK all other CS files, but it became a mess with all these namespaces and stuff.. I have resource dicionaries and lots of dependencies in the code. Any ideas how to make such a project that shares the same code-behind files with some differences in Visual Studio?

v.g.
  • 1,076
  • 5
  • 19
  • 38
  • I think [this](http://stackoverflow.com/questions/1116465/how-do-you-share-code-between-projects-solutions-in-visual-studio) may have already been answered. – Mike Eason Sep 28 '15 at 12:22

1 Answers1

0

XAML with a code behind are partial classes. You cannot have two partial classes referring to the same class in two different assemblies. Therefore I think you can't use common code behind for XAML from different projects.

The best approach is using a common view model for different views, but you don't use MVVM pattern.

Then you can use something like a proxy. The proxy is a common class in a separate assembly. It contains all logic and data. You get or set any state from your code behind only by the proxy.

UPD: Example:

It's a common contract for each view (it's an interface from a common assembly):

public interface IMyWindow
{
    Label HelloLabel { get; }
}

It's the first WPF project:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="First Application" Height="350" Width="525">
    <StackPanel>
        <Label x:Name="_helloLabel" Content ="Hello, I'm First Application!"></Label>
        <Button Click="ButtonBase_OnClick" Height="100">Press me</Button>
    </StackPanel>
</Window>

public partial class MainWindow : Window, IMyWindow
{
    private readonly MyWindowProxy _proxy;

    public MainWindow()
    {
        InitializeComponent();
        _proxy = new MyWindowProxy(this);
    }

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        _proxy.OnButtonClick();
    }

    public Label HelloLabel
    {
        get { return _helloLabel; }
    }
}

It's the second WPF project:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Second Application" Height="350" Width="525">
    <StackPanel>
        <Label x:Name="_helloLabel" Content ="Hello, I'm Second Application!"></Label>
        <Button Click="ButtonBase_OnClick" Width ="50" Height="50">OK</Button>
    </StackPanel>
</Window>

public partial class MainWindow : Window, IMyWindow
{
    private readonly MyWindowProxy _proxy;

    public MainWindow()
    {
        InitializeComponent();
        _proxy = new MyWindowProxy(this);
    }

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        _proxy.OnButtonClick();
    }

    public Label HelloLabel
    {
        get { return _helloLabel; }
    }
}

It's a proxy for each view (it's a class from a common assembly):

public class MyWindowProxy
{
    private readonly IMyWindow _window;

    public MyWindowProxy(IMyWindow window)
    {
        _window = window;
    }

    public void OnButtonClick()
    {
        _window.HelloLabel.Content = "Hello from common proxy!";
    }
}

Once again, this is NOT the best way to build an application architecture. I highly recommend using MVVM pattern then the question of separating of business logic disappear by itself.

Community
  • 1
  • 1
hcp
  • 3,268
  • 6
  • 26
  • 41