如何为多个客户定位 Xamarin PCL 应用程序?

How to target a Xamarin PCL application for multiple customers?

我在 Visual Studio Community 2015 中有一个 Xamarin PCL 解决方案。

我有兴趣为两个不同的客户为 Android 和 iOS 部署相同的应用程序。唯一会改变的是初始图像和应用程序图标。

我考虑过在我的 git 存储库中设置不同的分支,每个分支对应一个客户。但我认为这不是一个合适的解决方案。

另外,我想过有不同的配置方案,例如在iOS:

Configuration: [Ad-Hoc customer 1] - Platform: iPhone
Configuration: [Ad-Hoc customer 2] - Platform: iPhone

然后更改每个配置的项目属性的参数。但是如果我针对特定配置更改参数,切换到其他配置,然后我返回第一个配置的参数,有第二个配置的参数。

如果我没记错的话,我必须为每个构建指定不同的包名称,否则,GooglePlay 和 Apple Store 不会接受我的应用程序。

在Xcode中的概念是target/targeting。

在 Visual Studio 2015 年实现我的 objective 的正确形式是什么?

很遗憾,Visual Studio 不支持此功能。

但是还有第三种解决方案。您可以创建另一个项目并将所有(静态)文件作为 link 添加到项目中。有了这个,您不必在配置或分支之间切换,您只需 select 项目。

要将文件添加为 link,您必须右键单击该文件夹 -> 添加 -> 现有文件 -> select 来自原始项目的文件 -> "add"单击箭头和 select "add as link"(或类似的)

如果您想通过配置来完成,我建议您阅读此

我已经通过以下步骤实现了解决方案:

  1. 在我的 Visual Studio 解决方案中,在 Droid 项目中,我创建了一个文件夹 "Customers" 并在该文件夹中创建了两个子文件夹(每个子文件夹用于客户 "CustomerA" 和"CustomerB")
  2. 我已将图像、resx 等文件放入客户文件夹中,我想为每位客户更改这些文件。
  3. 在项目的根结构中,我创建了一个 PowerShell 脚本 (.ps1) 并编写了以下代码:

    param ([string] $ProjectDir, [string] $SolutionDir, [string] $SolutionName)
    
    Write-Host "ProjectDir --> " $ProjectDir
    Write-Host "SolutionDir -->" $SolutionDir
    Write-Host "SolutionName --> " $SolutionName
    
    $PathCustomerFileName = $SolutionDir + $SolutionName + "\" + $SolutionName + "\CustomerTarget.txt"
    
    Write-Host "PathCustomerFileName --> " $PathCustomerFileName
    
    $ConfigurationName = Get-Content $SolutionDir$SolutionName"\"$SolutionName"\CustomerTarget.txt"
    
    $CorrectCustomerName = "CustomerA","CustomerB" -Contains $ConfigurationName
    
    If ($ConfigurationName.length -gt 0 -And $CorrectCustomerName) {
    
        $Origin = $ProjectDir + "\Customers\" + $ConfigurationName + ""
        $Destination = $ProjectDir + "\Resources"
    
        robocopy $Origin $Destination /S
    
    } else {
        Write-Host "[ERROR] Error copying customer resources. Check ShellScript params and " $PathCustomerFileName " content file."
    }
    

它将文件从特定客户文件夹复制到 "Resources" 文件夹项目。

  1. 我已经转到项目的属性:双击 "Properties" -> 编译事件 --> 预构建,我已经设置了这个调用:

    PowerShell -File "$(ProjectDir)GetCustomerResources.ps1" $(ProjectDir) $(SolutionDir) $(SolutionName)

  2. 前面的步骤,我在每个平台项目中都做过(Android,iOS,...)。

  3. 然后,我在"Portable"项目中添加了一个"CustomerTarget.txt"文件,他的内容是"CustomerA"或"CustomerB"。

  4. 重建解决方案,仅此而已!