简体   繁体   English

适用于iPhone,Android的特定视图(Asp.net MVC)

[英]Specific View (Asp.net MVC) for IPhone, Android

I am using .. 我在用 ..

  • ASP.net MVC 4
  • 51Degrees.mobi
  • jQuery Mobile

By the helping of these technologies, I can make my web application's UI designs look good not only at Desktop based browsers but also at mobile based browsers without requiring me to create projects separately. 通过这些技术的帮助,我不仅可以使我的Web应用程序的UI设计在基于桌面的浏览器上而且在基于移动的浏览器上看起来都不错,而无需我单独创建项目。

But when it comes to more specific mobile devices, I would like to call specific view file. 但是,对于更特定的移动设备,我想调用特定的视图文件。
So I use below code at Global.asax file. 因此,我在Global.asax文件中使用以下代码。

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        //The Android view
        DisplayModes.Modes.Insert(0, new DefaultDisplayMode("android")
        {                
            ContextCondition = Context => Context.Request.Browser.Platform == "Android"
        });

        //The iPhone view
        DisplayModes.Modes.Insert(0, new DefaultDisplayMode("iphone")
        {
            ContextCondition = Context => Context.Request.Browser.MobileDeviceModel == "iPhone"                
        });

        //The mobile view
        //This has a lower priority than the other two so will only be used by a mobile device
        //that isn't Android or iPhone
        DisplayModes.Modes.Insert(1, new DefaultDisplayMode("mobile")
        {
            ContextCondition = Context => Context.Request.Browser.IsMobileDevice                
        });

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

Unfortunately, Android and IPhone specific view do not load whenever I call pages from IPhone Emulator and Opera Mobile Emulator. 不幸的是,每当我从iPhone模拟器和Opera Mobile模拟器调用页面时,都不会加载Android and IPhone specific view

_Layout.cshtml   [loaded from desktop based browser]
_Layout.Android.cshtml [never loaded]
_Layout.iPhone.cshtml  [never loaded]
_Layout.Mobile.cshtml  [loaded from mobile based any browser including iphone, opera] 

What I assume that something wrong is I get only two files when I downloaded from 51Degrees.mobi by using NuGet package. 我认为有问题的是,使用NuGet软件包从51Degrees.mobi下载文件时,我只能得到两个文件。

FiftyOne.Foundation.dll
51Degrees.mobi.config

Even though I think I should get App_Data/Devices.dat but I still only get these two files from 51Degrees.mobi. 即使我认为我应该获取App_Data/Devices.dat但仍然只能从51Degrees.mobi获取这两个文件。

Could anyone please give suggestion to me how I could call specific view for IPhone and Android? 谁能给我建议,如何为iPhone和Android调用特定视图? Every suggestion will be appreciated. 每个建议将不胜感激。

I have just done exactly this and had the same behaviour. 我恰好做到了这一点,并且具有相同的行为。 For starters the NuGet package is correct. 对于初学者,NuGet软件包是正确的。 The device.dat file used to be stored in APP_Data however if you are using the 'lite' version this is now embedded inside the FiftyOne.Foundation.dll. device.dat文件曾经存储在APP_Data中,但是,如果您使用的是“精简版”版本,则现在将其嵌入在FiftyOne.Foundation.dll中。

To fix the iPhone this is a case senesitive test. 要修复iPhone,这是区分大小写的测试。 FiftyOne sets the MobileDeviceModel to 'IPhone' (capital I) - This worked with the electric plum iphone emulator. FiftyOne将MobileDeviceModel设置为“ IPhone”(大写I)-这与电动梅花iphone仿真器一起使用。

For android to work it seems that the 'lite' version doesn't set the platform to 'Android'. 为了使android正常工作,似乎“精简版”未将平台设置为“ Android”。 Easy workaround is to use the UserAgent string. 一种简单的解决方法是使用UserAgent字符串。 ie ContextCondition = Context => Context.GetOverriddenUserAgent().Contains("Android") 即ContextCondition = Context => Context.GetOverriddenUserAgent()。Contains(“ Android”)

Finally you need to be careful about how you insert these items into the collection. 最后,您需要注意如何将这些项目插入集合中。 The code above inserts the Android rule then Inserts the IPhone rule (so android is now in position 1 in the collection) Then inserts the Mobile rule in position 1 - so the collection ends up looking like: IPhone Mobile Android 上面的代码插入Android规则,然后插入IPhone规则(因此android现在位于集合中的位置1),然后将Mobile规则插入位置1-这样集合最终看起来像:IPhone Mobile Android

As such an android device will always choose the Mobile rule first and never display the Android specific browser page. 因此,Android设备将始终始终选择“移动”规则,而从不显示特定于Android的浏览器页面。 So change the Inserts to 0,1 & 2 in the order above. 因此,按上述顺序将“插入”更改为0,1&2。 This gives the same order as the code and everything works just fine. 这给出了与代码相同的顺序,并且一切正常。

As an aside to fit with the ASP.Net MVC 4 initilisation style I separated this code out into the APP_Start folder in its own class ie. 除了适合ASP.Net MVC 4初始化样式外,我将此代码分离到了自己类的APP_Start文件夹中。

public class DeviceConfig
{
    public static void RegisterDevices(IList<IDisplayMode> modes)
    {
        //The Android view
        modes.Insert(0, new DefaultDisplayMode("android")
        {
            ContextCondition = Context => Context.GetOverriddenUserAgent().Contains("Android")
        });

        //The iPhone view
        modes.Insert(1, new DefaultDisplayMode("iphone")
        {
            ContextCondition = Context => Context.Request.Browser.MobileDeviceModel == "IPhone"
        });

        //The mobile view
        //This has a lower priority than the other two so will only be used by a mobile device
        //that isn't Android or iPhone
        modes.Insert(2, new DefaultDisplayMode("mobile")
        {
            ContextCondition = Context => Context.Request.Browser.IsMobileDevice
        });
    }
}

and then in Global.asax.cs 然后在Global.asax.cs中

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        DeviceConfig.RegisterDevices(DisplayModeProvider.Instance.Modes);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM