【问题标题】:Conditional Compilation in Xamarin using MvvmCross使用 MvvmCross 在 Xamarin 中进行条件编译
【发布时间】:2014-07-11 17:59:51
【问题描述】:

我是 xamarin 和 MvvmCross 框架的新手。我目前正在为 android、windows phone 和 iOS 创建一个多平台应用程序。我目前在查找应用程序在哪个平台上运行时遇到问题。

我想要做的是在 app.cs 文件中有一个 if 语句。我想检查这是否是一个 iOS 应用程序然后做一些事情,否则做这个。但是我还没有找到任何好的方法来做到这一点,我什至不确定它是否可以在这个文件中完成

到目前为止,这是我的代码:

using Cirrious.CrossCore;
using Cirrious.CrossCore.IoC;
using Cirrious.MvvmCross.ViewModels;
using tax.Mobile.Core.Interfaces;
using tax.Mobile.Core.Logic;strong text
namespace tax.Mobile.Core
{
public partial class App : MvxApplication
{
    public override void Initialize()
    {
        CreatableTypes()
            .EndingWith("Service")
            .AsInterfaces()
            .RegisterAsLazySingleton();

#if (__iOS__) 
        RegisterAppStart<ViewModels.FirstViewModel>();
#else
            RegisterAppStart<ViewModels.SearchViewModel>();
#endif
        Mvx.RegisterType<IWebService, MockWebService>();     
    }

}
}

谢谢!

【问题讨论】:

  • 符号的括号部分是否在项目属性中定义?通常你不会放它们。
  • 您是否遇到任何错误?您提供的代码有什么问题?
  • 听起来像是 IoC/DI 的工作,而不是 if/else 流控制。
  • 等等,你想让应用根据平台以不同的视图开始?

标签: ios xamarin.ios xamarin mvvmcross


【解决方案1】:

这不起作用,PCL 库在项目属性中使用自己的符号编译或由#define SYMBOL 语法定义。

要选择开始屏幕,您可以使用 App 构造函数和 Setup.CreateApp 方法

1) 在您的 PCL 中使用平台选项创建枚举

public enum OS
{
  Droid, Touch, WinPhone, WinStore, Mac, Wpf
}

2) 在你的 App 类构造函数中使用这个枚举值

public App (OS os)
{
    _os = os;
}

public override void Initialize()
{
    /*...*/

    switch(_os)
    {
        case OS.Touch:
            RegisterAppStart<ViewModels.FirstViewModel>(); break;
        default:
            RegisterAppStart<ViewModels.SearchViewModel>();
    }

    /*...*/
}

3) 在 Setup.CreateApp() 方法中将当前操作系统传递给 App

{
    return new.Core.App(OS.Droid)
}

【讨论】:

    猜你喜欢
    • 2017-02-08
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 2021-10-11
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多