[英]Specific View (Asp.net MVC) for IPhone, Android
我在用 ..
ASP.net MVC 4
51Degrees.mobi
jQuery Mobile
通過這些技術的幫助,我不僅可以使我的Web應用程序的UI設計在基於桌面的瀏覽器上而且在基於移動的瀏覽器上看起來都不錯,而無需我單獨創建項目。
但是,對於更特定的移動設備,我想調用特定的視圖文件。
因此,我在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);
}
不幸的是,每當我從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]
我認為有問題的是,使用NuGet軟件包從51Degrees.mobi下載文件時,我只能得到兩個文件。
FiftyOne.Foundation.dll
51Degrees.mobi.config
即使我認為我應該獲取App_Data/Devices.dat
但仍然只能從51Degrees.mobi獲取這兩個文件。
誰能給我建議,如何為iPhone和Android調用特定視圖? 每個建議將不勝感激。
我恰好做到了這一點,並且具有相同的行為。 對於初學者,NuGet軟件包是正確的。 device.dat文件曾經存儲在APP_Data中,但是,如果您使用的是“精簡版”版本,則現在將其嵌入在FiftyOne.Foundation.dll中。
要修復iPhone,這是區分大小寫的測試。 FiftyOne將MobileDeviceModel設置為“ IPhone”(大寫I)-這與電動梅花iphone仿真器一起使用。
為了使android正常工作,似乎“精簡版”未將平台設置為“ Android”。 一種簡單的解決方法是使用UserAgent字符串。 即ContextCondition = Context => Context.GetOverriddenUserAgent()。Contains(“ Android”)
最后,您需要注意如何將這些項目插入集合中。 上面的代碼插入Android規則,然后插入IPhone規則(因此android現在位於集合中的位置1),然后將Mobile規則插入位置1-這樣集合最終看起來像:IPhone Mobile Android
因此,Android設備將始終始終選擇“移動”規則,而從不顯示特定於Android的瀏覽器頁面。 因此,按上述順序將“插入”更改為0,1&2。 這給出了與代碼相同的順序,並且一切正常。
除了適合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
});
}
}
然后在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.